2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Markdown & HTML formatting functions.
|
|
|
|
|
|
|
|
.. versionadded:: 4.5.1
|
|
|
|
"""
|
|
|
|
|
|
|
|
import html
|
2022-07-24 21:14:09 +03:00
|
|
|
|
2022-04-30 22:28:00 +03:00
|
|
|
import re
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
from typing import Optional
|
|
|
|
|
2022-04-30 22:28:00 +03:00
|
|
|
|
|
|
|
def format_text(*args, separator="\n"):
|
|
|
|
"""
|
|
|
|
Formats a list of strings into a single string.
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
.. code:: python3
|
2022-04-30 22:28:00 +03:00
|
|
|
|
|
|
|
format_text( # just an example
|
|
|
|
mbold('Hello'),
|
|
|
|
mitalic('World')
|
|
|
|
)
|
|
|
|
|
|
|
|
:param args: Strings to format.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type args: :obj:`str`
|
|
|
|
|
2022-04-30 22:28:00 +03:00
|
|
|
:param separator: The separator to use between each string.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type separator: :obj:`str`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return separator.join(args)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def escape_html(content: str) -> str:
|
|
|
|
"""
|
|
|
|
Escapes HTML characters in a string of HTML.
|
|
|
|
|
|
|
|
:param content: The string of HTML to escape.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:return: The escaped string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return html.escape(content)
|
|
|
|
|
|
|
|
|
|
|
|
def escape_markdown(content: str) -> str:
|
|
|
|
"""
|
|
|
|
Escapes Markdown characters in a string of Markdown.
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
Credits to: simonsmh
|
2022-04-30 22:28:00 +03:00
|
|
|
|
|
|
|
:param content: The string of Markdown to escape.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:return: The escaped string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
parse = re.sub(r"([_*\[\]()~`>\#\+\-=|\.!])", r"\\\1", content)
|
|
|
|
reparse = re.sub(r"\\\\([_*\[\]()~`>\#\+\-=|\.!])", r"\1", parse)
|
|
|
|
return reparse
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def mbold(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns a Markdown-formatted bold string.
|
|
|
|
|
|
|
|
:param content: The string to bold.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '*{}*'.format(escape_markdown(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def hbold(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns an HTML-formatted bold string.
|
|
|
|
|
|
|
|
:param content: The string to bold.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '<b>{}</b>'.format(escape_html(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def mitalic(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns a Markdown-formatted italic string.
|
|
|
|
|
|
|
|
:param content: The string to italicize.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '_{}_\r'.format(escape_markdown(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def hitalic(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns an HTML-formatted italic string.
|
|
|
|
|
|
|
|
:param content: The string to italicize.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '<i>{}</i>'.format(escape_html(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def munderline(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns a Markdown-formatted underline string.
|
|
|
|
|
|
|
|
:param content: The string to underline.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '__{}__'.format(escape_markdown(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def hunderline(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns an HTML-formatted underline string.
|
|
|
|
|
|
|
|
:param content: The string to underline.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
|
|
|
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '<u>{}</u>'.format(escape_html(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def mstrikethrough(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns a Markdown-formatted strikethrough string.
|
|
|
|
|
|
|
|
:param content: The string to strikethrough.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '~{}~'.format(escape_markdown(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def hstrikethrough(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns an HTML-formatted strikethrough string.
|
|
|
|
|
|
|
|
:param content: The string to strikethrough.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '<s>{}</s>'.format(escape_html(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def mspoiler(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns a Markdown-formatted spoiler string.
|
|
|
|
|
|
|
|
:param content: The string to spoiler.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '||{}||'.format(escape_markdown(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def hspoiler(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns an HTML-formatted spoiler string.
|
|
|
|
|
|
|
|
:param content: The string to spoiler.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '<tg-spoiler>{}</tg-spoiler>'.format(escape_html(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def mlink(content: str, url: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns a Markdown-formatted link string.
|
|
|
|
|
|
|
|
:param content: The string to link.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
2022-04-30 22:28:00 +03:00
|
|
|
:param url: The URL to link to.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type url: str
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '[{}]({})'.format(escape_markdown(content), escape_markdown(url) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def hlink(content: str, url: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns an HTML-formatted link string.
|
|
|
|
|
|
|
|
:param content: The string to link.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
2022-04-30 22:28:00 +03:00
|
|
|
:param url: The URL to link to.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type url: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '<a href="{}">{}</a>'.format(escape_html(url), escape_html(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def mcode(content: str, language: str="", escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns a Markdown-formatted code string.
|
|
|
|
|
|
|
|
:param content: The string to code.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '```{}\n{}```'.format(language, escape_markdown(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def hcode(content: str, escape: Optional[bool]=True) -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns an HTML-formatted code string.
|
|
|
|
|
|
|
|
:param content: The string to code.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
return '<code>{}</code>'.format(escape_html(content) if escape else content)
|
|
|
|
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
def hpre(content: str, escape: Optional[bool]=True, language: str="") -> str:
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
|
|
|
Returns an HTML-formatted preformatted string.
|
|
|
|
|
|
|
|
:param content: The string to preformatted.
|
2022-07-24 21:14:09 +03:00
|
|
|
:type content: :obj:`str`
|
|
|
|
|
|
|
|
:param escape: True if you need to escape special characters. Defaults to True.
|
|
|
|
:type escape: :obj:`bool`
|
|
|
|
|
|
|
|
:return: The formatted string.
|
|
|
|
:rtype: :obj:`str`
|
2022-04-30 22:28:00 +03:00
|
|
|
"""
|
2022-05-21 15:39:45 +03:00
|
|
|
return '<pre><code class="{}">{}</code></pre>'.format(language, escape_html(content) if escape else content)
|
|
|
|
|
|
|
|
|
|
|
|
def hide_link(url: str) -> str:
|
|
|
|
"""
|
|
|
|
Hide url of an image.
|
|
|
|
|
2022-07-24 21:14:09 +03:00
|
|
|
:param url: The url of the image.
|
|
|
|
:type url: :obj:`str`
|
|
|
|
|
|
|
|
:return: The hidden url.
|
|
|
|
:rtype: :obj:`str`
|
2022-05-21 15:39:45 +03:00
|
|
|
"""
|
|
|
|
return f'<a href="{url}">⁠</a>'
|