mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Supports language in message entity pre
This commit is contained in:
parent
eeb3b63b97
commit
0a1909f0f4
2 changed files with 18 additions and 1 deletions
|
|
@ -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="<i>{value}</i>",
|
||||
code="<code>{value}</code>",
|
||||
pre="<pre>{value}</pre>",
|
||||
pre_language='<pre><code class="language-{language}">{value}</code></pre>',
|
||||
underline="<u>{value}</u>",
|
||||
strikethrough="<s>{value}</s>",
|
||||
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),
|
||||
|
|
|
|||
|
|
@ -20,6 +20,11 @@ class TestTextDecoration:
|
|||
[html_decoration, MessageEntity(type="italic", offset=0, length=5), "<i>test</i>"],
|
||||
[html_decoration, MessageEntity(type="code", offset=0, length=5), "<code>test</code>"],
|
||||
[html_decoration, MessageEntity(type="pre", offset=0, length=5), "<pre>test</pre>"],
|
||||
[
|
||||
html_decoration,
|
||||
MessageEntity(type="pre", offset=0, length=5, language="python"),
|
||||
'<pre><code class="language-python">test</code></pre>',
|
||||
],
|
||||
[html_decoration, MessageEntity(type="underline", offset=0, length=5), "<u>test</u>"],
|
||||
[
|
||||
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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue