Exemplo n.º 1
0
def insert_permalinks(el, permalink_text=None, permalink_class=None, permalink_title=None):
    for child in el:
        if child.tag in {'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8'}:
            permalink = etree.Element('a')

            # try to get any existing ID from the heading tag
            id = child.get('id', None)
            # if there is none, slugify the text of the heading tag
            if id is None:
                heading_text = strip_el_text(child, max_depth=3)
                id = slugify(heading_text.replace('\'', '').replace('"', ''))
                # apply the ID to the heading
                child.set('id', id)

            permalink.set('href', '#%s' % id)

            if permalink_class is not None:
                permalink.set('class', permalink_class)
            if permalink_title is not None:
                permalink.set('title', permalink_title)

            if permalink_text is not None:
                permalink.text = permalink_text

            child.append(permalink)

        # recursively insert permalinks
        insert_permalinks(
            child,
            permalink_text=permalink_text,
            permalink_class=permalink_class,
            permalink_title=permalink_title
        )
Exemplo n.º 2
0
def insert_permalinks(el,
                      permalink_text=None,
                      permalink_class=None,
                      permalink_title=None):
    for child in el:
        if child.tag in {'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8'}:
            permalink = etree.Element('a')

            # try to get any existing ID from the heading tag
            id = child.get('id', None)
            # if there is none, slugify the text of the heading tag
            if id is None:
                heading_text = strip_el_text(child, max_depth=3)
                id = slugify(heading_text.replace('\'', '').replace('"', ''))
                # apply the ID to the heading
                child.set('id', id)

            permalink.set('href', '#%s' % id)

            if permalink_class is not None:
                permalink.set('class', permalink_class)
            if permalink_title is not None:
                permalink.set('title', permalink_title)

            if permalink_text is not None:
                permalink.text = permalink_text

            child.append(permalink)

        # recursively insert permalinks
        insert_permalinks(child,
                          permalink_text=permalink_text,
                          permalink_class=permalink_class,
                          permalink_title=permalink_title)
Exemplo n.º 3
0
 def assert_andrew_bio_compiles(self, content):
     bio = ET.fromstring(content)
     self.assertEqual('html', bio.findall('.')[0].tag)
     self.assertEqual('Andrew Michaels', bio.findall('./head/title')[0].text.strip())
     self.assertEqual('mailto:[email protected]', bio.findall(".//div[@class='meta']/a")[0].attrib['href'])
     self.assertEqual('Contact Andrew', bio.findall(".//div[@class='meta']/a")[0].text.strip())
     bio_content = bio.findall(".//div[@class='content']/p")[0]
     bio_content_els = [el for el in bio_content]
     self.assertEqual('em', bio_content_els[0].tag)
     bio_content_text = strip_el_text(bio_content, max_depth=1)
     self.assertEqual("Here's Andrew's bio!", bio_content_text)
Exemplo n.º 4
0
 def assert_michael_bio_compiles(self, content):
     bio = ET.fromstring(content)
     self.assertEqual('html', bio.findall('.')[0].tag)
     self.assertEqual('Michael Anderson', bio.findall('./head/title')[0].text.strip())
     self.assertEqual('mailto:[email protected]', bio.findall(".//div[@class='meta']/a")[0].attrib['href'])
     self.assertEqual('Contact Michael', bio.findall(".//div[@class='meta']/a")[0].text.strip())
     bio_content = bio.findall(".//div[@class='content']/p")[0]
     bio_content_els = [el for el in bio_content]
     self.assertEqual('strong', bio_content_els[0].tag)
     self.assertEqual('Markdown', bio_content_els[0].text.strip())
     bio_content_text = strip_el_text(bio_content, max_depth=1)
     self.assertEqual("This is Michael's bio, in Markdown format.", bio_content_text)
