Skip to content

Instantly share code, notes, and snippets.

@zgoda
Created June 28, 2020 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zgoda/ada9550ee053be91378a6a68b153f380 to your computer and use it in GitHub Desktop.
Save zgoda/ada9550ee053be91378a6a68b153f380 to your computer and use it in GitHub Desktop.
"""
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