Port to Mistune 2.0.2
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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: `<color>(text)`,
 | 
			
		||||
      e.g. `<red>(Lorem ipsum)` or `<#000040>(sit dolor amet...)`
 | 
			
		||||
    """
 | 
			
		||||
def render_html_colour(colour, text):
 | 
			
		||||
    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)"
 | 
			
		||||
    color = re.compile(
 | 
			
		||||
        r"^<(.+?)>"          # capture the color in `<color>`
 | 
			
		||||
    colour = (
 | 
			
		||||
        r"^<(.+?)>"          # capture the colour in `<colour>`
 | 
			
		||||
        r"\((.+?)"           # capture text in `(text`
 | 
			
		||||
        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):
 | 
			
		||||
    """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 `<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>'
 | 
			
		||||
    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 <br> 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."""
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user