MTPyroger/pyrogram/client/style/markdown.py

108 lines
3.6 KiB
Python
Raw Normal View History

2017-12-05 11:42:57 +00:00
# Pyrogram - Telegram MTProto API Client Library for Python
2018-01-01 12:21:23 +00:00
# Copyright (C) 2017-2018 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 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
2018-02-15 10:24:56 +00:00
MARKDOWN_RE = re.compile(r"```([\w ]*)\n([\w\W]*)(?:\n|)```|\[([^[(]+)\]\(([^])]+)\)|({d})(.+?)\5".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
2018-02-15 10:24:56 +00:00
def __init__(self, peers_by_id: dict):
2017-12-13 09:44:24 +00:00
self.peers_by_id = peers_by_id
2018-02-15 10:24:56 +00:00
def parse(self, message: str):
2017-12-05 11:42:57 +00:00
entities = []
2018-02-15 10:24:56 +00:00
message = utils.add_surrogates(message).strip()
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
2018-02-15 10:24:56 +00:00
lang, pre, text, url, style, body = match.groups()
if pre:
body = pre = pre.strip()
entity = Pre(start, len(pre), lang.strip() or "")
offset += len(lang) + len(self.PRE_DELIMITER) * 2
elif url:
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(start, len(text), input_user)
if input_user
else MentionInvalid(start, len(text), user_id)
)
else:
entity = Url(start, len(text), url)
body = text
offset += len(url) + 4
else:
if style == self.BOLD_DELIMITER:
entity = Bold(start, len(body))
elif style == self.ITALIC_DELIMITER:
entity = Italic(start, len(body))
elif style == self.CODE_DELIMITER:
entity = Code(start, len(body))
elif style == self.PRE_DELIMITER:
entity = Pre(start, len(body), "")
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
2017-12-07 01:02:16 +00:00
return dict(
2018-02-15 10:24:56 +00:00
message=utils.remove_surrogates(message),
2017-12-07 01:02:16 +00:00
entities=entities
)