From 7b9b48b1aeb4f681de911c56f83f50262c494159 Mon Sep 17 00:00:00 2001 From: Newbyte Date: Sun, 17 Apr 2022 22:41:41 +0200 Subject: [PATCH] Port to Mistune 2.0.2 --- requirements.txt | 2 +- src/backend/html_markdown.py | 96 +++++++----------------------------- 2 files changed, 20 insertions(+), 78 deletions(-) diff --git a/requirements.txt b/requirements.txt index c49870a0..54a87efe 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ cairosvg >= 2.4.2, < 3 filetype >= 1.0.7, < 2 html_sanitizer >= 1.9.1, < 2 lxml >= 4.5.1, < 5 -mistune >= 0.8.4, < 0.9 +mistune >= 2.0.0, < 3.0 pymediainfo >= 4.2.1, < 5 plyer >= 1.4.3, < 2 sortedcontainers >= 2.2.2, < 3 diff --git a/src/backend/html_markdown.py b/src/backend/html_markdown.py index b7234d69..a159ea6f 100644 --- a/src/backend/html_markdown.py +++ b/src/backend/html_markdown.py @@ -18,84 +18,32 @@ from lxml.html import HtmlElement, etree # nosec from .color import SVGColor -class MarkdownInlineGrammar(mistune.InlineGrammar): - """Markdown inline elements syntax modifications for the Mistune parser. +def parse_colour(inline, m, state): + colour = m.group(1) + text = m.group(2) + return "colour", colour, text - - Disable underscores for bold/italics (e.g. `__bold__`) - - Add syntax for coloring text: `(text)`, - e.g. `(Lorem ipsum)` or `<#000040>(sit dolor amet...)` - """ +def render_html_colour(colour, text): + return f'{text}' - escape = re.compile(r"^\\([\\`*{}\[\]()#+\-.!_<>~|])") # Add < - emphasis = re.compile(r"^\*((?:\*\*|[^\*])+?)\*(?!\*)") - double_emphasis = re.compile(r"^\*{2}([\s\S]+?)\*{2}(?!\*)") +def plugin_matrix(md): # test string: r"(x) (x) \b>(x) b>(x) (\(z) (foo\)xyz)" - color = re.compile( - r"^<(.+?)>" # capture the color in `` + colour = ( + r"^<(.+?)>" # capture the colour in `` r"\((.+?)" # capture text in `(text` r"(?(hi) matches the + # inline_html rule instead of the colour rule. + md.inline.rules.insert(1, "colour") + md.inline.register_rule("colour", colour, parse_colour) -class MarkdownBlockGrammar(mistune.BlockGrammar): - """Markdown block elements syntax modifications for the Mistune parser. - - - Require a space after # for titles - """ - - heading = re.compile(r"^ *(#{1,6}) +([^\n]+?) *#* *(?:\n+|$)") - - -class MarkdownInlineLexer(mistune.InlineLexer): - """Apply the changes from `MarkdownInlineGrammar` for Mistune.""" - - grammar_class = MarkdownInlineGrammar - - default_rules = [ - "escape", "color", "autolink", "url", # Add color - "footnote", "link", "reflink", "nolink", - "double_emphasis", "emphasis", "code", - "linebreak", "strikethrough", "text", - ] - inline_html_rules = [ - "escape", "color", "autolink", "url", "link", "reflink", # Add color - "nolink", "double_emphasis", "emphasis", "code", - "linebreak", "strikethrough", "text", - ] - - - def output_double_emphasis(self, m): - return self.renderer.double_emphasis(self.output(m.group(1))) - - - def output_emphasis(self, m): - return self.renderer.emphasis(self.output(m.group(1))) - - - def output_color(self, m): - color = m.group(1) - text = self.output(m.group(2)) - return self.renderer.color(color, text) - - -class MarkdownBlockLexer(mistune.BlockLexer): - """Apply the changes from `MarkdownBlockGrammar` for Mistune.""" - - grammar_class = MarkdownBlockGrammar - - -class MarkdownRenderer(mistune.Renderer): - def color(self, color: str, text: str) -> str: - """Render given text with a color using ``.""" - - # This may be a SVG color name, try to get a #hex code from it: - with suppress(KeyError): - color = SVGColor[color.lower().replace(" ", "")].value.hex - - return f'{text}' + if md.renderer.NAME == "html": + md.renderer.register("colour", render_html_colour) class HTMLProcessor: @@ -201,19 +149,13 @@ class HTMLProcessor: # hard_wrap: convert all \n to
without required two spaces # escape: escape HTML characters in the input string, e.g. tags - self._markdown_to_html = mistune.Markdown( + self._markdown_to_html = mistune.create_markdown( hard_wrap = True, escape = True, - inline = MarkdownInlineLexer, - block = MarkdownBlockLexer, - renderer = MarkdownRenderer(), + renderer = "html", + plugins = ['strikethrough', plugin_matrix], ) - self._markdown_to_html.block.default_rules = [ - rule for rule in self._markdown_to_html.block.default_rules - if rule != "block_quote" - ] - def mentions_in_html(self, html: str) -> List[Tuple[str, str]]: """Return list of (text, href) tuples for all mention links in html."""