From b0d77d74a9d5d64e2036c5e2639e6a57c49c82de Mon Sep 17 00:00:00 2001 From: miruka Date: Fri, 20 Dec 2019 14:44:31 -0400 Subject: [PATCH] Add custom markdown syntax for coloring text (text to color) where color can be a SVG color name, 3 characters hex or 6 characters hex code. Can be used to send colored message from the composer. Other clients that follow the matrix spec should be able to display them (e.g. riot, even if it can't send them) --- TODO.md | 1 - src/backend/html_markdown.py | 42 +++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/TODO.md b/TODO.md index 7a0e4319..66929b5f 100644 --- a/TODO.md +++ b/TODO.md @@ -92,7 +92,6 @@ - Way to open context menus without a right mouse button - `smartVerticalFlick()` gradual acceleration - Make banner buttons look better -- Way to color HTML from the composer - Choose a better default easing type for animations - Make HListView scrollbars visible diff --git a/src/backend/html_markdown.py b/src/backend/html_markdown.py index ee6758f6..968c04e4 100644 --- a/src/backend/html_markdown.py +++ b/src/backend/html_markdown.py @@ -16,17 +16,41 @@ class MarkdownInlineGrammar(mistune.InlineGrammar): Modifications: - Disable underscores for bold/italics (e.g. `__bold__`) + + - Add syntax for coloring text: `(text)`, + e.g. `(Lorem ipsum)` or `<#000040>(sit dolor amet...)` """ + escape = re.compile(r"^\\([\\`*{}\[\]()#+\-.!_<>~|])") # Add < emphasis = re.compile(r"^\*((?:\*\*|[^\*])+?)\*(?!\*)") double_emphasis = re.compile(r"^\*{2}([\s\S]+?)\*{2}(?!\*)") + # test string: r"(x) (x) \b>(x) b>(x) (\(z) (foo\)xyz)" + color = re.compile( + r"^<(.+?)>" # capture the color in `` + r"\((.+?)" # capture text in `(text` + r"(?`.""" + + return f'{text}' + + class HTMLProcessor: """Provide HTML filtering and conversion from Markdown. @@ -95,7 +132,10 @@ 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( - hard_wrap=True, escape=True, inline=MarkdownInlineLexer, + hard_wrap=True, + escape=True, + inline=MarkdownInlineLexer, + renderer=MarkdownRenderer(), ) self._markdown_to_html.block.default_rules = [