2020-03-21 14:43:32 +00:00
|
|
|
# Pyrogram - Telegram MTProto API Client Library for Python
|
|
|
|
# Copyright (C) 2017-2020 Dan <https://github.com/delivrance>
|
2017-12-05 11:42:57 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# This file is part of Pyrogram.
|
2017-12-05 11:42:57 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# 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.
|
2017-12-05 11:42:57 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# 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.
|
2017-12-05 11:42:57 +00:00
|
|
|
#
|
2020-03-21 14:43:32 +00:00
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
2017-12-05 11:42:57 +00:00
|
|
|
|
2019-06-24 08:54:58 +00:00
|
|
|
import html
|
2017-12-05 11:42:57 +00:00
|
|
|
import re
|
2020-12-20 16:05:17 +00:00
|
|
|
from typing import Optional
|
2017-12-05 11:42:57 +00:00
|
|
|
|
2019-03-26 12:32:30 +00:00
|
|
|
import pyrogram
|
2019-06-25 08:24:19 +00:00
|
|
|
from . import utils
|
2019-06-24 08:07:28 +00:00
|
|
|
from .html import HTML
|
2017-12-05 11:42:57 +00:00
|
|
|
|
2019-06-25 08:24:19 +00:00
|
|
|
BOLD_DELIM = "**"
|
|
|
|
ITALIC_DELIM = "__"
|
|
|
|
UNDERLINE_DELIM = "--"
|
|
|
|
STRIKE_DELIM = "~~"
|
|
|
|
CODE_DELIM = "`"
|
|
|
|
PRE_DELIM = "```"
|
2017-12-05 11:42:57 +00:00
|
|
|
|
2019-06-26 14:05:09 +00:00
|
|
|
MARKDOWN_RE = re.compile(r"({d})|\[(.+?)\]\((.+?)\)".format(
|
|
|
|
d="|".join(
|
|
|
|
["".join(i) for i in [
|
2020-08-22 06:05:05 +00:00
|
|
|
[rf"\{j}" for j in i]
|
2019-06-26 14:05:09 +00:00
|
|
|
for i in [
|
|
|
|
PRE_DELIM,
|
|
|
|
CODE_DELIM,
|
|
|
|
STRIKE_DELIM,
|
|
|
|
UNDERLINE_DELIM,
|
|
|
|
ITALIC_DELIM,
|
|
|
|
BOLD_DELIM
|
|
|
|
]
|
|
|
|
]]
|
|
|
|
)))
|
|
|
|
|
|
|
|
OPENING_TAG = "<{}>"
|
|
|
|
CLOSING_TAG = "</{}>"
|
|
|
|
URL_MARKUP = '<a href="{}">{}</a>'
|
|
|
|
FIXED_WIDTH_DELIMS = [CODE_DELIM, PRE_DELIM]
|
|
|
|
|
2017-12-05 11:42:57 +00:00
|
|
|
|
2019-06-25 08:24:19 +00:00
|
|
|
class Markdown:
|
2020-12-20 16:05:17 +00:00
|
|
|
def __init__(self, client: Optional["pyrogram.Client"]):
|
2019-06-24 08:07:28 +00:00
|
|
|
self.html = HTML(client)
|
2018-02-15 10:24:56 +00:00
|
|
|
|
2020-08-21 05:28:27 +00:00
|
|
|
async def parse(self, text: str, strict: bool = False):
|
2019-06-26 14:05:09 +00:00
|
|
|
if strict:
|
|
|
|
text = html.escape(text)
|
2019-06-24 11:35:58 +00:00
|
|
|
|
|
|
|
delims = set()
|
2019-06-26 14:05:09 +00:00
|
|
|
is_fixed_width = False
|
|
|
|
|
|
|
|
for i, match in enumerate(re.finditer(MARKDOWN_RE, text)):
|
|
|
|
start, _ = match.span()
|
|
|
|
delim, text_url, url = match.groups()
|
|
|
|
full = match.group(0)
|
|
|
|
|
|
|
|
if delim in FIXED_WIDTH_DELIMS:
|
|
|
|
is_fixed_width = not is_fixed_width
|
2019-06-24 08:07:28 +00:00
|
|
|
|
2019-06-26 14:05:09 +00:00
|
|
|
if is_fixed_width and delim not in FIXED_WIDTH_DELIMS:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if text_url:
|
|
|
|
text = utils.replace_once(text, full, URL_MARKUP.format(url, text_url), start)
|
|
|
|
continue
|
2019-06-24 08:07:28 +00:00
|
|
|
|
2019-06-25 08:24:19 +00:00
|
|
|
if delim == BOLD_DELIM:
|
2019-06-24 08:07:28 +00:00
|
|
|
tag = "b"
|
2019-06-25 08:24:19 +00:00
|
|
|
elif delim == ITALIC_DELIM:
|
2019-06-24 08:07:28 +00:00
|
|
|
tag = "i"
|
2019-06-25 08:24:19 +00:00
|
|
|
elif delim == UNDERLINE_DELIM:
|
2019-06-24 08:07:28 +00:00
|
|
|
tag = "u"
|
2019-06-25 08:24:19 +00:00
|
|
|
elif delim == STRIKE_DELIM:
|
2019-06-24 08:07:28 +00:00
|
|
|
tag = "s"
|
2019-06-25 08:24:19 +00:00
|
|
|
elif delim == CODE_DELIM:
|
2019-06-24 08:07:28 +00:00
|
|
|
tag = "code"
|
2019-06-25 08:24:19 +00:00
|
|
|
elif delim == PRE_DELIM:
|
2019-06-24 08:07:28 +00:00
|
|
|
tag = "pre"
|
|
|
|
else:
|
|
|
|
continue
|
2018-02-15 10:24:56 +00:00
|
|
|
|
2019-06-24 11:35:58 +00:00
|
|
|
if delim not in delims:
|
|
|
|
delims.add(delim)
|
2019-06-26 14:05:09 +00:00
|
|
|
tag = OPENING_TAG.format(tag)
|
2019-06-24 08:07:28 +00:00
|
|
|
else:
|
2019-06-24 11:35:58 +00:00
|
|
|
delims.remove(delim)
|
2019-06-26 14:05:09 +00:00
|
|
|
tag = CLOSING_TAG.format(tag)
|
2019-06-24 08:54:58 +00:00
|
|
|
|
2019-06-26 14:05:09 +00:00
|
|
|
text = utils.replace_once(text, delim, tag, start)
|
2018-05-10 12:46:14 +00:00
|
|
|
|
2020-08-21 05:28:27 +00:00
|
|
|
return await self.html.parse(text)
|
2019-06-25 08:24:19 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def unparse(text: str, entities: list):
|
|
|
|
text = utils.add_surrogates(text)
|
2019-08-07 11:48:21 +00:00
|
|
|
|
|
|
|
entities_offsets = []
|
2019-06-25 08:24:19 +00:00
|
|
|
|
|
|
|
for entity in entities:
|
2019-08-07 11:48:21 +00:00
|
|
|
entity_type = entity.type
|
2019-06-25 08:24:19 +00:00
|
|
|
start = entity.offset
|
|
|
|
end = start + entity.length
|
|
|
|
|
2019-08-07 11:48:21 +00:00
|
|
|
if entity_type == "bold":
|
|
|
|
start_tag = end_tag = BOLD_DELIM
|
|
|
|
elif entity_type == "italic":
|
|
|
|
start_tag = end_tag = ITALIC_DELIM
|
|
|
|
elif entity_type == "underline":
|
|
|
|
start_tag = end_tag = UNDERLINE_DELIM
|
|
|
|
elif entity_type == "strike":
|
|
|
|
start_tag = end_tag = STRIKE_DELIM
|
|
|
|
elif entity_type == "code":
|
|
|
|
start_tag = end_tag = CODE_DELIM
|
|
|
|
elif entity_type in ("pre", "blockquote"):
|
|
|
|
start_tag = end_tag = PRE_DELIM
|
|
|
|
elif entity_type == "text_link":
|
|
|
|
url = entity.url
|
|
|
|
start_tag = "["
|
2020-08-22 06:05:05 +00:00
|
|
|
end_tag = f"]({url})"
|
2019-08-07 11:48:21 +00:00
|
|
|
elif entity_type == "text_mention":
|
|
|
|
user = entity.user
|
|
|
|
start_tag = "["
|
2020-08-22 06:05:05 +00:00
|
|
|
end_tag = f"](tg://user?id={user.id})"
|
2019-06-25 08:24:19 +00:00
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
2019-08-07 11:48:21 +00:00
|
|
|
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:]
|
2019-06-25 08:24:19 +00:00
|
|
|
|
|
|
|
return utils.remove_surrogates(text)
|