1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-12-17 23:24:20 +00:00
royalnet/royalnet_telethon/formatting.py

47 lines
1.6 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
2022-02-22 03:49:04 +00:00
- ``\uE01F``: start spoiler
- ``\uE00F``: end spoiler
2021-04-20 00:25:25 +00:00
- ``\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>")
2022-02-22 03:49:04 +00:00
string = string.replace("\uE01F", "<tg-spoiler>")
string = string.replace("\uE00F", "</tg-spoiler>")
2021-04-20 00:25:25 +00:00
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