Fix > parsing and local echo
This commit is contained in:
		@@ -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")
 | 
				
			||||||
        text = re.sub(
 | 
					
 | 
				
			||||||
            r"(^\s*>.*)", r'<span class="greentext">\1</span>', text
 | 
					        if not outgoing:
 | 
				
			||||||
        )
 | 
					            text = re.sub(
 | 
				
			||||||
 | 
					                r"(^\s*>.*)", 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")
 | 
				
			||||||
        text = re.sub(
 | 
					
 | 
				
			||||||
            r"<(p|br/?)>(\s*>.+)(!?<(?:br|p)/?>)",
 | 
					        if not outgoing:
 | 
				
			||||||
            r'<\1><span class="greentext">\2</span>\3',
 | 
					            text = re.sub(
 | 
				
			||||||
            text
 | 
					                r"<(p|br/?)>(\s*>.*)(!?</?(?:br|p)/?>)",
 | 
				
			||||||
        )
 | 
					                r'<\1><span class="greentext">\2</span>\3',
 | 
				
			||||||
 | 
					                text
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return text
 | 
					        return text
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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,
 | 
				
			||||||
        )
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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
 | 
				
			||||||
                      "  <font size=" + theme.fontSize.small +
 | 
					                      "  <font size=" + theme.fontSize.small +
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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 {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user