mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 21:07:59 +00:00
Ignore any other style when inside a fixed-width style
This commit is contained in:
parent
f12cee5d94
commit
8e0182633f
@ -24,66 +24,76 @@ from .html import HTML
|
|||||||
|
|
||||||
|
|
||||||
class Markdown:
|
class Markdown:
|
||||||
BOLD_DELIMITER = "**"
|
BOLD_DELIM = "**"
|
||||||
ITALIC_DELIMITER = "__"
|
ITALIC_DELIM = "__"
|
||||||
UNDERLINE_DELIMITER = "--"
|
UNDERLINE_DELIM = "--"
|
||||||
STRIKE_DELIMITER = "~~"
|
STRIKE_DELIM = "~~"
|
||||||
CODE_DELIMITER = "`"
|
CODE_DELIM = "`"
|
||||||
PRE_DELIMITER = "```"
|
PRE_DELIM = "```"
|
||||||
|
|
||||||
MARKDOWN_RE = re.compile(r"({d})".format(
|
MARKDOWN_RE = re.compile(r"({d})".format(
|
||||||
d="|".join(
|
d="|".join(
|
||||||
["".join(i) for i in [
|
["".join(i) for i in [
|
||||||
[r"\{}".format(j) for j in i]
|
[r"\{}".format(j) for j in i]
|
||||||
for i in [
|
for i in [
|
||||||
PRE_DELIMITER,
|
PRE_DELIM,
|
||||||
CODE_DELIMITER,
|
CODE_DELIM,
|
||||||
STRIKE_DELIMITER,
|
STRIKE_DELIM,
|
||||||
UNDERLINE_DELIMITER,
|
UNDERLINE_DELIM,
|
||||||
ITALIC_DELIMITER,
|
ITALIC_DELIM,
|
||||||
BOLD_DELIMITER
|
BOLD_DELIM
|
||||||
]
|
]
|
||||||
]]
|
]]
|
||||||
)))
|
)))
|
||||||
|
|
||||||
URL_RE = re.compile(r"\[([^[]+)]\(([^(]+)\)")
|
URL_RE = re.compile(r"\[([^[]+)]\(([^(]+)\)")
|
||||||
|
|
||||||
|
OPENING_TAG = "<{}>"
|
||||||
|
CLOSING_TAG = "</{}>"
|
||||||
|
URL_MARKUP = '<a href="{}">{}</a>'
|
||||||
|
FIXED_WIDTH_DELIMS = [CODE_DELIM, PRE_DELIM]
|
||||||
|
|
||||||
def __init__(self, client: "pyrogram.BaseClient"):
|
def __init__(self, client: "pyrogram.BaseClient"):
|
||||||
self.html = HTML(client)
|
self.html = HTML(client)
|
||||||
|
|
||||||
def parse(self, text: str):
|
def parse(self, text: str):
|
||||||
|
text = html.escape(text)
|
||||||
|
|
||||||
offset = 0
|
offset = 0
|
||||||
delimiters = set()
|
delims = set()
|
||||||
|
|
||||||
for i, match in enumerate(re.finditer(Markdown.MARKDOWN_RE, text)):
|
for i, match in enumerate(re.finditer(Markdown.MARKDOWN_RE, text)):
|
||||||
start, stop = match.span()
|
start, stop = match.span()
|
||||||
delimiter = match.group(1)
|
delim = match.group(1)
|
||||||
|
|
||||||
if delimiter == Markdown.BOLD_DELIMITER:
|
if delim == Markdown.BOLD_DELIM:
|
||||||
tag = "b"
|
tag = "b"
|
||||||
elif delimiter == Markdown.ITALIC_DELIMITER:
|
elif delim == Markdown.ITALIC_DELIM:
|
||||||
tag = "i"
|
tag = "i"
|
||||||
elif delimiter == Markdown.UNDERLINE_DELIMITER:
|
elif delim == Markdown.UNDERLINE_DELIM:
|
||||||
tag = "u"
|
tag = "u"
|
||||||
elif delimiter == Markdown.STRIKE_DELIMITER:
|
elif delim == Markdown.STRIKE_DELIM:
|
||||||
tag = "s"
|
tag = "s"
|
||||||
elif delimiter == Markdown.CODE_DELIMITER:
|
elif delim == Markdown.CODE_DELIM:
|
||||||
tag = "code"
|
tag = "code"
|
||||||
elif delimiter == Markdown.PRE_DELIMITER:
|
elif delim == Markdown.PRE_DELIM:
|
||||||
tag = "pre"
|
tag = "pre"
|
||||||
else:
|
else:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if delimiter not in delimiters:
|
if delim not in Markdown.FIXED_WIDTH_DELIMS and any(x in delims for x in Markdown.FIXED_WIDTH_DELIMS):
|
||||||
delimiters.add(delimiter)
|
continue
|
||||||
tag = "<{}>".format(tag)
|
|
||||||
|
if delim not in delims:
|
||||||
|
delims.add(delim)
|
||||||
|
tag = Markdown.OPENING_TAG.format(tag)
|
||||||
else:
|
else:
|
||||||
delimiters.remove(delimiter)
|
delims.remove(delim)
|
||||||
tag = "</{}>".format(tag)
|
tag = Markdown.CLOSING_TAG.format(tag)
|
||||||
|
|
||||||
text = text[:start + offset] + tag + text[stop + offset:]
|
text = text[:start + offset] + tag + text[stop + offset:]
|
||||||
|
|
||||||
offset += len(tag) - len(delimiter)
|
offset += len(tag) - len(delim)
|
||||||
|
|
||||||
offset = 0
|
offset = 0
|
||||||
|
|
||||||
@ -92,9 +102,7 @@ class Markdown:
|
|||||||
full = match.group(0)
|
full = match.group(0)
|
||||||
|
|
||||||
body, url = match.groups()
|
body, url = match.groups()
|
||||||
body = html.escape(body)
|
replace = Markdown.URL_MARKUP.format(url, body)
|
||||||
|
|
||||||
replace = '<a href="{}">{}</a>'.format(url, body)
|
|
||||||
|
|
||||||
text = text[:start + offset] + replace + text[stop + offset:]
|
text = text[:start + offset] + replace + text[stop + offset:]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user