diff --git a/src/sulguk/entities/__init__.py b/src/sulguk/entities/__init__.py index c37ee72..cff8766 100644 --- a/src/sulguk/entities/__init__.py +++ b/src/sulguk/entities/__init__.py @@ -10,6 +10,7 @@ "Spoiler", "Code", "Uppercase", + "Emoji", "Quote", "Blockquote", "Paragraph", @@ -38,6 +39,7 @@ Underline, Uppercase, ) +from .emoji import Emoji from .list import ListGroup, ListItem from .no_contents import HorizontalLine, NewLine from .progress import Progress diff --git a/src/sulguk/entities/base.py b/src/sulguk/entities/base.py index d72a7ab..a39cebe 100644 --- a/src/sulguk/entities/base.py +++ b/src/sulguk/entities/base.py @@ -1,6 +1,6 @@ from abc import ABC, abstractmethod from dataclasses import dataclass, field -from typing import List +from typing import List, Optional from sulguk.data import MessageEntity from sulguk.render import State @@ -36,12 +36,12 @@ def render(self, state: State) -> None: @dataclass class DecoratedEntity(Group): @abstractmethod - def _get_entity(self, offset: int, length: int) -> MessageEntity: + def _get_entity(self, offset: int, length: int) -> Optional[MessageEntity]: raise NotImplementedError def render(self, state: State) -> None: offset = state.canvas.size super().render(state) - state.entities.append( - self._get_entity(offset, state.canvas.size - offset), - ) + entity = self._get_entity(offset, state.canvas.size - offset) + if entity: + state.entities.append(entity) diff --git a/src/sulguk/entities/emoji.py b/src/sulguk/entities/emoji.py new file mode 100644 index 0000000..2e7b27d --- /dev/null +++ b/src/sulguk/entities/emoji.py @@ -0,0 +1,16 @@ +from dataclasses import dataclass + +from sulguk.data import MessageEntity +from .base import DecoratedEntity + + +@dataclass +class Emoji(DecoratedEntity): + custom_emoji_id: str = "" + + def _get_entity(self, offset: int, length: int) -> MessageEntity: + return MessageEntity( + type="custom_emoji", + offset=offset, length=length, + custom_emoji_id=self.custom_emoji_id, + ) diff --git a/src/sulguk/transformer.py b/src/sulguk/transformer.py index c1e7245..4cf4ff4 100644 --- a/src/sulguk/transformer.py +++ b/src/sulguk/transformer.py @@ -6,6 +6,7 @@ Blockquote, Bold, Code, + Emoji, Entity, Group, HorizontalLine, @@ -185,6 +186,13 @@ def _get_meter(self, attrs: Attrs) -> Entity: is_meter=True, ) + def _get_tg_emoji(self, attrs: Attrs) -> Entity: + emoji_id = self._find_attr("emoji-id", attrs) + if emoji_id: + return Emoji(custom_emoji_id=emoji_id) + else: + return Group() + def handle_startendtag(self, tag: str, attrs: Attrs) -> None: if tag == "br": entity = NewLine() @@ -240,6 +248,8 @@ def handle_starttag( nested = entity = Group() elif tag in ("tg-spoiler",): nested = entity = Spoiler() + elif tag in ("tg-emoji",): + nested = entity = self._get_tg_emoji(attrs) elif tag in ("p",): nested = entity = Paragraph() elif tag in ("u", "ins"):