""" Simple HTML -> Telegram entity parser. """ import struct from collections import deque from html import escape from html.parser import HTMLParser from typing import Iterable, Optional, Tuple, List from ..helpers import add_surrogate, del_surrogate, within_surrogate, strip_text from ..tl import TLObject from ..tl.types import ( MessageEntityBold, MessageEntityItalic, MessageEntityCode, MessageEntityPre, MessageEntityEmail, MessageEntityUrl, MessageEntityTextUrl, MessageEntityMentionName, MessageEntityUnderline, MessageEntityStrike, MessageEntityBlockquote, TypeMessageEntity ) class HTMLToTelegramParser(HTMLParser): def __init__(self): super().__init__() self.text = '' self.entities = [] self._building_entities = {} self._open_tags = deque() self._open_tags_meta = deque() def handle_starttag(self, tag, attrs): self._open_tags.appendleft(tag) self._open_tags_meta.appendleft(None) attrs = dict(attrs) EntityType = None args = {} if tag == 'strong' or tag == 'b': EntityType = MessageEntityBold elif tag == 'em' or tag == 'i': EntityType = MessageEntityItalic elif tag == 'u': EntityType = MessageEntityUnderline elif tag == 'del' or tag == 's': EntityType = MessageEntityStrike elif tag == 'blockquote': EntityType = MessageEntityBlockquote elif tag == 'code': try: # If we're in the middle of a
 tag, this  tag is
                # probably intended for syntax highlighting.
                #
                # Syntax highlighting is set with
                #     codeblock
                # inside 
 tags
                pre = self._building_entities['pre']
                try:
                    pre.language = attrs['class'][len('language-'):]
                except KeyError:
                    pass
            except KeyError:
                EntityType = MessageEntityCode
        elif tag == 'pre':
            EntityType = MessageEntityPre
            args['language'] = ''
        elif tag == 'a':
            try:
                url = attrs['href']
            except KeyError:
                return
            if url.startswith('mailto:'):
                url = url[len('mailto:'):]
                EntityType = MessageEntityEmail
            else:
                if self.get_starttag_text() == url:
                    EntityType = MessageEntityUrl
                else:
                    EntityType = MessageEntityTextUrl
                    args['url'] = del_surrogate(url)
                    url = None
            self._open_tags_meta.popleft()
            self._open_tags_meta.appendleft(url)

        if EntityType and tag not in self._building_entities:
            self._building_entities[tag] = EntityType(
                offset=len(self.text),
                # The length will be determined when closing the tag.
                length=0,
                **args)

    def handle_data(self, text):
        previous_tag = self._open_tags[0] if len(self._open_tags) > 0 else ''
        if previous_tag == 'a':
            url = self._open_tags_meta[0]
            if url:
                text = url

        for tag, entity in self._building_entities.items():
            entity.length += len(text)

        self.text += text

    def handle_endtag(self, tag):
        try:
            self._open_tags.popleft()
            self._open_tags_meta.popleft()
        except IndexError:
            pass
        entity = self._building_entities.pop(tag, None)
        if entity:
            self.entities.append(entity)


def parse(html: str) -> Tuple[str, List[TypeMessageEntity]]:
    """
    Parses the given HTML message and returns its stripped representation
    plus a list of the MessageEntity's that were found.

    :param html: the message with HTML to be parsed.
    :return: a tuple consisting of (clean message, [message entities]).
    """
    if not html:
        return html, []

    parser = HTMLToTelegramParser()
    parser.feed(add_surrogate(html))
    text = strip_text(parser.text, parser.entities)
    return del_surrogate(text), parser.entities


ENTITY_TO_FORMATTER = {
    MessageEntityBold: ('', ''),
    MessageEntityItalic: ('', ''),
    MessageEntityCode: ('', ''),
    MessageEntityUnderline: ('', ''),
    MessageEntityStrike: ('', ''),
    MessageEntityBlockquote: ('
', '
'), MessageEntityPre: lambda e, _: ( "
\n"
        "    \n"
        "        ".format(e.language), "{}\n"
        "    \n"
        "
" ), MessageEntityEmail: lambda _, t: (''.format(t), ''), MessageEntityUrl: lambda _, t: (''.format(t), ''), MessageEntityTextUrl: lambda e, _: (''.format(escape(e.url)), ''), MessageEntityMentionName: lambda e, _: (''.format(e.user_id), ''), } def unparse(text: str, entities: Iterable[TypeMessageEntity]) -> str: """ Performs the reverse operation to .parse(), effectively returning HTML given a normal text and its MessageEntity's. :param text: the text to be reconverted into HTML. :param entities: the MessageEntity's applied to the text. :return: a HTML representation of the combination of both inputs. """ if not text: return text elif not entities: return escape(text) if isinstance(entities, TLObject): entities = (entities,) text = add_surrogate(text) insert_at = [] for entity in entities: s = entity.offset e = entity.offset + entity.length delimiter = ENTITY_TO_FORMATTER.get(type(entity), None) if delimiter: if callable(delimiter): delimiter = delimiter(entity, text[s:e]) insert_at.append((s, delimiter[0])) insert_at.append((e, delimiter[1])) insert_at.sort(key=lambda t: t[0]) next_escape_bound = len(text) while insert_at: # Same logic as markdown.py at, what = insert_at.pop() while within_surrogate(text, at): at += 1 text = text[:at] + what + escape(text[at:next_escape_bound]) + text[next_escape_bound:] next_escape_bound = at text = escape(text[:next_escape_bound]) + text[next_escape_bound:] return del_surrogate(text)