From af296510aa56ea05f40a42a7af0102408516c32d Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Tue, 12 Apr 2022 00:56:06 -0400 Subject: [PATCH] Reinstate outbound formatting --- .../formatter/from_matrix.py | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/matrix_appservice_kakaotalk/formatter/from_matrix.py b/matrix_appservice_kakaotalk/formatter/from_matrix.py index db87083..465c58d 100644 --- a/matrix_appservice_kakaotalk/formatter/from_matrix.py +++ b/matrix_appservice_kakaotalk/formatter/from_matrix.py @@ -19,6 +19,7 @@ from typing import NamedTuple from mautrix.appservice import IntentAPI from mautrix.types import Format, MessageEventContent, RelationType, RoomID, UserID +from mautrix.util import utf16_surrogate from mautrix.util.formatter import ( EntityString, EntityType, @@ -46,6 +47,7 @@ class SendParams(NamedTuple): class KakaoTalkFormatString(EntityString[SimpleEntity, EntityType], MarkdownString): def format(self, entity_type: EntityType, **kwargs) -> KakaoTalkFormatString: + prefix = suffix = "" if entity_type == EntityType.USER_MENTION: self.entities.append( SimpleEntity( @@ -55,7 +57,32 @@ class KakaoTalkFormatString(EntityString[SimpleEntity, EntityType], MarkdownStri extra_info={"user_id": kwargs["user_id"]}, ) ) - self.text = f"@{self.text}" + return self + elif entity_type == EntityType.BOLD: + prefix = suffix = "*" + elif entity_type == EntityType.ITALIC: + prefix = suffix = "_" + elif entity_type == EntityType.STRIKETHROUGH: + prefix = suffix = "~" + elif entity_type == EntityType.URL: + if kwargs["url"] != self.text: + suffix = f" ({kwargs['url']})" + elif entity_type == EntityType.PREFORMATTED: + prefix = f"```{kwargs['language']}\n" + suffix = "\n```" + elif entity_type == EntityType.INLINE_CODE: + prefix = suffix = "`" + elif entity_type == EntityType.BLOCKQUOTE: + children = self.trim().split("\n") + children = [child.prepend("> ") for child in children] + return self.join(children, "\n") + elif entity_type == EntityType.HEADER: + prefix = "#" * kwargs["size"] + " " + else: + return self + + self._offset_entities(len(prefix)) + self.text = f"{prefix}{self.text}{suffix}" return self @@ -126,8 +153,8 @@ async def matrix_to_kakaotalk( else: reply_to = None if content.get("format", None) == Format.HTML and content["formatted_body"] and content.msgtype.is_text: - parsed = await ToKakaoTalkParser().parse(content["formatted_body"]) - text = parsed.text + parsed = await ToKakaoTalkParser().parse(utf16_surrogate.add(content["formatted_body"])) + text = utf16_surrogate.remove(parsed.text) mentions_by_user: dict[Long, MentionStruct] = {} # Make sure to not create remote mentions for any remote user not in the room if parsed.entities: