diff options
-rw-r--r-- | markdown/extensions/toc.py | 26 | ||||
-rw-r--r-- | tests/test_extensions.py | 42 |
2 files changed, 54 insertions, 14 deletions
diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index db6659c..e9d9c02 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -59,7 +59,7 @@ def order_toc_list(toc_list): for p in reversed(parents): if current_level <= p['level']: to_pop += 1 - else: + else: # pragma: no cover break if to_pop: levels = levels[:-to_pop] @@ -153,7 +153,6 @@ class TocTreeprocessor(Treeprocessor): used_ids.add(c.attrib["id"]) toc_list = [] - marker_found = False for (p, c) in self.iterparent(doc): text = ''.join(itertext(c)).strip() if not text: @@ -171,7 +170,6 @@ class TocTreeprocessor(Treeprocessor): if p[i] == c: p[i] = div break - marker_found = True if header_rgx.match(c.tag): @@ -200,12 +198,12 @@ class TocTreeprocessor(Treeprocessor): prettify = self.markdown.treeprocessors.get('prettify') if prettify: prettify.run(div) - if not marker_found: - # serialize and attach to markdown instance. - toc = self.markdown.serializer(div) - for pp in self.markdown.postprocessors.values(): - toc = pp.run(toc) - self.markdown.toc = toc + + # serialize and attach to markdown instance. + toc = self.markdown.serializer(div) + for pp in self.markdown.postprocessors.values(): + toc = pp.run(toc) + self.markdown.toc = toc class TocExtension(Extension): @@ -223,12 +221,12 @@ class TocExtension(Extension): "title": ["", "Title to insert into TOC <div> - " "Defaults to an empty string"], - "anchorlink": [0, - "1 if header should be a self link - " - "Defaults to 0"], + "anchorlink": [False, + "True if header should be a self link - " + "Defaults to False"], "permalink": [0, - "1 or link text if a Sphinx-style permalink should " - "be added - Defaults to 0"] + "True or link text if a Sphinx-style permalink should " + "be added - Defaults to False"] } super(TocExtension, self).__init__(*args, **kwargs) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index 2380f17..dae8829 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -741,6 +741,48 @@ class TestTOC(unittest.TestCase): self.md.reset() self.assertEqual(self.md.toc, '') + def testAnchorLink(self): + """ Test TOC Anchorlink. """ + md = markdown.Markdown( + extensions=[markdown.extensions.toc.TocExtension(anchorlink=True)] + ) + text = '# Header 1\n\n## Header *2*' + self.assertEqual( + md.convert(text), + '<h1 id="header-1"><a class="toclink" href="#header-1">Header 1</a></h1>\n' + '<h2 id="header-2"><a class="toclink" href="#header-2">Header <em>2</em></a></h2>' + ) + + def testTitle(self): + """ Test TOC Title. """ + md = markdown.Markdown( + extensions=[markdown.extensions.toc.TocExtension(title='Table of Contents')] + ) + md.convert('# Header 1\n\n## Header 2') + self.assertTrue(md.toc.startswith('<div class="toc"><span class="toctitle">Table of Contents</span><ul>')) + + def testWithAttrList(self): + """ Test TOC with attr_list Extension. """ + md = markdown.Markdown(extensions=['markdown.extensions.toc', 'markdown.extensions.attr_list']) + text = '# Header 1\n\n## Header 2 { #foo }' + self.assertEqual( + md.convert(text), + '<h1 id="header-1">Header 1</h1>\n' + '<h2 id="foo">Header 2</h2>' + ) + self.assertEqual( + md.toc, + '<div class="toc">\n' + '<ul>\n' # noqa + '<li><a href="#header-1">Header 1</a>' # noqa + '<ul>\n' # noqa + '<li><a href="#foo">Header 2</a></li>\n' # noqa + '</ul>\n' # noqa + '</li>\n' # noqa + '</ul>\n' # noqa + '</div>\n' + ) + class TestSmarty(unittest.TestCase): def setUp(self): |