-
Notifications
You must be signed in to change notification settings - Fork 0
/
titleoffset.py
35 lines (28 loc) · 1.05 KB
/
titleoffset.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
import re
from xml.etree import ElementTree
from markdown.extensions import Extension
from markdown.treeprocessors import Treeprocessor
TITLE_RE = re.compile(r"[hH][1-6]")
class TitleOffsetProcessor(Treeprocessor):
"""
Offset the title level by some number.
Useful if the markdown isn't supposed to start at h1 but h3 for example.
"""
offset = 0
def run(self, root: ElementTree.ElementTree):
for elm in root.iter():
if TITLE_RE.match(elm.tag):
level = int(elm.tag.lstrip("hH"))
level += self.offset
elm.tag = f"h{level}"
class TitleOffset(Extension):
"""An extension to register the TitleOffsettProcessor."""
def __init__(self, **kwargs):
self.config = {
'offset': [0, 'The offset to apply to h[n] elements']
}
super().__init__(**kwargs)
def extendMarkdown(self, md):
processor = TitleOffsetProcessor(md)
processor.offset = self.getConfig('offset', 0)
md.treeprocessors.register(processor, 'titleoffset', 1)