2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 13:27:47 +00:00

Fix HTML and Markdown unparsing

This commit is contained in:
Dan 2022-04-25 08:30:26 +02:00
parent a93e21831f
commit 8a2416665b
2 changed files with 23 additions and 17 deletions

View File

@ -24,6 +24,7 @@ from typing import Optional
import pyrogram import pyrogram
from pyrogram import raw from pyrogram import raw
from pyrogram.enums import MessageEntityType
from pyrogram.errors import PeerIdInvalid from pyrogram.errors import PeerIdInvalid
from . import utils from . import utils
@ -155,17 +156,21 @@ class HTML:
start = entity.offset start = entity.offset
end = start + entity.length end = start + entity.length
if entity_type in ("bold", "italic", "underline", "strikethrough"): if entity_type in (MessageEntityType.BOLD, MessageEntityType.ITALIC, MessageEntityType.UNDERLINE,
start_tag = f"<{entity_type[0]}>" MessageEntityType.STRIKETHROUGH):
end_tag = f"</{entity_type[0]}>" name = entity_type.name[0].lower()
elif entity_type in ("code", "pre", "blockquote", "spoiler"): start_tag = f"<{name}>"
start_tag = f"<{entity_type}>" end_tag = f"</{name}>"
end_tag = f"</{entity_type}>" elif entity_type in (MessageEntityType.CODE, MessageEntityType.PRE, MessageEntityType.BLOCKQUOTE,
elif entity_type == "text_link": MessageEntityType.SPOILER):
name = entity_type.name.lower()
start_tag = f"<{name}>"
end_tag = f"</{name}>"
elif entity_type == MessageEntityType.TEXT_LINK:
url = entity.url url = entity.url
start_tag = f'<a href="{url}">' start_tag = f'<a href="{url}">'
end_tag = "</a>" end_tag = "</a>"
elif entity_type == "text_mention": elif entity_type == MessageEntityType.TEXT_MENTION:
user = entity.user user = entity.user
start_tag = f'<a href="tg://user?id={user.id}">' start_tag = f'<a href="tg://user?id={user.id}">'
end_tag = "</a>" end_tag = "</a>"

View File

@ -21,6 +21,7 @@ import re
from typing import Optional from typing import Optional
import pyrogram import pyrogram
from pyrogram.enums import MessageEntityType
from . import utils from . import utils
from .html import HTML from .html import HTML
@ -119,25 +120,25 @@ class Markdown:
start = entity.offset start = entity.offset
end = start + entity.length end = start + entity.length
if entity_type == "bold": if entity_type == MessageEntityType.BOLD:
start_tag = end_tag = BOLD_DELIM start_tag = end_tag = BOLD_DELIM
elif entity_type == "italic": elif entity_type == MessageEntityType.ITALIC:
start_tag = end_tag = ITALIC_DELIM start_tag = end_tag = ITALIC_DELIM
elif entity_type == "underline": elif entity_type == MessageEntityType.UNDERLINE:
start_tag = end_tag = UNDERLINE_DELIM start_tag = end_tag = UNDERLINE_DELIM
elif entity_type == "strikethrough": elif entity_type == MessageEntityType.STRIKETHROUGH:
start_tag = end_tag = STRIKE_DELIM start_tag = end_tag = STRIKE_DELIM
elif entity_type == "code": elif entity_type == MessageEntityType.CODE:
start_tag = end_tag = CODE_DELIM start_tag = end_tag = CODE_DELIM
elif entity_type in ("pre", "blockquote"): elif entity_type in (MessageEntityType.PRE, MessageEntityType.BLOCKQUOTE):
start_tag = end_tag = PRE_DELIM start_tag = end_tag = PRE_DELIM
elif entity_type == "spoiler": elif entity_type == MessageEntityType.SPOILER:
start_tag = end_tag = SPOILER_DELIM start_tag = end_tag = SPOILER_DELIM
elif entity_type == "text_link": elif entity_type == MessageEntityType.TEXT_LINK:
url = entity.url url = entity.url
start_tag = "[" start_tag = "["
end_tag = f"]({url})" end_tag = f"]({url})"
elif entity_type == "text_mention": elif entity_type == MessageEntityType.TEXT_MENTION:
user = entity.user user = entity.user
start_tag = "[" start_tag = "["
end_tag = f"](tg://user?id={user.id})" end_tag = f"](tg://user?id={user.id})"