Merge branch 'mistune-2.0.2' into 'main'

Port to Mistune 2.0.2

See merge request mx-moment/moment!16
This commit is contained in:
Maze 2023-03-17 21:59:21 +00:00
commit 111462d3d0
2 changed files with 20 additions and 78 deletions

View File

@ -5,7 +5,7 @@ cairosvg >= 2.4.2, < 3
filetype >= 1.0.7, < 2 filetype >= 1.0.7, < 2
html_sanitizer >= 1.9.1, < 2 html_sanitizer >= 1.9.1, < 2
lxml >= 4.5.1, < 5 lxml >= 4.5.1, < 5
mistune >= 0.8.4, < 0.9 mistune >= 2.0.0, < 3.0
pymediainfo >= 4.2.1, < 5 pymediainfo >= 4.2.1, < 5
plyer >= 1.4.3, < 2 plyer >= 1.4.3, < 2
sortedcontainers >= 2.2.2, < 3 sortedcontainers >= 2.2.2, < 3

View File

@ -18,84 +18,32 @@ from lxml.html import HtmlElement, etree # nosec
from .color import SVGColor from .color import SVGColor
class MarkdownInlineGrammar(mistune.InlineGrammar): def parse_colour(inline, m, state):
"""Markdown inline elements syntax modifications for the Mistune parser. 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: `<color>(text)`, def render_html_colour(colour, text):
e.g. `<red>(Lorem ipsum)` or `<#000040>(sit dolor amet...)` return f'<span data-mx-color="{colour}">{text}</span>'
"""
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"<b>(x) <r>(x) \<a>b>(x) <a\>b>(x) <b>(\(z) <c>(foo\)xyz)" # test string: r"<b>(x) <r>(x) \<a>b>(x) <a\>b>(x) <b>(\(z) <c>(foo\)xyz)"
color = re.compile( colour = (
r"^<(.+?)>" # capture the color in `<color>` r"^<(.+?)>" # capture the colour in `<colour>`
r"\((.+?)" # capture text in `(text` r"\((.+?)" # capture text in `(text`
r"(?<!\\)(?:\\\\)*" # ignore the next `)` if it's \escaped r"(?<!\\)(?:\\\\)*" # ignore the next `)` if it's \escaped
r"\)", # finish on a `)` r"\)" # finish on a `)`
) )
# Mark colour as high priority as otherwise e.g. <red>(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): if md.renderer.NAME == "html":
"""Markdown block elements syntax modifications for the Mistune parser. md.renderer.register("colour", render_html_colour)
- 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 `<span data-mx-color=#hex>`."""
# 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'<span data-mx-color="{color}">{text}</span>'
class HTMLProcessor: class HTMLProcessor:
@ -201,19 +149,13 @@ class HTMLProcessor:
# hard_wrap: convert all \n to <br> without required two spaces # hard_wrap: convert all \n to <br> without required two spaces
# escape: escape HTML characters in the input string, e.g. tags # 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, hard_wrap = True,
escape = True, escape = True,
inline = MarkdownInlineLexer, renderer = "html",
block = MarkdownBlockLexer, plugins = ['strikethrough', plugin_matrix],
renderer = MarkdownRenderer(),
) )
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]]: def mentions_in_html(self, html: str) -> List[Tuple[str, str]]:
"""Return list of (text, href) tuples for all mention links in html.""" """Return list of (text, href) tuples for all mention links in html."""