mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Add better support for nested entities (both for HTML and Markdown) (#297)
* Added better support for nested entities, both for HTML and Markdown * Tiny style fix * Make use of pre-defined constants
This commit is contained in:
parent
82e0087def
commit
2aefbfd531
@ -147,43 +147,38 @@ class HTML:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def unparse(text: str, entities: list):
|
def unparse(text: str, entities: list):
|
||||||
text = utils.add_surrogates(text)
|
text = utils.add_surrogates(text)
|
||||||
copy = text
|
|
||||||
|
entities_offsets = []
|
||||||
|
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
|
entity_type = entity.type
|
||||||
start = entity.offset
|
start = entity.offset
|
||||||
end = start + entity.length
|
end = start + entity.length
|
||||||
|
|
||||||
type = entity.type
|
if entity_type in ("bold", "italic", "underline", "strike"):
|
||||||
|
start_tag = "<{}>".format(entity_type[0])
|
||||||
url = entity.url
|
end_tag = "</{}>".format(entity_type[0])
|
||||||
user = entity.user
|
elif entity_type in ("code", "pre", "blockquote"):
|
||||||
|
start_tag = "<{}>".format(entity_type)
|
||||||
sub = copy[start:end]
|
end_tag = "</{}>".format(entity_type)
|
||||||
|
elif entity_type == "text_link":
|
||||||
if type == "bold":
|
url = entity.url
|
||||||
style = "b"
|
start_tag = '<a href="{}">'.format(url)
|
||||||
elif type == "italic":
|
end_tag = "</a>"
|
||||||
style = "i"
|
elif entity_type == "text_mention":
|
||||||
elif type == "underline":
|
user = entity.user
|
||||||
style = "u"
|
start_tag = '<a href="tg://user?id={}">'.format(user.id)
|
||||||
elif type == "strike":
|
end_tag = "</a>"
|
||||||
style = "s"
|
|
||||||
elif type == "code":
|
|
||||||
style = "code"
|
|
||||||
elif type == "pre":
|
|
||||||
style = "pre"
|
|
||||||
elif type == "blockquote":
|
|
||||||
style = "blockquote"
|
|
||||||
elif type == "text_link":
|
|
||||||
text = text[:start] + text[start:].replace(sub, '<a href="{}">{}</a>'.format(url, sub), 1)
|
|
||||||
continue
|
|
||||||
elif type == "text_mention":
|
|
||||||
text = text[:start] + text[start:].replace(
|
|
||||||
sub, '<a href="tg://user?id={}">{}</a>'.format(user.id, sub), 1)
|
|
||||||
continue
|
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
text = text[:start] + text[start:].replace(sub, "<{0}>{1}</{0}>".format(style, sub), 1)
|
entities_offsets.append((start_tag, start,))
|
||||||
|
entities_offsets.append((end_tag, end,))
|
||||||
|
|
||||||
|
# sorting by offset (desc)
|
||||||
|
entities_offsets.sort(key=lambda x: -x[1])
|
||||||
|
|
||||||
|
for entity, offset in entities_offsets:
|
||||||
|
text = text[:offset] + entity + text[offset:]
|
||||||
|
|
||||||
return utils.remove_surrogates(text)
|
return utils.remove_surrogates(text)
|
||||||
|
@ -107,44 +107,44 @@ class Markdown:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def unparse(text: str, entities: list):
|
def unparse(text: str, entities: list):
|
||||||
text = utils.add_surrogates(text)
|
text = utils.add_surrogates(text)
|
||||||
copy = text
|
|
||||||
|
entities_offsets = []
|
||||||
|
|
||||||
for entity in entities:
|
for entity in entities:
|
||||||
|
entity_type = entity.type
|
||||||
start = entity.offset
|
start = entity.offset
|
||||||
end = start + entity.length
|
end = start + entity.length
|
||||||
|
|
||||||
type = entity.type
|
if entity_type == "bold":
|
||||||
|
start_tag = end_tag = BOLD_DELIM
|
||||||
url = entity.url
|
elif entity_type == "italic":
|
||||||
user = entity.user
|
start_tag = end_tag = ITALIC_DELIM
|
||||||
|
elif entity_type == "underline":
|
||||||
sub = copy[start:end]
|
start_tag = end_tag = UNDERLINE_DELIM
|
||||||
|
elif entity_type == "strike":
|
||||||
if type == "bold":
|
start_tag = end_tag = STRIKE_DELIM
|
||||||
style = BOLD_DELIM
|
elif entity_type == "code":
|
||||||
elif type == "italic":
|
start_tag = end_tag = CODE_DELIM
|
||||||
style = ITALIC_DELIM
|
elif entity_type in ("pre", "blockquote"):
|
||||||
elif type == "underline":
|
start_tag = end_tag = PRE_DELIM
|
||||||
style = UNDERLINE_DELIM
|
elif entity_type == "text_link":
|
||||||
elif type == "strike":
|
url = entity.url
|
||||||
style = STRIKE_DELIM
|
start_tag = "["
|
||||||
elif type == "code":
|
end_tag = "]({})".format(url)
|
||||||
style = CODE_DELIM
|
elif entity_type == "text_mention":
|
||||||
elif type == "pre":
|
user = entity.user
|
||||||
style = PRE_DELIM
|
start_tag = "["
|
||||||
# TODO: Blockquote for MD
|
end_tag = "](tg://user?id={})".format(user.id)
|
||||||
# elif type == "blockquote":
|
|
||||||
# style = ...
|
|
||||||
elif type == "text_link":
|
|
||||||
text = text[:start] + text[start:].replace(sub, '[{1}]({0})'.format(url, sub), 1)
|
|
||||||
continue
|
|
||||||
elif type == "text_mention":
|
|
||||||
text = text[:start] + text[start:].replace(
|
|
||||||
sub, '[{1}](tg://user?id={0})'.format(user.id, sub), 1)
|
|
||||||
continue
|
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
text = text[:start] + text[start:].replace(sub, "{0}{1}{0}".format(style, sub), 1)
|
entities_offsets.append((start_tag, start,))
|
||||||
|
entities_offsets.append((end_tag, end,))
|
||||||
|
|
||||||
|
# sorting by offset (desc)
|
||||||
|
entities_offsets.sort(key=lambda x: -x[1])
|
||||||
|
|
||||||
|
for entity, offset in entities_offsets:
|
||||||
|
text = text[:offset] + entity + text[offset:]
|
||||||
|
|
||||||
return utils.remove_surrogates(text)
|
return utils.remove_surrogates(text)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user