From f7b9137a6852e6f8c20903d2eabf0b08a62fb092 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 30 Dec 2021 09:14:49 +0100 Subject: [PATCH] Add support for "spoiler" MessageEntity --- docs/source/topics/text-formatting.rst | 7 +++++++ pyrogram/parser/html.py | 4 +++- pyrogram/parser/markdown.py | 8 +++++++- pyrogram/types/messages_and_media/message_entity.py | 3 +++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/docs/source/topics/text-formatting.rst b/docs/source/topics/text-formatting.rst index 5d9a9376..bada964b 100644 --- a/docs/source/topics/text-formatting.rst +++ b/docs/source/topics/text-formatting.rst @@ -34,6 +34,7 @@ list of the basic styles currently supported by Pyrogram. - *italic* - :strike:`strike` - :underline:`underline` +- spoiler - `text URL `_ - `user text mention `_ - ``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: strike, strike, strike + spoiler + text URL inline mention @@ -136,6 +142,7 @@ The following tags are currently supported: "italic, " "underline, " "strike, " + "spoiler, " "mention, " "URL, " "code\n\n" diff --git a/pyrogram/parser/html.py b/pyrogram/parser/html.py index f3f55195..805ca021 100644 --- a/pyrogram/parser/html.py +++ b/pyrogram/parser/html.py @@ -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"" elif entity_type in ("code", "pre", "blockquote"): diff --git a/pyrogram/parser/markdown.py b/pyrogram/parser/markdown.py index f71503d8..3652c4c4 100644 --- a/pyrogram/parser/markdown.py +++ b/pyrogram/parser/markdown.py @@ -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 = "[" diff --git a/pyrogram/types/messages_and_media/message_entity.py b/pyrogram/types/messages_and_media/message_entity.py index a0fc86a9..9a211fde 100644 --- a/pyrogram/types/messages_and_media/message_entity.py +++ b/pyrogram/types/messages_and_media/message_entity.py @@ -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.