diff --git a/aiogram/utils/text_decorations.py b/aiogram/utils/text_decorations.py index 5b9502b7..125547e8 100644 --- a/aiogram/utils/text_decorations.py +++ b/aiogram/utils/text_decorations.py @@ -22,6 +22,7 @@ class TextDecoration: italic: str code: str pre: str + pre_language: str underline: str strikethrough: str quote: Callable[[AnyStr], AnyStr] @@ -34,8 +35,12 @@ class TextDecoration: :param text: :return: """ - if entity.type in ("bold", "italic", "code", "pre", "underline", "strikethrough"): + if entity.type in ("bold", "italic", "code", "underline", "strikethrough"): return getattr(self, entity.type).format(value=text) + if entity.type == "pre": + return (self.pre_language if entity.language else self.pre).format( + value=text, language=entity.language + ) elif entity.type == "text_mention": return self.link.format(value=text, link=f"tg://user?id={entity.user.id}") elif entity.type == "text_link": @@ -94,6 +99,7 @@ html_decoration = TextDecoration( italic="{value}", code="{value}", pre="
{value}
", + pre_language='
{value}
', underline="{value}", strikethrough="{value}", quote=html.escape, @@ -107,6 +113,7 @@ markdown_decoration = TextDecoration( italic="_{value}_\r", code="`{value}`", pre="```{value}```", + pre_language="```{language}\n{value}\n```", underline="__{value}__", strikethrough="~{value}~", quote=lambda text: re.sub(pattern=MARKDOWN_QUOTE_PATTERN, repl=r"\\\1", string=text), diff --git a/tests/test_utils/test_text_decorations.py b/tests/test_utils/test_text_decorations.py index babe0a58..ad822c8f 100644 --- a/tests/test_utils/test_text_decorations.py +++ b/tests/test_utils/test_text_decorations.py @@ -20,6 +20,11 @@ class TestTextDecoration: [html_decoration, MessageEntity(type="italic", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="code", offset=0, length=5), "test"], [html_decoration, MessageEntity(type="pre", offset=0, length=5), "
test
"], + [ + html_decoration, + MessageEntity(type="pre", offset=0, length=5, language="python"), + '
test
', + ], [html_decoration, MessageEntity(type="underline", offset=0, length=5), "test"], [ html_decoration, @@ -51,6 +56,11 @@ class TestTextDecoration: [markdown_decoration, MessageEntity(type="italic", offset=0, length=5), "_test_\r"], [markdown_decoration, MessageEntity(type="code", offset=0, length=5), "`test`"], [markdown_decoration, MessageEntity(type="pre", offset=0, length=5), "```test```"], + [ + markdown_decoration, + MessageEntity(type="pre", offset=0, length=5, language="python"), + "```python\ntest\n```", + ], [markdown_decoration, MessageEntity(type="underline", offset=0, length=5), "__test__"], [ markdown_decoration,