[docs]defescape_html(text:str)->str:"""Escape HTML special characters for Telegram HTML mode. :param text: Raw text possibly containing ``&``, ``<`` and ``>``. :returns: A safe string for Telegram ``parse_mode=HTML``. Example:: >>> escape_html('<b>Hi & welcome</b>') '<b>Hi & welcome</b>' """# First escape HTML special characterstext=text.replace("&","&")text=text.replace("<","<")text=text.replace(">",">")returntext
[docs]defremove_html_tags(text:str)->str:"""Remove HTML tags from a string. :param text: Input text with possible tags like ``<b>``. :returns: Plain text with tags removed. Example:: >>> remove_html_tags('<b>Hello</b> world') 'Hello world' """returnre.sub(r"<[^>]*>","",text)
[docs]defcut_text(text:str,max_length:int)->str:"""Cut text to max_length characters and add ellipsis if text is longer. :param text: Input text. :param max_length: Maximum length of the text. :returns: Cut text with ellipsis if it is longer than max_length. Example:: >>> cut_text("Hello, world!", 5) 'Hello...' >>> cut_text("Hello, world!", 12) 'Hello, world!' """iflen(text)<=max_length:returntextreturntext[:max_length]+"..."