From 182a26caa7185ba3d951526c6dd19c1f32a1688a 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 | 19 +++++++++++++++++++ 2 files changed, 25 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..760069f 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -733,6 +733,25 @@ 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.assertEqual( + self.md.toc, + '
\n' + '\n' # noqa + '
\n' + ) + self.md.reset() + self.assertEqual(self.md.toc, '') + class TestSmarty(unittest.TestCase): def setUp(self): -- cgit v1.2.3