-
Notifications
You must be signed in to change notification settings - Fork 1
/
markdown_smarttoc.py
51 lines (39 loc) · 1.66 KB
/
markdown_smarttoc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from markdown.util import etree
import markdown
__author__ = 'tigge'
class SmartTocTreeporcessor(markdown.treeprocessors.Treeprocessor):
def __init__(self, md):
super(SmartTocTreeporcessor, self).__init__(md)
self.running = False
self.headers = []
def collect_headers(self, element, indent=0):
if element.tag == "p" and element.text == "${toc}":
self.running = True
if element.tag == "h1" and self.running:
element.attrib["id"] = "toc-" + str(len(self.headers))
text = element.text
for postprocessor in self.markdown.postprocessors.values():
text = postprocessor.run(text)
self.headers.append({"anchor": element.attrib["id"], "text": text})
for child in element:
self.collect_headers(child, indent=indent + 1)
def build_toc(self):
string = '<div class="toc">\n'
string += ' <ul>\n'
for header in self.headers:
string += ' <li><a href="#{0}">{1}</a></li>'.format(header["anchor"], header["text"])
string += ' </ul>\n'
string += '</div>'
return string
def run(self, root):
self.collect_headers(root)
self.markdown.toc = self.build_toc()
class SmartTocExtension(markdown.Extension):
def __init__(self, *args, **kwargs):
super(SmartTocExtension, self).__init__(*args, **kwargs)
def extendMarkdown(self, md, md_globals):
md.registerExtension(self)
treeprocessor = SmartTocTreeporcessor(md)
md.treeprocessors.add("toc", treeprocessor, "_end")
def makeExtension(*args, **kwargs):
return SmartTocExtension(*args, **kwargs)