1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00
royalnet/royalnet_telethon/formatting.py

43 lines
1.4 KiB
Python
Raw Normal View History

2021-04-20 00:25:25 +00:00
def tg_html_format(string: str) -> str:
"""
Format a string as Telegram HTML.
The following characters will be considered control characters:
- ``\uE01B``: start bold
- ``\uE00B``: end bold
- ``\uE011``: start italic
- ``\uE001``: end italic
- ``\uE012``: start underline
- ``\uE002``: end underline
- ``\uE015``: start strike
- ``\uE005``: end strike
- ``\uE01C``: start single-line code
- ``\uE00C``: end single-line code
- ``\uE01D``: start multi-line code
- ``\uE00D``: end multi-line code
:param string: The string to format.
:return: The formatted string.
.. warning:: For now, this is a Telethon implementation detail.
.. todo:: This may cause denial of service attacks from users!
"""
string = string.replace("&", "&")
string = string.replace("<", "&lt;")
string = string.replace(">", "&gt;")
string = string.replace("\uE01B", "<b>")
string = string.replace("\uE00B", "</b>")
string = string.replace("\uE011", "<i>")
string = string.replace("\uE001", "</i>")
string = string.replace("\uE015", "<s>")
string = string.replace("\uE005", "</s>")
string = string.replace("\uE012", "<u>")
string = string.replace("\uE002", "</u>")
string = string.replace("\uE01C", "<code>")
string = string.replace("\uE00C", "</code>")
string = string.replace("\uE01D", "<pre>")
string = string.replace("\uE00D", "</pre>")
return string