Fix > parsing and local echo

This commit is contained in:
miruka 2019-07-21 17:41:43 -04:00
parent 459880903d
commit 843d40ca26
4 changed files with 32 additions and 23 deletions

View File

@ -43,23 +43,26 @@ class HtmlFilter:
] ]
def from_markdown(self, text: str) -> str: def from_markdown(self, text: str, outgoing: bool = False) -> str:
return self.filter(self._markdown_to_html(text)) return self.filter(self._markdown_to_html(text), outgoing)
def from_markdown_inline(self, text: str) -> str: def from_markdown_inline(self, text: str, outgoing: bool = False) -> str:
return self.filter_inline(self._markdown_to_html(text)) 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 = self._inline_sanitizer.sanitize(html).strip("\n")
if not outgoing:
text = re.sub( text = re.sub(
r"(^\s*&gt;.*)", r'<span class="greentext">\1</span>', text r"(^\s*&gt;.*)", r'<span class="greentext">\1</span>', text
) )
return text return text
def filter(self, html: str) -> str: def filter(self, html: str, outgoing: bool = False) -> str:
html = self._sanitizer.sanitize(html) html = self._sanitizer.sanitize(html)
tree = etree.fromstring(html, parser=etree.HTMLParser()) tree = etree.fromstring(html, parser=etree.HTMLParser())
@ -76,11 +79,14 @@ class HtmlFilter:
for el in tree[0].iterchildren())) for el in tree[0].iterchildren()))
text = str(result, "utf-8").strip("\n") text = str(result, "utf-8").strip("\n")
if not outgoing:
text = re.sub( text = re.sub(
r"<(p|br/?)>(\s*&gt;.+)(!?<(?:br|p)/?>)", r"<(p|br/?)>(\s*&gt;.*)(!?</?(?:br|p)/?>)",
r'<\1><span class="greentext">\2</span>\3', r'<\1><span class="greentext">\2</span>\3',
text text
) )
return text return text

View File

@ -162,13 +162,15 @@ class MatrixClient(nio.AsyncClient):
event_type = nio.RoomMessageEmote event_type = nio.RoomMessageEmote
text = text[len("/me "): ] text = text[len("/me "): ]
content = {"body": text, "msgtype": "m.emote"} 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: else:
event_type = nio.RoomMessageText event_type = nio.RoomMessageText
content = {"body": text, "msgtype": "m.text"} 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"<p>{text}</p>"): if to_html not in (html.escape(text), f"<p>{html.escape(text)}</p>"):
content["format"] = "org.matrix.custom.html" content["format"] = "org.matrix.custom.html"
content["formatted_body"] = to_html content["formatted_body"] = to_html
@ -178,7 +180,7 @@ class MatrixClient(nio.AsyncClient):
event_id = f"local_echo.{uuid4()}", event_id = f"local_echo.{uuid4()}",
sender_id = self.user_id, sender_id = self.user_id,
date = datetime.now(), date = datetime.now(),
content = to_html, content = echo_html,
is_local_echo = True, is_local_echo = True,
) )

View File

@ -66,10 +66,8 @@ Row {
id: contentLabel id: contentLabel
width: parent.width width: parent.width
Component.onCompleted: print(text) Component.onCompleted: print(text, "\n")
text: '<style type="text/css">' + text: theme.chat.message.styleInclude +
theme.chat.message.stylesheet +
'</style>' +
Utils.processedEventText(model) + Utils.processedEventText(model) +
// time // time
"&nbsp;&nbsp;<font size=" + theme.fontSize.small + "&nbsp;&nbsp;<font size=" + theme.fontSize.small +

View File

@ -120,8 +120,11 @@ QtObject {
property color greenTextColor: Ut.hsl(80, 60, 25) property color greenTextColor: Ut.hsl(80, 60, 25)
property string stylesheet: property string styleSheet:
".greentext { color: " + greenTextColor + " }" ".greentext { color: " + greenTextColor + " }"
property string styleInclude:
'<style type"text/css">\n' + styleSheet + '\n</style>\n'
} }
property QtObject daybreak: QtObject { property QtObject daybreak: QtObject {