Add languages to "pre" tags (HTML and Markdown) #1118

* added `language` to entities when unparsing (both markdown and html)

* added `language` to entities also when parsing (html only)

* Update html.py

* Update markdown.py

* Update markdown.py

* Update markdown.py

Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com>
This commit is contained in:
Andrea Princic 2022-10-15 18:19:38 +02:00 committed by GitHub
parent bf52ec1e94
commit 9ade92c855
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -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"</{name}>"
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"</{name}>"
elif entity_type in (
MessageEntityType.CODE,
MessageEntityType.PRE,
MessageEntityType.BLOCKQUOTE,
MessageEntityType.SPOILER,
):

View File

@ -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'<pre language="{language}">', 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