Skip to content

Commit

Permalink
Merge pull request #23 from Tishka17/feature/custom_emoji
Browse files Browse the repository at this point in the history
Custom emoji
  • Loading branch information
Tishka17 authored Oct 2, 2023
2 parents 4e7592f + 92e6682 commit 0bcbe82
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/sulguk/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"Spoiler",
"Code",
"Uppercase",
"Emoji",
"Quote",
"Blockquote",
"Paragraph",
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/sulguk/entities/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
16 changes: 16 additions & 0 deletions src/sulguk/entities/emoji.py
Original file line number Diff line number Diff line change
@@ -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,
)
10 changes: 10 additions & 0 deletions src/sulguk/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Blockquote,
Bold,
Code,
Emoji,
Entity,
Group,
HorizontalLine,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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"):
Expand Down

0 comments on commit 0bcbe82

Please sign in to comment.