mirror of
https://github.com/Mayuri-Chan/pyrofork.git
synced 2025-12-29 12:04:51 +00:00
Pyrofork: Add blockquote markdown parser
Co-authored-by: wulan17 <wulan17@nusantararom.org> Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
parent
a95c1b6ac1
commit
c13f3b766e
2 changed files with 36 additions and 1 deletions
|
|
@ -74,6 +74,8 @@ To strictly use this mode, pass :obj:`~pyrogram.enums.ParseMode.MARKDOWN` to the
|
||||||
code block
|
code block
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> Quoted text
|
||||||
|
|
||||||
**Example**:
|
**Example**:
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import html
|
import html
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
|
@ -32,6 +33,7 @@ STRIKE_DELIM = "~~"
|
||||||
SPOILER_DELIM = "||"
|
SPOILER_DELIM = "||"
|
||||||
CODE_DELIM = "`"
|
CODE_DELIM = "`"
|
||||||
PRE_DELIM = "```"
|
PRE_DELIM = "```"
|
||||||
|
BLOCKQUOTE_DELIM = ">"
|
||||||
|
|
||||||
MARKDOWN_RE = re.compile(r"({d})|\[(.+?)\]\((.+?)\)".format(
|
MARKDOWN_RE = re.compile(r"({d})|\[(.+?)\]\((.+?)\)".format(
|
||||||
d="|".join(
|
d="|".join(
|
||||||
|
|
@ -59,9 +61,39 @@ class Markdown:
|
||||||
def __init__(self, client: Optional["pyrogram.Client"]):
|
def __init__(self, client: Optional["pyrogram.Client"]):
|
||||||
self.html = HTML(client)
|
self.html = HTML(client)
|
||||||
|
|
||||||
|
def blockquote_parser(self, text):
|
||||||
|
text = re.sub(r'\n>', '\n>', re.sub(r'^>', '>', text))
|
||||||
|
lines = text.split('\n')
|
||||||
|
result = []
|
||||||
|
|
||||||
|
in_blockquote = False
|
||||||
|
|
||||||
|
for line in lines:
|
||||||
|
if line.startswith(BLOCKQUOTE_DELIM):
|
||||||
|
if not in_blockquote:
|
||||||
|
line = re.sub(r'^> ', OPENING_TAG.format("blockquote"), line)
|
||||||
|
line = re.sub(r'^>', OPENING_TAG.format("blockquote"), line)
|
||||||
|
in_blockquote = True
|
||||||
|
result.append(line.strip())
|
||||||
|
else:
|
||||||
|
result.append(line[-1].strip())
|
||||||
|
else:
|
||||||
|
if in_blockquote:
|
||||||
|
line = CLOSING_TAG.format("blockquote") + line
|
||||||
|
in_blockquote = False
|
||||||
|
result.append(line.strip())
|
||||||
|
|
||||||
|
if in_blockquote:
|
||||||
|
line = result[len(result)-1] + CLOSING_TAG.format("blockquote")
|
||||||
|
result.pop(len(result)-1)
|
||||||
|
result.append(line)
|
||||||
|
|
||||||
|
return '\n'.join(result)
|
||||||
|
|
||||||
async def parse(self, text: str, strict: bool = False):
|
async def parse(self, text: str, strict: bool = False):
|
||||||
if strict:
|
if strict:
|
||||||
text = html.escape(text)
|
text = html.escape(text)
|
||||||
|
text = self.blockquote_parser(text)
|
||||||
|
|
||||||
delims = set()
|
delims = set()
|
||||||
is_fixed_width = False
|
is_fixed_width = False
|
||||||
|
|
@ -141,7 +173,8 @@ class Markdown:
|
||||||
start_tag = f"{PRE_DELIM}{language}\n"
|
start_tag = f"{PRE_DELIM}{language}\n"
|
||||||
end_tag = f"\n{PRE_DELIM}"
|
end_tag = f"\n{PRE_DELIM}"
|
||||||
elif entity_type == MessageEntityType.BLOCKQUOTE:
|
elif entity_type == MessageEntityType.BLOCKQUOTE:
|
||||||
start_tag = end_tag = PRE_DELIM
|
start_tag = BLOCKQUOTE_DELIM
|
||||||
|
end_tag = ""
|
||||||
elif entity_type == MessageEntityType.SPOILER:
|
elif entity_type == MessageEntityType.SPOILER:
|
||||||
start_tag = end_tag = SPOILER_DELIM
|
start_tag = end_tag = SPOILER_DELIM
|
||||||
elif entity_type == MessageEntityType.TEXT_LINK:
|
elif entity_type == MessageEntityType.TEXT_LINK:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue