MTPyroger/pyrogram/client/style/markdown.py

144 lines
4.9 KiB
Python
Raw Normal View History

2017-12-05 11:42:57 +00:00
# Pyrogram - Telegram MTProto API Client Library for Python
2019-01-01 11:36:16 +00:00
# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance>
2017-12-05 11:42:57 +00:00
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import re
from collections import OrderedDict
2017-12-05 11:42:57 +00:00
from pyrogram.api.types import (
MessageEntityBold as Bold,
MessageEntityItalic as Italic,
MessageEntityCode as Code,
MessageEntityTextUrl as Url,
MessageEntityPre as Pre,
2018-02-15 10:24:56 +00:00
MessageEntityMentionName as MentionInvalid,
2017-12-13 09:44:24 +00:00
InputMessageEntityMentionName as Mention
2017-12-05 11:42:57 +00:00
)
from . import utils
2017-12-05 11:42:57 +00:00
class Markdown:
2018-02-15 10:24:56 +00:00
BOLD_DELIMITER = "**"
ITALIC_DELIMITER = "__"
CODE_DELIMITER = "`"
PRE_DELIMITER = "```"
2017-12-05 11:42:57 +00:00
MARKDOWN_RE = re.compile(r"({d})([\w\W]*?)\1|\[([^[]+?)\]\(([^(]+?)\)".format(
2017-12-05 11:42:57 +00:00
d="|".join(
["".join(i) for i in [
["\{}".format(j) for j in i]
2018-02-15 10:24:56 +00:00
for i in [
PRE_DELIMITER,
CODE_DELIMITER,
ITALIC_DELIMITER,
BOLD_DELIMITER
]
2017-12-05 11:42:57 +00:00
]]
)
2018-02-15 10:24:56 +00:00
))
MENTION_RE = re.compile(r"tg://user\?id=(\d+)")
2017-12-05 11:42:57 +00:00
def __init__(self, peers_by_id: dict = None):
self.peers_by_id = peers_by_id or {}
2017-12-13 09:44:24 +00:00
2018-02-15 10:24:56 +00:00
def parse(self, message: str):
message = utils.add_surrogates(str(message or "")).strip()
entities = []
2017-12-05 11:42:57 +00:00
offset = 0
2018-02-15 10:24:56 +00:00
for match in self.MARKDOWN_RE.finditer(message):
2017-12-05 11:42:57 +00:00
start = match.start() - offset
style, body, text, url = match.groups()
2018-02-15 10:24:56 +00:00
if url:
2018-02-15 10:24:56 +00:00
mention = self.MENTION_RE.match(url)
if mention:
user_id = int(mention.group(1))
input_user = self.peers_by_id.get(user_id, None)
entity = (
Mention(offset=start, length=len(text), user_id=input_user)
2018-02-15 10:24:56 +00:00
if input_user
else MentionInvalid(offset=start, length=len(text), user_id=user_id)
2018-02-15 10:24:56 +00:00
)
else:
entity = Url(offset=start, length=len(text), url=url)
2018-02-15 10:24:56 +00:00
body = text
offset += len(url) + 4
else:
if style == self.BOLD_DELIMITER:
entity = Bold(offset=start, length=len(body))
2018-02-15 10:24:56 +00:00
elif style == self.ITALIC_DELIMITER:
entity = Italic(offset=start, length=len(body))
2018-02-15 10:24:56 +00:00
elif style == self.CODE_DELIMITER:
entity = Code(offset=start, length=len(body))
elif style == self.PRE_DELIMITER:
entity = Pre(offset=start, length=len(body), language="")
2018-02-15 10:24:56 +00:00
else:
2017-12-05 11:42:57 +00:00
continue
2018-02-15 10:24:56 +00:00
offset += len(style) * 2
2017-12-05 11:42:57 +00:00
entities.append(entity)
2018-02-15 10:24:56 +00:00
message = message.replace(match.group(), body)
2017-12-05 11:42:57 +00:00
# TODO: OrderedDict to be removed in Python3.6
return OrderedDict([
("message", utils.remove_surrogates(message)),
("entities", entities)
])
2018-05-10 12:46:14 +00:00
def unparse(self, message: str, entities: list):
message = utils.add_surrogates(message).strip()
2018-05-10 12:46:14 +00:00
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":
offset += 4 + len(url)
2018-05-10 13:07:03 +00:00
message = message[:start] + message[start:].replace(
sub, "[{}]({})".format(sub, url), 1)
2018-05-10 12:46:14 +00:00
continue
elif type == "text_mention":
offset += 17 + len(str(user.id))
2018-05-10 13:07:03 +00:00
message = message[:start] + message[start:].replace(
sub, "[{}](tg://user?id={})".format(sub, user.id), 1)
2018-05-10 12:46:14 +00:00
continue
else:
continue
offset += len(style) * 2
2018-05-10 13:07:03 +00:00
message = message[:start] + message[start:].replace(
sub, "{0}{1}{0}".format(style, sub), 1)
2018-05-10 12:46:14 +00:00
return utils.remove_surrogates(message)