-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown_fixtitle.py
55 lines (37 loc) · 1.5 KB
/
markdown_fixtitle.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
52
53
54
55
import markdown
import xml.etree.ElementTree as ET
class FixTitleTreeprocessor(markdown.treeprocessors.Treeprocessor):
def run(self, doc):
# Create the cool header
headerone = doc.find("h1")
headertwo = doc.find("h2")
headerone.text += ' '
muted = ET.Element('span', attrib={'class':'small'})
muted.text = headertwo.text
headerone.insert(0, muted)
doc.remove(headerone)
doc.remove(headertwo)
# Make the page title div
pheader = ET.Element('div', attrib={'class': 'page-header'})
pheader.insert(0, headerone)
doc.insert(0, pheader)
class FixTitleExtension(markdown.extensions.Extension):
TreeProcessorClass = FixTitleTreeprocessor
def __init__(self, configs=[]):
self.config = {}
for key, value in configs.items():
self.setConfig(key, value)
def extendMarkdown(self, md):
fext = self.TreeProcessorClass(md)
fext.config = self.getConfigs()
# Headerid ext is set to '>prettify'. With this set to the lowest
# priority, it should always come after headerid ext (and honor ids
# assigned by the header id extension) if both are used. Same goes for
# attr_list extension. This must come last because we don't want to
# redefine ids after toc is created. But we do want toc prettified.
# Get the last element registered to force a sort then sneak a look at
# its priority..
_ = md.treeprocessors[-1]
md.treeprocessors.register(fext, "fixtitle", md.treeprocessors._priority[-1].priority - 10)
def makeExtension(configs={}):
return FixTitleExtension(configs=configs)