From 24bdda8ca12b025d14fb63044a86b40d1febfda7 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 28 Sep 2018 18:39:57 +0300 Subject: [PATCH] Reorganize formatter utils and add more blue text --- mautrix_telegram/formatter/from_telegram.py | 9 +-- mautrix_telegram/formatter/util.py | 71 +++++++++++---------- 2 files changed, 42 insertions(+), 38 deletions(-) diff --git a/mautrix_telegram/formatter/from_telegram.py b/mautrix_telegram/formatter/from_telegram.py index baee7ff1..86cd5b5e 100644 --- a/mautrix_telegram/formatter/from_telegram.py +++ b/mautrix_telegram/formatter/from_telegram.py @@ -22,8 +22,9 @@ import re from telethon.tl.types import (MessageEntityMention, MessageEntityMentionName, MessageEntityUrl, MessageEntityEmail, MessageEntityTextUrl, MessageEntityBold, MessageEntityItalic, MessageEntityCode, MessageEntityPre, - MessageEntityBotCommand, Message, PeerChannel, MessageEntityHashtag, - TypeMessageEntity, MessageFwdHeader, PeerUser) + MessageEntityBotCommand, MessageEntityHashtag, MessageEntityCashtag, + MessageEntityPhone, TypeMessageEntity, Message, PeerChannel, + MessageFwdHeader, PeerUser) from mautrix_appservice import MatrixRequestError from mautrix_appservice.intent_api import IntentAPI @@ -252,12 +253,12 @@ def _telegram_entities_to_matrix(text: str, entities: List[TypeMessageEntity]) - skip_entity = _parse_name_mention(html, entity_text, TelegramID(entity.user_id)) elif entity_type == MessageEntityEmail: html.append(f"{entity_text}") - elif entity_type in {MessageEntityTextUrl, MessageEntityUrl}: + elif entity_type in (MessageEntityTextUrl, MessageEntityUrl): skip_entity = _parse_url(html, entity_text, entity.url if entity_type == MessageEntityTextUrl else None) elif entity_type == MessageEntityBotCommand: html.append(f"!{entity_text[1:]}") - elif entity_type == MessageEntityHashtag: + elif entity_type in (MessageEntityHashtag, MessageEntityCashtag, MessageEntityPhone): html.append(f"{entity_text}") else: skip_entity = True diff --git a/mautrix_telegram/formatter/util.py b/mautrix_telegram/formatter/util.py index 2a296614..70c19ebb 100644 --- a/mautrix_telegram/formatter/util.py +++ b/mautrix_telegram/formatter/util.py @@ -20,40 +20,6 @@ import struct import re -# add_surrogates and remove_surrogates are unicode surrogate utility functions from Telethon. -# Licensed under the MIT license. -# https://github.com/LonamiWebs/Telethon/blob/master/telethon/extensions/markdown.py -def add_surrogates(text: Optional[str]) -> Optional[str]: - if text is None: - return None - return "".join("".join(chr(y) for y in struct.unpack(" Optional[str]: - if text is None: - return None - return text.encode("utf-16", "surrogatepass").decode("utf-16") - - -def trim_reply_fallback_text(text: str) -> str: - if not text.startswith("> ") or "\n" not in text: - return text - lines = text.split("\n") - while len(lines) > 0 and lines[0].startswith("> "): - lines.pop(0) - return "\n".join(lines) - - -html_reply_fallback_regex = re.compile("^" - r"[\s\S]+?" - "") # type: Pattern - - -def trim_reply_fallback_html(html: str) -> str: - return html_reply_fallback_regex.sub("", html) - - def unicode_to_html(text: str, html: str, ctrl: str, tag: str) -> str: if ctrl not in text: return html @@ -84,3 +50,40 @@ def unicode_to_html(text: str, html: str, ctrl: str, tag: str) -> str: def html_to_unicode(text: str, ctrl: str) -> str: return ctrl.join(text) + ctrl + + +# add_surrogates and remove_surrogates are unicode surrogate utility functions from Telethon. +# Licensed under the MIT license. +# https://github.com/LonamiWebs/Telethon/blob/7cce7aa3e4c6c7019a55530391b1761d33e5a04e/telethon/helpers.py +def add_surrogates(text: Optional[str]) -> Optional[str]: + if text is None: + return None + return "".join("".join(chr(y) for y in struct.unpack(" Optional[str]: + if text is None: + return None + return text.encode("utf-16", "surrogatepass").decode("utf-16") + + +# trim_reply_fallback_text, html_reply_fallback_regex and trim_reply_fallback_html are Matrix +# reply fallback utility functions. +# You may copy and use them under any OSI-approved license. +def trim_reply_fallback_text(text: str) -> str: + if not text.startswith("> ") or "\n" not in text: + return text + lines = text.split("\n") + while len(lines) > 0 and lines[0].startswith("> "): + lines.pop(0) + return "\n".join(lines) + + +html_reply_fallback_regex = re.compile("^" + r"[\s\S]+?" + "") # type: Pattern + + +def trim_reply_fallback_html(html: str) -> str: + return html_reply_fallback_regex.sub("", html)