Add support for "spoiler" MessageEntity

This commit is contained in:
Dan 2021-12-30 09:14:49 +01:00
parent 42c690757d
commit f7b9137a68
4 changed files with 20 additions and 2 deletions

View File

@ -34,6 +34,7 @@ list of the basic styles currently supported by Pyrogram.
- *italic*
- :strike:`strike`
- :underline:`underline`
- spoiler
- `text URL <https://pyrogram.org>`_
- `user text mention <https://t.me/haskell>`_
- ``inline fixed-width code``
@ -63,6 +64,8 @@ To strictly use this mode, pass "markdown" to the *parse_mode* parameter when us
~~strike~~
##spoiler##
[text URL](https://docs.pyrogram.org/)
[text user mention](tg://user?id=23122162)
@ -86,6 +89,7 @@ To strictly use this mode, pass "markdown" to the *parse_mode* parameter when us
"__italic__, "
"--underline--, "
"~~strike~~, "
"##spoiler##, "
"[mention](tg://user?id=23122162), "
"[URL](https://pyrogram.org), "
"`code`, "
@ -113,6 +117,8 @@ The following tags are currently supported:
<s>strike</s>, <del>strike</del>, <strike>strike</strike>
<spoiler>spoiler</spoiler>
<a href="http://docs.pyrogram.org/">text URL</a>
<a href="tg://user?id=23122162">inline mention</a>
@ -136,6 +142,7 @@ The following tags are currently supported:
"<i>italic</i>, "
"<u>underline</u>, "
"<s>strike</s>, "
"<spoiler>spoiler</spoiler>, "
"<a href=\"tg://user?id=23122162\">mention</a>, "
"<a href=\"https://pyrogram.org/\">URL</a>, "
"<code>code</code>\n\n"

View File

@ -61,6 +61,8 @@ class Parser(HTMLParser):
elif tag == "pre":
entity = raw.types.MessageEntityPre
extra["language"] = ""
elif tag == "spoiler":
entity = raw.types.MessageEntitySpoiler
elif tag == "a":
url = attrs.get("href", "")
@ -153,7 +155,7 @@ class HTML:
start = entity.offset
end = start + entity.length
if entity_type in ("bold", "italic", "underline", "strikethrough"):
if entity_type in ("bold", "italic", "underline", "strikethrough", "spoiler"):
start_tag = f"<{entity_type[0]}>"
end_tag = f"</{entity_type[0]}>"
elif entity_type in ("code", "pre", "blockquote"):

View File

@ -28,6 +28,7 @@ BOLD_DELIM = "**"
ITALIC_DELIM = "__"
UNDERLINE_DELIM = "--"
STRIKE_DELIM = "~~"
SPOILER_DELIM = "##"
CODE_DELIM = "`"
PRE_DELIM = "```"
@ -41,7 +42,8 @@ MARKDOWN_RE = re.compile(r"({d})|\[(.+?)\]\((.+?)\)".format(
STRIKE_DELIM,
UNDERLINE_DELIM,
ITALIC_DELIM,
BOLD_DELIM
BOLD_DELIM,
SPOILER_DELIM
]
]]
)))
@ -90,6 +92,8 @@ class Markdown:
tag = "code"
elif delim == PRE_DELIM:
tag = "pre"
elif delim == SPOILER_DELIM:
tag = "spoiler"
else:
continue
@ -127,6 +131,8 @@ class Markdown:
start_tag = end_tag = CODE_DELIM
elif entity_type in ("pre", "blockquote"):
start_tag = end_tag = PRE_DELIM
elif entity_type == "spoiler":
start_tag = end_tag = SPOILER_DELIM
elif entity_type == "text_link":
url = entity.url
start_tag = "["

View File

@ -42,6 +42,7 @@ class MessageEntityType(AutoName):
ITALIC = auto()
UNDERLINE = auto()
STRIKETHROUGH = auto()
SPOILER = auto()
CODE = auto()
PRE = auto()
TEXT_LINK = auto()
@ -62,6 +63,7 @@ RAW_ENTITIES_TO_TYPE = {
raw.types.MessageEntityPre: MessageEntityType.PRE,
raw.types.MessageEntityUnderline: MessageEntityType.UNDERLINE,
raw.types.MessageEntityStrike: MessageEntityType.STRIKETHROUGH,
raw.types.MessageEntitySpoiler: MessageEntityType.SPOILER,
raw.types.MessageEntityBlockquote: MessageEntityType.BLOCKQUOTE,
raw.types.MessageEntityTextUrl: MessageEntityType.TEXT_LINK,
raw.types.MessageEntityMentionName: MessageEntityType.TEXT_MENTION,
@ -90,6 +92,7 @@ class MessageEntity(Object):
- "italic": *italic text*.
- "underline": underlined text.
- "strikethrough": strikethrough text.
- "spoiler": spoiler text.
- "code": monowidth string.
- "pre": monowidth block (see *language* below).
- "text_link": for clickable text URLs.