import re

from BeautifulSoup import BeautifulSoup
from flickipedia.config.settings import GET_VAR_ARTICLE


def parse_strip_elements(html):
    """
    Strips out undesirable divs & spans
    """
    soup = BeautifulSoup(html)

    # Remove thumbs
    for node in soup.findAll(attrs={'class': re.compile(r".*\bgallery\b.*")}): node.extract()
    for node in soup.findAll(attrs={'class': re.compile(r".*\bthumb\b.*")}): node.extract()
    for node in soup.findAll(attrs={'class': re.compile(r".*\bimage\b.*")}): node.extract()

    for div in soup.findAll('div', 'gallery'): div.extract()
    for td in soup.findAll('td', 'mbox-image'): td.extract()

    # Remove edit links
    for div in soup.findAll('span', 'mw-editsection'): div.extract()
    return str(soup)


def parse_convert_links(html):
    """
    Handles converting links to Flickipedia
    """
    # TODO - get all the links

    soup = BeautifulSoup(html)

    # for a in soup.findAll('a', 'mw-redirect'):
    #     a['href'] = a['href'].replace('/wiki/', '?' + GET_VAR_ARTICLE + '=')

    for a in soup.findAll('a'):
        if re.search(r'/wiki/', a['href']):
            a['href'] = a['href'].replace('/wiki/', '?' + GET_VAR_ARTICLE + '=')
    return str(soup)
@@ -1201,28 +1201,728 @@
  + '=')%0A    return str(soup)
+%0A%0A%0Adef embed_photo_content(photo, section_head):%0A    %22%22%22%0A    Embeds a new photo at the top of a section%0A%0A    :param photo:       Photo info from flickr.photos.search%0A    :param section:     Section header%0A%0A    return section%0A    %22%22%22%0A%0A    tag = section_head.new_tag('a')%0A    tag.string = '%3Ca href=%22https://www.flickr.com/photos/' + photo.owner + %5C%0A                 '/' + photo.photo_id  + '%22 title=%22' + photo.title + %5C%0A                 '%22%3E%3Cimg src=%22https://farm' + photo.farm + %5C%0A                 '.staticflickr.com/' + photo.server + '/' + photo.photo_id %5C%0A                 + '_' +  photo.secret + '.jpg%22 width=%22300%22 height=%22300%22%3E%3C/a%3E'%0A%0A    section_head.insert_after(tag)%0A    return str(section_head)

add - method that inserts a tag after the section header.