From 341b3db99ec92c14f45d7569a3a0c955a0bd5b39 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 8 May 2020 11:49:56 +0300 Subject: [PATCH 1/2] Make parse_formatted output links in a nicer way than markdown --- maubot/matrix.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/maubot/matrix.py b/maubot/matrix.py index 1821a27..2864465 100644 --- a/maubot/matrix.py +++ b/maubot/matrix.py @@ -18,12 +18,24 @@ from html import escape import attr from mautrix.client import Client as MatrixClient, SyncStream -from mautrix.util.formatter import parse_html +from mautrix.util.formatter import MatrixParser, MarkdownString, EntityType from mautrix.util import markdown from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent, MessageType, TextMessageEventContent, Format, RelatesTo) +class HumanReadableString(MarkdownString): + def format(self, entity_type: EntityType, **kwargs) -> 'MarkdownString': + if entity_type == EntityType.URL and kwargs['url'] != self.text: + self.text = f"{self.text} ({kwargs['url']})" + return self + return super(HumanReadableString, self).format(entity_type, **kwargs) + + +class MaubotHTMLParser(MatrixParser[HumanReadableString]): + e = HumanReadableString + + def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True ) -> Tuple[str, str]: if render_markdown: @@ -32,7 +44,7 @@ def parse_formatted(message: str, allow_html: bool = False, render_markdown: boo html = message else: return message, escape(message) - return parse_html(html), html + return MaubotHTMLParser.parse(html).text, html class MaubotMessageEvent(MessageEvent): From 6960382aeb66e4fce4bae1222c2c2c5461d81155 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 8 May 2020 12:37:05 +0300 Subject: [PATCH 2/2] Fix mistake --- maubot/matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maubot/matrix.py b/maubot/matrix.py index 2864465..ef10e69 100644 --- a/maubot/matrix.py +++ b/maubot/matrix.py @@ -33,7 +33,7 @@ class HumanReadableString(MarkdownString): class MaubotHTMLParser(MatrixParser[HumanReadableString]): - e = HumanReadableString + fs = HumanReadableString def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True