Skip to content

Instantly share code, notes, and snippets.

@zgoda zgoda/centerblock.py

Created Jun 28, 2020
Embed
What would you like to do?
"""
This is Python Markdown extension that adds support for centered blocks similar to Vuepress.
"""
class CenterBlockProcessor(BlockProcessor):
RE_START = r'^->'
RE_END = r'<-$'
def test(self, parent, block):
return re.match(self.RE_START, block)
def run(self, parent, blocks):
original_block = blocks[0]
blocks[0] = re.sub(self.RE_START, '', blocks[0])
for block_num, block in enumerate(blocks):
if re.search(self.RE_END, block):
blocks[block_num] = re.sub(self.RE_END, '', block)
e = etree.SubElement(parent, 'div')
e.set('style', 'text-align:center')
self.parser.parseBlocks(e, blocks[0:block_num + 1])
for _ in range(0, block_num + 1):
blocks.pop(0)
return True
blocks[0] = original_block
return False
class CenterBlockExtension(Extension):
def extendMarkdown(self, md): # noqa: N802
md.parser.blockprocessors.register(
CenterBlockProcessor(md.parser), 'centerblock', 175
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.