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:
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*&gt;.*)", r'<span class="greentext">\1</span>', text
)
if not outgoing:
text = re.sub(
r"(^\s*&gt;.*)", r'<span class="greentext">\1</span>', 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*&gt;.+)(!?<(?:br|p)/?>)",
r'<\1><span class="greentext">\2</span>\3',
text
)
if not outgoing:
text = re.sub(
r"<(p|br/?)>(\s*&gt;.*)(!?</?(?:br|p)/?>)",
r'<\1><span class="greentext">\2</span>\3',
text
)
return text

View File

@ -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"<p>{text}</p>"):
if to_html not in (html.escape(text), f"<p>{html.escape(text)}</p>"):
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,
)

View File

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

View File

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