mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Add markdown unparse method
This commit is contained in:
parent
d16219e187
commit
b334b06117
@ -36,7 +36,7 @@ class Markdown:
|
|||||||
CODE_DELIMITER = "`"
|
CODE_DELIMITER = "`"
|
||||||
PRE_DELIMITER = "```"
|
PRE_DELIMITER = "```"
|
||||||
|
|
||||||
MARKDOWN_RE = re.compile(r"```([\w ]*)\n([\w\W]*)(?:\n|)```|\[(.+?)\]\((.+?)\)|({d})(.+?)\5".format(
|
MARKDOWN_RE = re.compile(r"```([\w\W]*)(?:\n|)```|\[(.+?)\]\((.+?)\)|({d})(.+?)\4".format(
|
||||||
d="|".join(
|
d="|".join(
|
||||||
["".join(i) for i in [
|
["".join(i) for i in [
|
||||||
["\{}".format(j) for j in i]
|
["\{}".format(j) for j in i]
|
||||||
@ -61,12 +61,12 @@ class Markdown:
|
|||||||
|
|
||||||
for match in self.MARKDOWN_RE.finditer(message):
|
for match in self.MARKDOWN_RE.finditer(message):
|
||||||
start = match.start() - offset
|
start = match.start() - offset
|
||||||
lang, pre, text, url, style, body = match.groups()
|
pre, text, url, style, body = match.groups()
|
||||||
|
|
||||||
if pre:
|
if pre:
|
||||||
body = pre = pre.strip()
|
body = pre
|
||||||
entity = Pre(start, len(pre), lang.strip() or "")
|
entity = Pre(start, len(pre), "")
|
||||||
offset += len(lang) + len(self.PRE_DELIMITER) * 2
|
offset += len(self.PRE_DELIMITER) * 2
|
||||||
elif url:
|
elif url:
|
||||||
mention = self.MENTION_RE.match(url)
|
mention = self.MENTION_RE.match(url)
|
||||||
|
|
||||||
@ -91,8 +91,6 @@ class Markdown:
|
|||||||
entity = Italic(start, len(body))
|
entity = Italic(start, len(body))
|
||||||
elif style == self.CODE_DELIMITER:
|
elif style == self.CODE_DELIMITER:
|
||||||
entity = Code(start, len(body))
|
entity = Code(start, len(body))
|
||||||
elif style == self.PRE_DELIMITER:
|
|
||||||
entity = Pre(start, len(body), "")
|
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@ -105,3 +103,43 @@ class Markdown:
|
|||||||
message=utils.remove_surrogates(message),
|
message=utils.remove_surrogates(message),
|
||||||
entities=entities
|
entities=entities
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def unparse(self, message: str, entities: list):
|
||||||
|
offset = 0
|
||||||
|
|
||||||
|
for entity in entities:
|
||||||
|
start = entity.offset + offset
|
||||||
|
type = entity.type
|
||||||
|
url = entity.url
|
||||||
|
user = entity.user
|
||||||
|
sub = message[start: start + entity.length]
|
||||||
|
|
||||||
|
if type == "bold":
|
||||||
|
style = self.BOLD_DELIMITER
|
||||||
|
elif type == "italic":
|
||||||
|
style = self.ITALIC_DELIMITER
|
||||||
|
elif type == "code":
|
||||||
|
style = self.CODE_DELIMITER
|
||||||
|
elif type == "pre":
|
||||||
|
style = self.PRE_DELIMITER
|
||||||
|
elif type == "text_link":
|
||||||
|
message = message[:start] + message[start:].replace(
|
||||||
|
sub, "[{}]({})".format(sub, url), 1
|
||||||
|
)
|
||||||
|
offset += 4 + len(url)
|
||||||
|
continue
|
||||||
|
elif type == "text_mention":
|
||||||
|
message = message[:start] + message[start:].replace(
|
||||||
|
sub, "[{}](tg://user?id={})".format(sub, user.id), 1
|
||||||
|
)
|
||||||
|
offset += 17 + len(str(user.id))
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
|
message = message[:start] + message[start:].replace(
|
||||||
|
sub, "{0}{1}{0}".format(style, sub), 1
|
||||||
|
)
|
||||||
|
offset += len(style) * 2
|
||||||
|
|
||||||
|
return message
|
||||||
|
Loading…
x
Reference in New Issue
Block a user