From 843d40ca2678f9d87d23b32aad960efa6fb787d3 Mon Sep 17 00:00:00 2001 From: miruka Date: Sun, 21 Jul 2019 17:41:43 -0400 Subject: [PATCH] Fix > parsing and local echo --- src/python/html_filter.py | 34 +++++++++++++++----------- src/python/matrix_client.py | 10 +++++--- src/qml/Chat/Timeline/EventContent.qml | 6 ++--- src/qml/Theme.qml | 5 +++- 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/python/html_filter.py b/src/python/html_filter.py index f43b65b5..061eac08 100644 --- a/src/python/html_filter.py +++ b/src/python/html_filter.py @@ -43,23 +43,26 @@ class HtmlFilter: ] - def from_markdown(self, text: str) -> str: - return self.filter(self._markdown_to_html(text)) + def from_markdown(self, text: str, outgoing: bool = False) -> str: + return self.filter(self._markdown_to_html(text), outgoing) - def from_markdown_inline(self, text: str) -> str: - return self.filter_inline(self._markdown_to_html(text)) + def from_markdown_inline(self, text: str, outgoing: bool = False) -> str: + return self.filter_inline(self._markdown_to_html(text), outgoing) - def filter_inline(self, html: str) -> str: + def filter_inline(self, html: str, outgoing: bool = False) -> str: text = self._inline_sanitizer.sanitize(html).strip("\n") - text = re.sub( - r"(^\s*>.*)", r'\1', text - ) + + if not outgoing: + text = re.sub( + r"(^\s*>.*)", r'\1', text + ) + return text - def filter(self, html: str) -> str: + def filter(self, html: str, outgoing: bool = False) -> str: html = self._sanitizer.sanitize(html) tree = etree.fromstring(html, parser=etree.HTMLParser()) @@ -76,11 +79,14 @@ class HtmlFilter: for el in tree[0].iterchildren())) text = str(result, "utf-8").strip("\n") - text = re.sub( - r"<(p|br/?)>(\s*>.+)(!?<(?:br|p)/?>)", - r'<\1>\2\3', - text - ) + + if not outgoing: + text = re.sub( + r"<(p|br/?)>(\s*>.*)(!?)", + r'<\1>\2\3', + text + ) + return text diff --git a/src/python/matrix_client.py b/src/python/matrix_client.py index fbb6b937..56813795 100644 --- a/src/python/matrix_client.py +++ b/src/python/matrix_client.py @@ -162,13 +162,15 @@ class MatrixClient(nio.AsyncClient): event_type = nio.RoomMessageEmote text = text[len("/me "): ] content = {"body": text, "msgtype": "m.emote"} - to_html = HTML_FILTER.from_markdown_inline(text) + to_html = HTML_FILTER.from_markdown_inline(text, outgoing=True) + echo_html = HTML_FILTER.from_markdown_inline(text) else: event_type = nio.RoomMessageText content = {"body": text, "msgtype": "m.text"} - to_html = HTML_FILTER.from_markdown(text) + to_html = HTML_FILTER.from_markdown(text, outgoing=True) + echo_html = HTML_FILTER.from_markdown(text) - if to_html not in (text, f"

{text}

"): + if to_html not in (html.escape(text), f"

{html.escape(text)}

"): content["format"] = "org.matrix.custom.html" content["formatted_body"] = to_html @@ -178,7 +180,7 @@ class MatrixClient(nio.AsyncClient): event_id = f"local_echo.{uuid4()}", sender_id = self.user_id, date = datetime.now(), - content = to_html, + content = echo_html, is_local_echo = True, ) diff --git a/src/qml/Chat/Timeline/EventContent.qml b/src/qml/Chat/Timeline/EventContent.qml index 76a4b029..c3883f1a 100644 --- a/src/qml/Chat/Timeline/EventContent.qml +++ b/src/qml/Chat/Timeline/EventContent.qml @@ -66,10 +66,8 @@ Row { id: contentLabel width: parent.width - Component.onCompleted: print(text) - text: '' + + Component.onCompleted: print(text, "\n") + text: theme.chat.message.styleInclude + Utils.processedEventText(model) + // time "  \n' + styleSheet + '\n\n' } property QtObject daybreak: QtObject {