2017-12-05 11:42:57 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
|
|
|
# Copyright (C) 2017 Dan Tès <https://github.com/delivrance>
|
|
|
|
#
|
|
|
|
# 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 struct import unpack
|
|
|
|
|
|
|
|
from pyrogram.api.types import (
|
|
|
|
MessageEntityBold as Bold,
|
|
|
|
MessageEntityItalic as Italic,
|
|
|
|
MessageEntityCode as Code,
|
|
|
|
MessageEntityTextUrl as Url,
|
|
|
|
MessageEntityPre as Pre,
|
2017-12-13 09:44:24 +00:00
|
|
|
InputMessageEntityMentionName as Mention
|
2017-12-05 11:42:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class Markdown:
|
|
|
|
INLINE_DELIMITERS = {
|
|
|
|
"**": Bold,
|
|
|
|
"__": Italic,
|
|
|
|
"`": Code
|
|
|
|
}
|
|
|
|
|
|
|
|
# SMP = Supplementary Multilingual Plane: https://en.wikipedia.org/wiki/Plane_(Unicode)#Overview
|
|
|
|
SMP_RE = re.compile(r"[\U00010000-\U0010FFFF]")
|
|
|
|
|
|
|
|
# ``` python
|
|
|
|
# for i in range(10):
|
|
|
|
# print(i)
|
|
|
|
# ```
|
|
|
|
PRE_RE = r"(?P<pre>```(?P<lang>.*)\n(?P<code>(.|\n)*)\n```)"
|
|
|
|
|
|
|
|
# [url](github.com)
|
2017-12-13 09:44:24 +00:00
|
|
|
URL_RE = r"(?P<url>(\[(?P<url_text>.+?)\]\((?P<url_path>.+?)\)))"
|
|
|
|
|
|
|
|
# [name](tg://user?id=123456789)
|
|
|
|
MENTION_RE = r"(?P<mention>(\[(?P<mention_text>.+?)\]\(tg:\/\/user\?id=(?P<user_id>\d+?)\)))"
|
2017-12-05 11:42:57 +00:00
|
|
|
|
|
|
|
# **bold**
|
|
|
|
# __italic__
|
|
|
|
# `code`
|
|
|
|
INLINE_RE = r"(?P<inline>(?P<start_delimiter>{d})(?P<body>.+?)(?P<end_delimiter>{d}))".format(
|
|
|
|
d="|".join(
|
|
|
|
["".join(i) for i in [
|
|
|
|
["\{}".format(j) for j in i]
|
|
|
|
for i in sorted( # Sort delimiters by length
|
|
|
|
INLINE_DELIMITERS.keys(),
|
|
|
|
key=lambda k: len(k), # Or: key=len
|
|
|
|
reverse=True
|
|
|
|
)
|
|
|
|
]]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2017-12-13 09:44:24 +00:00
|
|
|
MARKDOWN_RE = re.compile("|".join([PRE_RE, MENTION_RE, URL_RE, INLINE_RE]))
|
2017-12-05 11:42:57 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add_surrogates(cls, text):
|
|
|
|
# Replace each SMP code point with a surrogate pair
|
|
|
|
return cls.SMP_RE.sub(
|
|
|
|
lambda match: # Split SMP in two surrogates
|
2017-12-14 10:07:04 +00:00
|
|
|
"".join(chr(i) for i in unpack("<HH", match.group().encode("utf-16le"))),
|
|
|
|
text
|
2017-12-05 11:42:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def remove_surrogates(text):
|
|
|
|
# Replace each surrogate pair with a SMP code point
|
|
|
|
return text.encode("utf-16", "surrogatepass").decode("utf-16")
|
|
|
|
|
2017-12-13 09:44:24 +00:00
|
|
|
def __init__(self, peers_by_id):
|
|
|
|
self.peers_by_id = peers_by_id
|
|
|
|
|
|
|
|
def parse(self, text):
|
2017-12-05 11:42:57 +00:00
|
|
|
entities = []
|
2017-12-13 09:44:24 +00:00
|
|
|
text = self.add_surrogates(text)
|
2017-12-05 11:42:57 +00:00
|
|
|
offset = 0
|
|
|
|
|
2017-12-13 09:44:24 +00:00
|
|
|
for match in self.MARKDOWN_RE.finditer(text):
|
2017-12-05 11:42:57 +00:00
|
|
|
start = match.start() - offset
|
|
|
|
|
|
|
|
if match.group("pre"):
|
|
|
|
pattern = match.group("pre")
|
|
|
|
lang = match.group("lang")
|
|
|
|
replace = match.group("code")
|
|
|
|
entity = Pre(start, len(replace), lang.strip())
|
|
|
|
offset += len(lang) + 8
|
|
|
|
elif match.group("url"):
|
|
|
|
pattern = match.group("url")
|
2017-12-13 09:44:24 +00:00
|
|
|
replace = match.group("url_text")
|
|
|
|
path = match.group("url_path")
|
2017-12-05 11:42:57 +00:00
|
|
|
entity = Url(start, len(replace), path)
|
|
|
|
offset += len(path) + 4
|
2017-12-13 09:44:24 +00:00
|
|
|
elif match.group("mention"):
|
|
|
|
pattern = match.group("mention")
|
|
|
|
replace = match.group("mention_text")
|
|
|
|
user_id = match.group("user_id")
|
|
|
|
entity = Mention(start, len(replace), self.peers_by_id[int(user_id)])
|
|
|
|
offset += len(user_id) + 17
|
2017-12-05 11:42:57 +00:00
|
|
|
elif match.group("inline"):
|
|
|
|
pattern = match.group("inline")
|
|
|
|
replace = match.group("body")
|
|
|
|
start_delimiter = match.group("start_delimiter")
|
|
|
|
end_delimiter = match.group("end_delimiter")
|
|
|
|
|
|
|
|
if start_delimiter != end_delimiter:
|
|
|
|
continue
|
|
|
|
|
2017-12-13 09:44:24 +00:00
|
|
|
entity = self.INLINE_DELIMITERS[start_delimiter](start, len(replace))
|
2017-12-05 11:42:57 +00:00
|
|
|
offset += len(start_delimiter) * 2
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
entities.append(entity)
|
|
|
|
text = text.replace(pattern, replace)
|
|
|
|
|
2017-12-07 01:02:16 +00:00
|
|
|
return dict(
|
2017-12-13 09:44:24 +00:00
|
|
|
message=self.remove_surrogates(text),
|
2017-12-07 01:02:16 +00:00
|
|
|
entities=entities
|
|
|
|
)
|