-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom_md_extension.py
36 lines (31 loc) · 1.5 KB
/
custom_md_extension.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
from markdown.extensions import Extension
from markdown.blockprocessors import BlockProcessor
import re
import xml.etree.ElementTree as etree
class LibDocBlockProcessor(BlockProcessor):
RE_FENCE_START = r'^ *!{5,} *\n' # start line, e.g., ` !!!! `
RE_FENCE_END = r'\n *!{5,}\s*$' # last non-blank line, e.g, '!!!\n \n\n'
def test(self, parent, block):
return re.match(self.RE_FENCE_START, block)
def run(self, parent, blocks):
original_block = blocks[0]
blocks[0] = re.sub(self.RE_FENCE_START, '', blocks[0])
# Find block with ending fence
for block_num, block in enumerate(blocks):
if re.search(self.RE_FENCE_END, block):
# remove fence
blocks[block_num] = re.sub(self.RE_FENCE_END, '', block)
# render fenced area inside a new div
e = etree.SubElement(parent, 'div')
e.set('class', 'custom_lib_docs')
self.parser.parseBlocks(e, blocks[0:block_num + 1])
# remove used blocks
for i in range(0, block_num + 1):
blocks.pop(0)
return True # or could have had no return statement
# No closing marker! Restore and do nothing
blocks[0] = original_block
return False # equivalent to our test() routine returning False
class LibDocExtension(Extension):
def extendMarkdown(self, md):
md.parser.blockprocessors.register(LibDocBlockProcessor(md.parser), 'box', 175)