-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown_frontpage.py
55 lines (37 loc) · 1.47 KB
/
markdown_frontpage.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 FrontpageTreeprocessor(markdown.treeprocessors.Treeprocessor):
def run(self, doc):
# Create the cool header
headerone = doc.find("h1")
headertwo = doc.find("h2")
headerone.tag = "h2"
headerone.attrib['class'] = 'featurette-heading'
headerone.text += ' '
muted = ET.Element('span', attrib={'class':'small'})
muted.text = headertwo.text
headerone.insert(0, muted)
doc.remove(headertwo)
# Make lead
p = doc.find("p")
p.attrib['class'] = 'lead'
class FrontpageExtension(markdown.extensions.Extension):
TreeProcessorClass = FrontpageTreeprocessor
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, "frontpage", md.treeprocessors._priority[-1].priority - 10)
def makeExtension(configs={}):
return FrontpageExtension(configs=configs)