aiogram/aiogram/utils/text_decorations.py
Andrew df7b16d5b3
Some checks failed
Tests / tests (macos-latest, 3.10) (push) Has been cancelled
Tests / tests (macos-latest, 3.11) (push) Has been cancelled
Tests / tests (macos-latest, 3.12) (push) Has been cancelled
Tests / tests (macos-latest, 3.13) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.10) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.11) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.12) (push) Has been cancelled
Tests / tests (ubuntu-latest, 3.13) (push) Has been cancelled
Tests / tests (windows-latest, 3.10) (push) Has been cancelled
Tests / tests (windows-latest, 3.11) (push) Has been cancelled
Tests / tests (windows-latest, 3.12) (push) Has been cancelled
Tests / tests (windows-latest, 3.13) (push) Has been cancelled
Tests / pypy-tests (macos-latest, pypy3.10) (push) Has been cancelled
Tests / pypy-tests (macos-latest, pypy3.11) (push) Has been cancelled
Tests / pypy-tests (ubuntu-latest, pypy3.10) (push) Has been cancelled
Tests / pypy-tests (ubuntu-latest, pypy3.11) (push) Has been cancelled
EOL of Py3.9 (#1726)
* Drop py3.9 and pypy3.9

Add pypy3.11 (testing) into `tests.yml`

Remove py3.9 from matrix in `tests.yml`

Refactor not auto-gen code to be compatible with py3.10+, droping ugly 3.9 annotation.

Replace some `from typing` imports to `from collections.abc`, due to deprecation

Add `from __future__ import annotations` and `if TYPE_CHECKING:` where possible

Add some `noqa` to calm down Ruff in some places, if Ruff will be used as default linting+formatting tool in future

Replace some relative imports to absolute

Sort `__all__` tuples in `__init__.py` and some other `.py` files

Sort `__slots__` tuples in classes

Split raises into `msg` and `raise` (`EM101`, `EM102`) to not duplicate error message in the traceback

Add `Self` from `typing_extenstion` where possible

Resolve typing problem in `aiogram/filters/command.py:18`

Concatenate nested `if` statements

Convert `HandlerContainer` into a dataclass in `aiogram/fsm/scene.py`

Bump tests docker-compose.yml `redis:6-alpine` -> `redis:8-alpine`

Bump tests docker-compose.yml `mongo:7.0.6` -> `mongo:8.0.14`

Bump pre-commit-config `black==24.4.2` -> `black==25.9.0`

Bump pre-commit-config `ruff==0.5.1` -> `ruff==0.13.3`

Update Makefile lint for ruff to show fixes

Add `make outdated` into Makefile

Use `pathlib` instead of `os.path`

Bump `redis[hiredis]>=5.0.1,<5.3.0` -> `redis[hiredis]>=6.2.0,<7`

Bump `cryptography>=43.0.0` -> `cryptography>=46.0.0` due to security reasons

Bump `pytz~=2023.3` -> `pytz~=2025.2`

Bump `pycryptodomex~=3.19.0` -> `pycryptodomex~=3.23.0` due to security reasons

Bump linting and formatting tools

* Add `1726.removal.rst`

* Update aiogram/utils/dataclass.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update aiogram/filters/callback_data.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update 1726.removal.rst

* Remove `outdated` from Makefile

* Add `__slots__` to `HandlerContainer`

* Remove unused imports

* Add `@dataclass` with `slots=True` to `HandlerContainer`

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-10-06 19:19:23 +03:00

277 lines
8.1 KiB
Python

from __future__ import annotations
import html
import re
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING, cast
from aiogram.enums import MessageEntityType
if TYPE_CHECKING:
from collections.abc import Generator
from re import Pattern
from aiogram.types import MessageEntity
__all__ = (
"HtmlDecoration",
"MarkdownDecoration",
"TextDecoration",
"add_surrogates",
"html_decoration",
"markdown_decoration",
"remove_surrogates",
)
def add_surrogates(text: str) -> bytes:
return text.encode("utf-16-le")
def remove_surrogates(text: bytes) -> str:
return text.decode("utf-16-le")
class TextDecoration(ABC):
def apply_entity(self, entity: MessageEntity, text: str) -> str:
"""
Apply single entity to text
:param entity:
:param text:
:return:
"""
if entity.type in {
MessageEntityType.BOT_COMMAND,
MessageEntityType.URL,
MessageEntityType.MENTION,
MessageEntityType.PHONE_NUMBER,
MessageEntityType.HASHTAG,
MessageEntityType.CASHTAG,
MessageEntityType.EMAIL,
}:
# These entities should not be changed
return text
if entity.type in {
MessageEntityType.BOLD,
MessageEntityType.ITALIC,
MessageEntityType.CODE,
MessageEntityType.UNDERLINE,
MessageEntityType.STRIKETHROUGH,
MessageEntityType.SPOILER,
MessageEntityType.BLOCKQUOTE,
MessageEntityType.EXPANDABLE_BLOCKQUOTE,
}:
return cast(str, getattr(self, entity.type)(value=text))
if entity.type == MessageEntityType.PRE:
return (
self.pre_language(value=text, language=entity.language)
if entity.language
else self.pre(value=text)
)
if entity.type == MessageEntityType.TEXT_MENTION:
from aiogram.types import User
user = cast(User, entity.user)
return self.link(value=text, link=f"tg://user?id={user.id}")
if entity.type == MessageEntityType.TEXT_LINK:
return self.link(value=text, link=cast(str, entity.url))
if entity.type == MessageEntityType.CUSTOM_EMOJI:
return self.custom_emoji(value=text, custom_emoji_id=cast(str, entity.custom_emoji_id))
# This case is not possible because of `if` above, but if any new entity is added to
# API it will be here too
return self.quote(text)
def unparse(self, text: str, entities: list[MessageEntity] | None = None) -> str:
"""
Unparse message entities
:param text: raw text
:param entities: Array of MessageEntities
:return:
"""
return "".join(
self._unparse_entities(
add_surrogates(text),
sorted(entities, key=lambda item: item.offset) if entities else [],
),
)
def _unparse_entities(
self,
text: bytes,
entities: list[MessageEntity],
offset: int | None = None,
length: int | None = None,
) -> Generator[str, None, None]:
if offset is None:
offset = 0
length = length or len(text)
for index, entity in enumerate(entities):
if entity.offset * 2 < offset:
continue
if entity.offset * 2 > offset:
yield self.quote(remove_surrogates(text[offset : entity.offset * 2]))
start = entity.offset * 2
offset = entity.offset * 2 + entity.length * 2
sub_entities = list(
filter(lambda e: e.offset * 2 < (offset or 0), entities[index + 1 :]),
)
yield self.apply_entity(
entity,
"".join(self._unparse_entities(text, sub_entities, offset=start, length=offset)),
)
if offset < length:
yield self.quote(remove_surrogates(text[offset:length]))
@abstractmethod
def link(self, value: str, link: str) -> str:
pass
@abstractmethod
def bold(self, value: str) -> str:
pass
@abstractmethod
def italic(self, value: str) -> str:
pass
@abstractmethod
def code(self, value: str) -> str:
pass
@abstractmethod
def pre(self, value: str) -> str:
pass
@abstractmethod
def pre_language(self, value: str, language: str) -> str:
pass
@abstractmethod
def underline(self, value: str) -> str:
pass
@abstractmethod
def strikethrough(self, value: str) -> str:
pass
@abstractmethod
def spoiler(self, value: str) -> str:
pass
@abstractmethod
def quote(self, value: str) -> str:
pass
@abstractmethod
def custom_emoji(self, value: str, custom_emoji_id: str) -> str:
pass
@abstractmethod
def blockquote(self, value: str) -> str:
pass
@abstractmethod
def expandable_blockquote(self, value: str) -> str:
pass
class HtmlDecoration(TextDecoration):
BOLD_TAG = "b"
ITALIC_TAG = "i"
UNDERLINE_TAG = "u"
STRIKETHROUGH_TAG = "s"
SPOILER_TAG = "tg-spoiler"
EMOJI_TAG = "tg-emoji"
BLOCKQUOTE_TAG = "blockquote"
def link(self, value: str, link: str) -> str:
return f'<a href="{link}">{value}</a>'
def bold(self, value: str) -> str:
return f"<{self.BOLD_TAG}>{value}</{self.BOLD_TAG}>"
def italic(self, value: str) -> str:
return f"<{self.ITALIC_TAG}>{value}</{self.ITALIC_TAG}>"
def code(self, value: str) -> str:
return f"<code>{value}</code>"
def pre(self, value: str) -> str:
return f"<pre>{value}</pre>"
def pre_language(self, value: str, language: str) -> str:
return f'<pre><code class="language-{language}">{value}</code></pre>'
def underline(self, value: str) -> str:
return f"<{self.UNDERLINE_TAG}>{value}</{self.UNDERLINE_TAG}>"
def strikethrough(self, value: str) -> str:
return f"<{self.STRIKETHROUGH_TAG}>{value}</{self.STRIKETHROUGH_TAG}>"
def spoiler(self, value: str) -> str:
return f"<{self.SPOILER_TAG}>{value}</{self.SPOILER_TAG}>"
def quote(self, value: str) -> str:
return html.escape(value, quote=False)
def custom_emoji(self, value: str, custom_emoji_id: str) -> str:
return f'<{self.EMOJI_TAG} emoji-id="{custom_emoji_id}">{value}</{self.EMOJI_TAG}>'
def blockquote(self, value: str) -> str:
return f"<{self.BLOCKQUOTE_TAG}>{value}</{self.BLOCKQUOTE_TAG}>"
def expandable_blockquote(self, value: str) -> str:
return f"<{self.BLOCKQUOTE_TAG} expandable>{value}</{self.BLOCKQUOTE_TAG}>"
class MarkdownDecoration(TextDecoration):
MARKDOWN_QUOTE_PATTERN: Pattern[str] = re.compile(r"([_*\[\]()~`>#+\-=|{}.!\\])")
def link(self, value: str, link: str) -> str:
return f"[{value}]({link})"
def bold(self, value: str) -> str:
return f"*{value}*"
def italic(self, value: str) -> str:
return f"_\r{value}_\r"
def code(self, value: str) -> str:
return f"`{value}`"
def pre(self, value: str) -> str:
return f"```\n{value}\n```"
def pre_language(self, value: str, language: str) -> str:
return f"```{language}\n{value}\n```"
def underline(self, value: str) -> str:
return f"__\r{value}__\r"
def strikethrough(self, value: str) -> str:
return f"~{value}~"
def spoiler(self, value: str) -> str:
return f"||{value}||"
def quote(self, value: str) -> str:
return re.sub(pattern=self.MARKDOWN_QUOTE_PATTERN, repl=r"\\\1", string=value)
def custom_emoji(self, value: str, custom_emoji_id: str) -> str:
return f'!{self.link(value=value, link=f"tg://emoji?id={custom_emoji_id}")}'
def blockquote(self, value: str) -> str:
return "\n".join(f">{line}" for line in value.splitlines())
def expandable_blockquote(self, value: str) -> str:
return "\n".join(f">{line}" for line in value.splitlines()) + "||"
html_decoration = HtmlDecoration()
markdown_decoration = MarkdownDecoration()