From 52b9f8c1ea191ce9c1ae0cd485306460cb52f71b Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Tue, 30 Dec 2014 11:00:27 -0500 Subject: Add reset support to TOC extension. Now, whenever the TOC extensiuon is loaded, the Markdown class instance will always have a toc attribute (md.toc). Calling md.reset() will also reset the toc attribute which defaults to an empty string. --- markdown/extensions/toc.py | 6 ++++++ tests/test_extensions.py | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 99542ed..db6659c 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -234,6 +234,9 @@ class TocExtension(Extension): super(TocExtension, self).__init__(*args, **kwargs) def extendMarkdown(self, md, md_globals): + md.registerExtension(self) + self.md = md + self.reset() tocext = self.TreeProcessorClass(md) tocext.config = self.getConfigs() # Headerid ext is set to '>prettify'. With this set to '_end', @@ -243,6 +246,9 @@ class TocExtension(Extension): # to redefine ids after toc is created. But we do want toc prettified. md.treeprocessors.add("toc", tocext, "_end") + def reset(self): + self.md.toc = '' + def makeExtension(*args, **kwargs): return TocExtension(*args, **kwargs) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index e24118f..2380f17 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -733,6 +733,14 @@ class TestTOC(unittest.TestCase): '\n' ) + def testReset(self): + """ Test TOC Reset. """ + self.assertEqual(self.md.toc, '') + self.md.convert('# Header 1\n\n## Header 2') + self.assertTrue(self.md.toc.startswith('
')) + self.md.reset() + self.assertEqual(self.md.toc, '') + class TestSmarty(unittest.TestCase): def setUp(self): -- cgit v1.2.3 From 5ad00fab5d0942e8ff2b264e81db82ced75261a5 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Tue, 30 Dec 2014 11:16:20 -0500 Subject: Always add toc to Markdown Class Instance Previously, we only added the toc attribute (md.toc) if no Marker was found within the document. However, that has caused framworks to do things like force insert a marker, run convert, then extract the toc from the body of the document. This is much cleaner. And if the user wants to add the toc to the document also, they still can. --- markdown/extensions/toc.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index db6659c..b3bab93 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -200,12 +200,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): -- cgit v1.2.3 From ede69aee2e53cd2eb2c960dc3ecba2d423faac82 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Tue, 30 Dec 2014 12:02:35 -0500 Subject: Complete test coverage of TOC Extension --- markdown/extensions/toc.py | 14 ++++++-------- tests/test_extensions.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index b3bab93..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): @@ -223,12 +221,12 @@ class TocExtension(Extension): "title": ["", "Title to insert into TOC
- " "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), + '

Header 1

\n' + '

Header 2

' + ) + + 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('
Table of Contents
    ')) + + 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), + '

    Header 1

    \n' + '

    Header 2

    ' + ) + self.assertEqual( + md.toc, + '
    \n' + '
      \n' # noqa + '
    • Header 1' # noqa + '\n' # noqa + '
    • \n' # noqa + '
    \n' # noqa + '
    \n' + ) + class TestSmarty(unittest.TestCase): def setUp(self): -- cgit v1.2.3