diff --git a/pyrogram/parser/html.py b/pyrogram/parser/html.py index dee8a49d..af70c5cb 100644 --- a/pyrogram/parser/html.py +++ b/pyrogram/parser/html.py @@ -61,7 +61,7 @@ class Parser(HTMLParser): entity = raw.types.MessageEntityCode elif tag == "pre": entity = raw.types.MessageEntityPre - extra["language"] = "" + extra["language"] = attrs.get("language", "") elif tag == "spoiler": entity = raw.types.MessageEntitySpoiler elif tag == "a": @@ -172,9 +172,13 @@ class HTML: name = entity_type.name[0].lower() start_tag = f"<{name}>" end_tag = f"" + elif entity_type == MessageEntityType.PRE: + name = entity_type.name.lower() + language = getattr(entity, "language", "") or "" + start_tag = f'<{name} language="{language}">' if language else f"<{name}>" + end_tag = f"" elif entity_type in ( MessageEntityType.CODE, - MessageEntityType.PRE, MessageEntityType.BLOCKQUOTE, MessageEntityType.SPOILER, ): diff --git a/pyrogram/parser/markdown.py b/pyrogram/parser/markdown.py index 36479340..6219d958 100644 --- a/pyrogram/parser/markdown.py +++ b/pyrogram/parser/markdown.py @@ -105,6 +105,12 @@ class Markdown: delims.remove(delim) tag = CLOSING_TAG.format(tag) + if delim == PRE_DELIM and delim in delims: + delim_and_language = text[text.find(PRE_DELIM):].split("\n")[0] + language = delim_and_language[len(PRE_DELIM):] + text = utils.replace_once(text, delim_and_language, f'
', start)
+                continue
+
             text = utils.replace_once(text, delim, tag, start)
 
         return await self.html.parse(text)
@@ -130,7 +136,11 @@ class Markdown:
                 start_tag = end_tag = STRIKE_DELIM
             elif entity_type == MessageEntityType.CODE:
                 start_tag = end_tag = CODE_DELIM
-            elif entity_type in (MessageEntityType.PRE, MessageEntityType.BLOCKQUOTE):
+            elif entity_type == MessageEntityType.PRE:
+                language = getattr(entity, "language", "") or ""
+                start_tag = f"{PRE_DELIM}{language}\n"
+                end_tag = f"\n{PRE_DELIM}"
+            elif entity_type == MessageEntityType.BLOCKQUOTE:
                 start_tag = end_tag = PRE_DELIM
             elif entity_type == MessageEntityType.SPOILER:
                 start_tag = end_tag = SPOILER_DELIM