Add name mention formatting to HTML and Markdown (#1019)

This commit is contained in:
painor 2018-10-04 14:56:32 +01:00 committed by Lonami
parent 2468b32fc5
commit 340f5614b5
2 changed files with 19 additions and 10 deletions

View File

@ -9,7 +9,7 @@ from html.parser import HTMLParser
from ..tl.types import (
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
MessageEntityPre, MessageEntityEmail, MessageEntityUrl,
MessageEntityTextUrl
MessageEntityTextUrl, MessageEntityMentionName
)
@ -178,6 +178,9 @@ def unparse(text, entities):
elif entity_type == MessageEntityTextUrl:
html.append('<a href="{}">{}</a>'
.format(escape(entity.url), entity_text))
elif entity_type == MessageEntityMentionName:
html.append('<a href="tg://user?id={}">{}</a>'
.format(entity.user_id, entity_text))
else:
skip_entity = True
last_offset = entity.offset + (0 if skip_entity else entity.length)

View File

@ -9,7 +9,7 @@ from ..helpers import add_surrogate, del_surrogate
from ..tl import TLObject
from ..tl.types import (
MessageEntityBold, MessageEntityItalic, MessageEntityCode,
MessageEntityPre, MessageEntityTextUrl
MessageEntityPre, MessageEntityTextUrl, MessageEntityMentionName
)
DEFAULT_DELIMITERS = {
@ -161,10 +161,16 @@ def unparse(text, entities, delimiters=None, url_fmt=None):
delimiter = delimiters.get(type(entity), None)
if delimiter:
text = text[:s] + delimiter + text[s:e] + delimiter + text[e:]
elif isinstance(entity, MessageEntityTextUrl) and url_fmt:
elif url_fmt:
url = None
if isinstance(entity, MessageEntityTextUrl):
url = entity.url
elif isinstance(entity, MessageEntityMentionName):
url = 'tg://user?id={}'.format(entity.user_id)
if url:
text = (
text[:s] +
add_surrogate(url_fmt.format(text[s:e], entity.url)) +
add_surrogate(url_fmt.format(text[s:e], url)) +
text[e:]
)