feat: add spoiler entities

This commit is contained in:
Oleg A 2021-12-30 16:45:33 +03:00
parent e0ecbc4ec9
commit 1e86ec0643
3 changed files with 48 additions and 6 deletions

View file

@ -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

View file

@ -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),
("||", "||"),
("<b>", "</b>"),
("<i>", "</i>"),
("<code>", "</code>"),
@ -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)

View file

@ -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"<i>{value}</i>"
def spoiler(self, value: str) -> str:
return f'<span class="tg-spoiler">{value}</span>'
def code(self, value: str) -> str:
return f"<code>{value}</code>"
@ -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}`"