aiogram/aiogram/utils/markdown.py

279 lines
5.4 KiB
Python
Raw Normal View History

from .text_decorations import html_decoration, markdown_decoration
LIST_MD_SYMBOLS = "*_`["
2017-05-19 22:55:29 +03:00
2017-07-22 20:16:51 +03:00
MD_SYMBOLS = (
(LIST_MD_SYMBOLS[0], LIST_MD_SYMBOLS[0]),
(LIST_MD_SYMBOLS[1], LIST_MD_SYMBOLS[1]),
(LIST_MD_SYMBOLS[2], LIST_MD_SYMBOLS[2]),
(LIST_MD_SYMBOLS[2] * 3 + "\n", "\n" + LIST_MD_SYMBOLS[2] * 3),
2021-12-30 16:45:33 +03:00
("||", "||"),
("<b>", "</b>"),
("<i>", "</i>"),
("<code>", "</code>"),
("<pre>", "</pre>"),
2022-01-01 13:14:15 +03:00
('<span class="tg-spoiler">', "</span>"),
2017-07-22 20:16:51 +03:00
)
2017-05-19 22:55:29 +03:00
HTML_QUOTES_MAP = {"<": "&lt;", ">": "&gt;", "&": "&amp;", '"': "&quot;"}
_HQS = HTML_QUOTES_MAP.keys() # HQS for HTML QUOTES SYMBOLS
2017-07-22 20:16:51 +03:00
def quote_html(*content, sep=" ") -> str:
"""
Quote HTML symbols
All <, >, & and " symbols that are not a part of a tag or
an HTML entity must be replaced with the corresponding HTML entities
(< with &lt; > with &gt; & with &amp and " with &quot).
:param content:
:param sep:
:return:
"""
return html_decoration.quote(_join(*content, sep=sep))
def escape_md(*content, sep=" ") -> str:
"""
Escape markdown text
E.g. for usernames
:param content:
:param sep:
:return:
"""
return markdown_decoration.quote(_join(*content, sep=sep))
def _join(*content, sep=" "):
2017-07-22 20:16:51 +03:00
return sep.join(map(str, content))
def text(*content, sep=" "):
2017-07-25 18:18:49 +03:00
"""
Join all elements with a separator
2017-07-25 18:18:49 +03:00
:param content:
:param sep:
:return:
"""
return _join(*content, sep=sep)
2017-05-19 22:55:29 +03:00
def bold(*content, sep=" ") -> str:
2017-07-25 18:18:49 +03:00
"""
Make bold text (Markdown)
:param content:
:param sep:
:return:
"""
return markdown_decoration.bold(
2020-05-10 22:40:37 +07:00
value=markdown_decoration.quote(_join(*content, sep=sep))
)
2017-05-19 22:55:29 +03:00
def hbold(*content, sep=" ") -> str:
2017-07-25 18:18:49 +03:00
"""
Make bold text (HTML)
:param content:
:param sep:
:return:
"""
return html_decoration.bold(
value=html_decoration.quote(_join(*content, sep=sep))
)
def italic(*content, sep=" ") -> str:
2017-07-25 18:18:49 +03:00
"""
Make italic text (Markdown)
:param content:
:param sep:
:return:
"""
return markdown_decoration.italic(
2020-05-10 22:40:37 +07:00
value=markdown_decoration.quote(_join(*content, sep=sep))
)
2017-05-19 22:55:29 +03:00
def hitalic(*content, sep=" ") -> str:
2017-07-25 18:18:49 +03:00
"""
Make italic text (HTML)
:param content:
:param sep:
:return:
"""
return html_decoration.italic(
value=html_decoration.quote(_join(*content, sep=sep))
)
2021-12-30 16:45:33 +03:00
def spoiler(*content, sep=" ") -> str:
"""
Make spoiler text (Markdown)
:param content:
:param sep:
:return:
"""
return markdown_decoration.spoiler(
value=markdown_decoration.quote(_join(*content, sep=sep))
)
def hspoiler(*content, sep=" ") -> str:
"""
Make spoiler text (HTML)
:param content:
:param sep:
:return:
"""
return html_decoration.spoiler(
value=html_decoration.quote(_join(*content, sep=sep))
)
def code(*content, sep=" ") -> str:
2017-07-25 18:18:49 +03:00
"""
Make mono-width text (Markdown)
:param content:
:param sep:
:return:
"""
return markdown_decoration.code(
2020-05-10 22:40:37 +07:00
value=markdown_decoration.quote(_join(*content, sep=sep))
)
2017-05-19 22:55:29 +03:00
def hcode(*content, sep=" ") -> str:
2017-07-25 18:18:49 +03:00
"""
Make mono-width text (HTML)
:param content:
:param sep:
:return:
"""
return html_decoration.code(
value=html_decoration.quote(_join(*content, sep=sep))
)
def pre(*content, sep="\n") -> str:
2017-07-25 18:18:49 +03:00
"""
Make mono-width text block (Markdown)
:param content:
:param sep:
:return:
"""
return markdown_decoration.pre(
2020-05-10 22:40:37 +07:00
value=markdown_decoration.quote(_join(*content, sep=sep))
)
2017-05-19 22:55:29 +03:00
def hpre(*content, sep="\n") -> str:
2017-07-25 18:18:49 +03:00
"""
Make mono-width text block (HTML)
:param content:
:param sep:
:return:
"""
return html_decoration.pre(
value=html_decoration.quote(_join(*content, sep=sep))
)
def underline(*content, sep=" ") -> str:
2017-07-25 18:18:49 +03:00
"""
Make underlined text (Markdown)
2017-07-25 18:18:49 +03:00
:param content:
:param sep:
2017-07-25 18:18:49 +03:00
:return:
"""
return markdown_decoration.underline(
value=markdown_decoration.quote(_join(*content, sep=sep))
)
2017-05-19 22:55:29 +03:00
def hunderline(*content, sep=" ") -> str:
2017-07-25 18:18:49 +03:00
"""
Make underlined text (HTML)
2017-07-25 18:18:49 +03:00
:param content:
:param sep:
2017-07-25 18:18:49 +03:00
:return:
"""
return html_decoration.underline(
value=html_decoration.quote(_join(*content, sep=sep))
)
def strikethrough(*content, sep=" ") -> str:
"""
Make strikethrough text (Markdown)
:param content:
:param sep:
:return:
2017-07-25 18:18:49 +03:00
"""
return markdown_decoration.strikethrough(
value=markdown_decoration.quote(_join(*content, sep=sep))
)
2017-07-25 18:18:49 +03:00
def hstrikethrough(*content, sep=" ") -> str:
"""
Make strikethrough text (HTML)
2017-07-25 18:18:49 +03:00
:param content:
:param sep:
:return:
"""
return html_decoration.strikethrough(
value=html_decoration.quote(_join(*content, sep=sep))
)
def link(title: str, url: str) -> str:
"""
Format URL (Markdown)
:param title:
:param url:
:return:
"""
return markdown_decoration.link(value=markdown_decoration.quote(title), link=url)
def hlink(title: str, url: str) -> str:
"""
Format URL (HTML)
:param title:
:param url:
:return:
"""
return html_decoration.link(value=html_decoration.quote(title), link=url)
def hide_link(url: str) -> str:
"""
Hide URL (HTML only)
Can be used for adding an image to a text message
:param url:
:return:
"""
return f'<a href="{url}">&#8288;</a>'