From e5e07c3746358fc70afc2e5b0344a5e7573b444a Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Tue, 19 Feb 2013 11:00:38 -0500 Subject: Ensure toc attribute is available on Markdown class. This appears to have recently been broken with the fixes in #191. This time I've added tests to prevent future breakage and added documentation to explain the behavior. --- docs/extensions/toc.txt | 21 +++++++++++++++++++-- markdown/extensions/toc.py | 6 +++--- tests/test_extensions.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/docs/extensions/toc.txt b/docs/extensions/toc.txt index 632294b..af282c6 100644 --- a/docs/extensions/toc.txt +++ b/docs/extensions/toc.txt @@ -40,8 +40,12 @@ would generate the following output:

Header 1

Header 2

-Configuration Options ---------------------- +Usage +----- + +From the Python interpreter: + + >>> html = markdown.markdown(some_text, extensions=['toc']) The following options are provided to configure the output: @@ -54,3 +58,16 @@ The following options are provided to configure the output: * **title**: Title to insert in TOC ``
``. Defaults to ``None``. * **anchorlink**: Set to ``True`` to have the headers link to themselves. Default is ``False``. + +If a 'marker' is not found in the document, then the toc is available as an +attribute of the Markdown class. This allows one to insert the toc elsewhere +in their page template. For example: + + >>> text = ''' + # Header 1 + + ## Header 2 + ''' + >>> md = markdown.Markdown(extensions=['toc']) + >>> html = md.convert(text) + >>> render_some_template(context={'body': html, 'toc': md.toc}) diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 173ae17..580d77e 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -170,9 +170,9 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor): self.add_anchor(c, elem_id) - if marker_found: - toc_list_nested = order_toc_list(toc_list) - self.build_toc_etree(div, toc_list_nested) + toc_list_nested = order_toc_list(toc_list) + self.build_toc_etree(div, toc_list_nested) + if not marker_found: # serialize and attach to markdown instance. prettify = self.markdown.treeprocessors.get('prettify') if prettify: prettify.run(div) diff --git a/tests/test_extensions.py b/tests/test_extensions.py index bee270a..fa9a801 100644 --- a/tests/test_extensions.py +++ b/tests/test_extensions.py @@ -371,3 +371,42 @@ class TestAdmonition(unittest.TestCase): ] for test, expected in tests: self.assertEqual(RE.match(test).groups(), expected) + +class TestTOC(unittest.TestCase): + """ Test TOC Extension. """ + + def setUp(self): + self.md = markdown.Markdown(extensions=['toc']) + + def testMarker(self): + """ Test TOC with a Marker. """ + text = '[TOC]\n\n# Header 1\n\n## Header 2' + self.assertEqual(self.md.convert(text), + '
\n' + '\n' + '
\n' + '

Header 1

\n' + '

Header 2

') + + def testNoMarker(self): + """ Test TOC without a Marker. """ + text = '# Header 1\n\n## Header 2' + self.assertEqual(self.md.convert(text), + '

Header 1

\n' + '

Header 2

') + self.assertEqual(self.md.toc, + '
\n' + '\n' + '
\n') \ No newline at end of file -- cgit v1.2.3