Fix markdown in HTML behaviour for Python-Markdown 3.3.3

This commit is contained in:
RunasSudo 2020-12-20 23:14:22 +11:00
parent 84493063fd
commit 5e4d32b20d
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -27,16 +27,10 @@ class WNMarkdown(markdown.Markdown):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.registerExtensions([FootnoteExtension(), 'toc', 'tables'], {})
self.registerExtensions([FootnoteExtension(), 'toc', 'tables', 'md_in_html'], {})
self.meta = {}
# Markdown in HTML
self.preprocessors['html_block'].markdown_in_raw = True
self.parser.blockprocessors.register(markdown.extensions.md_in_html.MarkdownInHtmlProcessor(self.parser), 'markdown_block', 105)
self.parser.blockprocessors.tag_counter = -1
self.parser.blockprocessors.contain_span_tags = re.compile(r'^(p|h[1-6]|li|dd|dt|td|th|legend|address)$', re.IGNORECASE)
# Override default Markdown processors
self.preprocessors.register(NormalizeWhitespace(self), 'normalize_whitespace', 30)
self.parser.blockprocessors.register(HashHeaderProcessor(self.parser), 'hashheader', 70)
@ -136,6 +130,17 @@ class NormalizeWhitespace(markdown.preprocessors.Preprocessor):
source = re.sub(r'(?<=\n) +\n', '\n', source)
return source.split('\n')
# Override to inherit markdown attribute
# This is required since Python-Markdown 3.3.3 so we implement this for backwards compatibility
def HTMLExtractorExtra_get_state(self, tag, attrs):
if 'markdown' not in attrs:
parent_state = self.mdstate[-1] if self.mdstate else None
if parent_state != 'off':
attrs['markdown'] = '1'
return HTMLExtractorExtra_get_state.orig(self, tag, attrs)
HTMLExtractorExtra_get_state.orig = markdown.extensions.md_in_html.HTMLExtractorExtra.get_state
markdown.extensions.md_in_html.HTMLExtractorExtra.get_state = HTMLExtractorExtra_get_state
class DirectiveProcessor(markdown.blockprocessors.BlockProcessor):
RE = re.compile(r'^.. +(?P<name>[a-zA-Z0-9_-]+?)::(?: +(?P<arg>.*?))?(?:\n|$)')