From 4cabf3b94c17317b16d82e143e8221c88ff60d99 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Thu, 4 Aug 2011 14:05:31 -0400 Subject: TOC extension now attaches toc to Markdown instance (Markdown.toc), but only if a marker was not found in the document. --- markdown/extensions/toc.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/markdown/extensions/toc.py b/markdown/extensions/toc.py index 2fa5ce1..1f1de15 100644 --- a/markdown/extensions/toc.py +++ b/markdown/extensions/toc.py @@ -23,6 +23,8 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor): yield parent, child def run(self, doc): + marker_found = False + div = etree.Element("div") div.attrib["class"] = "toc" last_li = None @@ -61,6 +63,7 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor): if p[i] == c: p[i] = div break + marker_found = True if header_rgx.match(c.tag): tag_level = int(c.tag[-1]) @@ -106,6 +109,14 @@ class TocTreeprocessor(markdown.treeprocessors.Treeprocessor): c.append(anchor) list_stack[-1].append(last_li) + if not marker_found: + # searialize and attach to markdown instance. + prettify = self.markdown.treeprocessors.get('prettify') + if prettify: prettify.run(div) + toc = self.markdown.serializer(div) + for pp in self.markdown.postprocessors.values(): + toc = pp.run(toc) + self.markdown.toc = toc class TocExtension(markdown.Extension): def __init__(self, configs): -- cgit v1.2.3