Reinstate outbound formatting

This commit is contained in:
Andrew Ferrazzutti 2022-04-12 00:56:06 -04:00
parent b2f9298817
commit af296510aa
1 changed files with 30 additions and 3 deletions

View File

@ -19,6 +19,7 @@ from typing import NamedTuple
from mautrix.appservice import IntentAPI from mautrix.appservice import IntentAPI
from mautrix.types import Format, MessageEventContent, RelationType, RoomID, UserID from mautrix.types import Format, MessageEventContent, RelationType, RoomID, UserID
from mautrix.util import utf16_surrogate
from mautrix.util.formatter import ( from mautrix.util.formatter import (
EntityString, EntityString,
EntityType, EntityType,
@ -46,6 +47,7 @@ class SendParams(NamedTuple):
class KakaoTalkFormatString(EntityString[SimpleEntity, EntityType], MarkdownString): class KakaoTalkFormatString(EntityString[SimpleEntity, EntityType], MarkdownString):
def format(self, entity_type: EntityType, **kwargs) -> KakaoTalkFormatString: def format(self, entity_type: EntityType, **kwargs) -> KakaoTalkFormatString:
prefix = suffix = ""
if entity_type == EntityType.USER_MENTION: if entity_type == EntityType.USER_MENTION:
self.entities.append( self.entities.append(
SimpleEntity( SimpleEntity(
@ -55,7 +57,32 @@ class KakaoTalkFormatString(EntityString[SimpleEntity, EntityType], MarkdownStri
extra_info={"user_id": kwargs["user_id"]}, 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 return self
@ -126,8 +153,8 @@ async def matrix_to_kakaotalk(
else: else:
reply_to = None reply_to = None
if content.get("format", None) == Format.HTML and content["formatted_body"] and content.msgtype.is_text: if content.get("format", None) == Format.HTML and content["formatted_body"] and content.msgtype.is_text:
parsed = await ToKakaoTalkParser().parse(content["formatted_body"]) parsed = await ToKakaoTalkParser().parse(utf16_surrogate.add(content["formatted_body"]))
text = parsed.text text = utf16_surrogate.remove(parsed.text)
mentions_by_user: dict[Long, MentionStruct] = {} mentions_by_user: dict[Long, MentionStruct] = {}
# Make sure to not create remote mentions for any remote user not in the room # Make sure to not create remote mentions for any remote user not in the room
if parsed.entities: if parsed.entities: