aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--docs/extensions/toc.txt21
-rw-r--r--markdown/extensions/toc.py6
-rw-r--r--tests/test_extensions.py39
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:
<h1 id="header-1">Header 1</h1>
<h1 id="header-2">Header 2</h1>
-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 ``<div>``. 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),
+ '<div class="toc">\n'
+ '<ul>\n'
+ '<li><a href="#header-1">Header 1</a>'
+ '<ul>\n'
+ '<li><a href="#header-2">Header 2</a></li>\n'
+ '</ul>\n'
+ '</li>\n'
+ '</ul>\n'
+ '</div>\n'
+ '<h1 id="header-1">Header 1</h1>\n'
+ '<h2 id="header-2">Header 2</h2>')
+
+ def testNoMarker(self):
+ """ Test TOC without a Marker. """
+ text = '# Header 1\n\n## Header 2'
+ self.assertEqual(self.md.convert(text),
+ '<h1 id="header-1">Header 1</h1>\n'
+ '<h2 id="header-2">Header 2</h2>')
+ self.assertEqual(self.md.toc,
+ '<div class="toc">\n'
+ '<ul>\n'
+ '<li><a href="#header-1">Header 1</a>'
+ '<ul>\n'
+ '<li><a href="#header-2">Header 2</a></li>\n'
+ '</ul>\n'
+ '</li>\n'
+ '</ul>\n'
+ '</div>\n') \ No newline at end of file