Exemplo n.º 5
0
 def assert_andrew_bio_compiles(self, content):
     bio = ET.fromstring(content)
     self.assertEqual('html', bio.findall('.')[0].tag)
     self.assertEqual('Andrew Michaels',
                      bio.findall('./head/title')[0].text.strip())
     self.assertEqual(
         'mailto:[email protected]',
         bio.findall(".//div[@class='meta']/a")[0].attrib['href'])
     self.assertEqual(
         'Contact Andrew',
         bio.findall(".//div[@class='meta']/a")[0].text.strip())
     bio_content = bio.findall(".//div[@class='content']/p")[0]
     bio_content_els = [el for el in bio_content]
     self.assertEqual('em', bio_content_els[0].tag)
     bio_content_text = strip_el_text(bio_content, max_depth=1)
     self.assertEqual("Here's Andrew's bio!", bio_content_text)
Exemplo n.º 6
0
 def assert_michael_bio_compiles(self, content):
     bio = ET.fromstring(content)
     self.assertEqual('html', bio.findall('.')[0].tag)
     self.assertEqual('Michael Anderson',
                      bio.findall('./head/title')[0].text.strip())
     self.assertEqual(
         'mailto:[email protected]',
         bio.findall(".//div[@class='meta']/a")[0].attrib['href'])
     self.assertEqual(
         'Contact Michael',
         bio.findall(".//div[@class='meta']/a")[0].text.strip())
     bio_content = bio.findall(".//div[@class='content']/p")[0]
     bio_content_els = [el for el in bio_content]
     self.assertEqual('strong', bio_content_els[0].tag)
     self.assertEqual('Markdown', bio_content_els[0].text.strip())
     bio_content_text = strip_el_text(bio_content, max_depth=1)
     self.assertEqual("This is Michael's bio, in Markdown format.",
                      bio_content_text)
Exemplo n.º 7
0
 def assert_my_first_post_compiles(self, content):
     post = ET.fromstring(content)
     self.assertEqual('html', post.findall('.')[0].tag)
     self.assertEqual('My first post', post.findall('./head/title')[0].text.strip())
     self.assertEqual('2016-06-15', post.findall(".//div[@class='published']")[0].text.strip())
     self.assertEqual('/bios/michael/', post.findall(".//div[@class='author']/a")[0].attrib['href'])
     self.assertEqual('By Michael', post.findall(".//div[@class='author']/a")[0].text.strip())
     post_content = post.findall(".//div[@class='content']/p")[0]
     post_content_els = [el for el in post_content]
     self.assertEqual('strong', post_content_els[0].tag)
     self.assertEqual('Markdown', post_content_els[0].text.strip())
     self.assertEqual('code', post_content_els[1].tag)
     self.assertEqual('HTML', post_content_els[1].text.strip())
     post_content_text = strip_el_text(post_content, max_depth=1)
     self.assertEqual(
         "This is the Markdown content of the first post, which should appropriately be translated into the " +
         "relevant HTML code.",
         post_content_text
     )
Exemplo n.º 8
0
 def assert_my_first_post_compiles(self, content):
     post = ET.fromstring(content)
     self.assertEqual('html', post.findall('.')[0].tag)
     self.assertEqual('My first post',
                      post.findall('./head/title')[0].text.strip())
     self.assertEqual(
         '2016-06-15',
         post.findall(".//div[@class='published']")[0].text.strip())
     self.assertEqual(
         '/bios/michael/',
         post.findall(".//div[@class='author']/a")[0].attrib['href'])
     self.assertEqual(
         'By Michael',
         post.findall(".//div[@class='author']/a")[0].text.strip())
     post_content = post.findall(".//div[@class='content']/p")[0]
     post_content_els = [el for el in post_content]
     self.assertEqual('strong', post_content_els[0].tag)
     self.assertEqual('Markdown', post_content_els[0].text.strip())
     self.assertEqual('code', post_content_els[1].tag)
     self.assertEqual('HTML', post_content_els[1].text.strip())
     post_content_text = strip_el_text(post_content, max_depth=1)
     self.assertEqual(
         "This is the Markdown content of the first post, which should appropriately be translated into the "
         + "relevant HTML code.", post_content_text)