From 1e86ec064353074a376f2735ef02d977c6160274 Mon Sep 17 00:00:00 2001 From: Oleg A Date: Thu, 30 Dec 2021 16:45:33 +0300 Subject: [PATCH] feat: add spoiler entities --- aiogram/types/message_entity.py | 13 +++++++++---- aiogram/utils/markdown.py | 27 +++++++++++++++++++++++++++ aiogram/utils/text_decorations.py | 14 ++++++++++++-- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/aiogram/types/message_entity.py b/aiogram/types/message_entity.py index b2aaf425..9788af05 100644 --- a/aiogram/types/message_entity.py +++ b/aiogram/types/message_entity.py @@ -77,6 +77,9 @@ class MessageEntity(base.TelegramObject): if self.type == MessageEntityType.ITALIC: method = markdown.hitalic if as_html else markdown.italic return method(entity_text) + if self.type == MessageEntityType.SPOILER: + method = markdown.spoiler if as_html else markdown.hspoiler + return method(entity_text) if self.type == MessageEntityType.PRE: method = markdown.hpre if as_html else markdown.pre return method(entity_text) @@ -108,10 +111,11 @@ class MessageEntityType(helper.Helper): :key: PHONE_NUMBER :key: BOLD :key: ITALIC - :key: CODE - :key: PRE :key: UNDERLINE :key: STRIKETHROUGH + :key: SPOILER + :key: CODE + :key: PRE :key: TEXT_LINK :key: TEXT_MENTION """ @@ -127,9 +131,10 @@ class MessageEntityType(helper.Helper): PHONE_NUMBER = helper.Item() # phone_number BOLD = helper.Item() # bold - bold text ITALIC = helper.Item() # italic - italic text - CODE = helper.Item() # code - monowidth string - PRE = helper.Item() # pre - monowidth block UNDERLINE = helper.Item() # underline STRIKETHROUGH = helper.Item() # strikethrough + SPOILER = helper.Item() # spoiler + CODE = helper.Item() # code - monowidth string + PRE = helper.Item() # pre - monowidth block TEXT_LINK = helper.Item() # text_link - for clickable text URLs TEXT_MENTION = helper.Item() # text_mention - for users without usernames diff --git a/aiogram/utils/markdown.py b/aiogram/utils/markdown.py index 75f5fea0..3b50ffd4 100644 --- a/aiogram/utils/markdown.py +++ b/aiogram/utils/markdown.py @@ -7,6 +7,7 @@ MD_SYMBOLS = ( (LIST_MD_SYMBOLS[1], LIST_MD_SYMBOLS[1]), (LIST_MD_SYMBOLS[2], LIST_MD_SYMBOLS[2]), (LIST_MD_SYMBOLS[2] * 3 + "\n", "\n" + LIST_MD_SYMBOLS[2] * 3), + ("||", "||"), ("", ""), ("", ""), ("", ""), @@ -113,6 +114,32 @@ def hitalic(*content, sep=" ") -> str: ) +def spoiler(*content, sep=" ") -> str: + """ + Make spoiler text (Markdown) + + :param content: + :param sep: + :return: + """ + return markdown_decoration.spoiler( + value=markdown_decoration.quote(_join(*content, sep=sep)) + ) + + +def hspoiler(*content, sep=" ") -> str: + """ + Make spoiler text (HTML) + + :param content: + :param sep: + :return: + """ + return html_decoration.spoiler( + value=html_decoration.quote(_join(*content, sep=sep)) + ) + + def code(*content, sep=" ") -> str: """ Make mono-width text (Markdown) diff --git a/aiogram/utils/text_decorations.py b/aiogram/utils/text_decorations.py index 40fe296b..ae9af7d4 100644 --- a/aiogram/utils/text_decorations.py +++ b/aiogram/utils/text_decorations.py @@ -27,9 +27,9 @@ class TextDecoration(ABC): :return: """ if entity.type in {"bot_command", "url", "mention", "phone_number"}: - # This entities should not be changed + # These entities should not be changed return text - if entity.type in {"bold", "italic", "code", "underline", "strikethrough"}: + if entity.type in {"bold", "italic", "spoiler", "code", "underline", "strikethrough"}: return cast(str, getattr(self, entity.type)(value=text)) if entity.type == "pre": return ( @@ -115,6 +115,10 @@ class TextDecoration(ABC): def italic(self, value: str) -> str: # pragma: no cover pass + @abstractmethod + def spoiler(self, value: str) -> str: # pragma: no cover + pass + @abstractmethod def code(self, value: str) -> str: # pragma: no cover pass @@ -150,6 +154,9 @@ class HtmlDecoration(TextDecoration): def italic(self, value: str) -> str: return f"{value}" + def spoiler(self, value: str) -> str: + return f'{value}' + def code(self, value: str) -> str: return f"{value}" @@ -181,6 +188,9 @@ class MarkdownDecoration(TextDecoration): def italic(self, value: str) -> str: return f"_\r{value}_\r" + def spoiler(self, value: str) -> str: + return f"||{value}||" + def code(self, value: str) -> str: return f"`{value}`"