Merge branch 'nicer-plaintext-links'
This commit is contained in:
commit
c614f8c8ca
1 changed files with 15 additions and 2 deletions
|
@ -20,11 +20,24 @@ import asyncio
|
||||||
import attr
|
import attr
|
||||||
|
|
||||||
from mautrix.client import Client as MatrixClient, SyncStream
|
from mautrix.client import Client as MatrixClient, SyncStream
|
||||||
from mautrix.util import markdown, formatter
|
from mautrix.util.formatter import MatrixParser, MarkdownString, EntityType
|
||||||
|
from mautrix.util import markdown
|
||||||
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
|
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
|
||||||
MessageType, TextMessageEventContent, Format, RelatesTo)
|
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]):
|
||||||
|
fs = HumanReadableString
|
||||||
|
|
||||||
|
|
||||||
def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True
|
def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True
|
||||||
) -> Tuple[str, str]:
|
) -> Tuple[str, str]:
|
||||||
if render_markdown:
|
if render_markdown:
|
||||||
|
@ -33,7 +46,7 @@ def parse_formatted(message: str, allow_html: bool = False, render_markdown: boo
|
||||||
html = message
|
html = message
|
||||||
else:
|
else:
|
||||||
return message, escape(message)
|
return message, escape(message)
|
||||||
return formatter.parse_html(html), html
|
return MaubotHTMLParser.parse(html).text, html
|
||||||
|
|
||||||
|
|
||||||
class MaubotMessageEvent(MessageEvent):
|
class MaubotMessageEvent(MessageEvent):
|
||||||
|
|
Loading…
Reference in a new issue