Exemplo n.º 1
0
def parse_pre(text):
    text = text.strip()
    return (text, [
        MessageEntityPre(offset=0,
                         length=len(add_surrogate(text)),
                         language='potato')
    ])
Exemplo n.º 2
0
async def stringfy_message(event):
    msg = event.message
    output = [msg]

    raw_text = add_surrogate(msg.raw_text)
    for e in (event.entities or []):
        mention = None
        if isinstance(e, types.MessageEntityMention):
            mention = del_surrogate(raw_text[e.offset:e.offset + e.length])
        if isinstance(e, types.MessageEntityMentionName):
            mention = e.user_id

        if not mention:
            continue

        try:
            received_entity = await event.client.get_entity(mention)
        except:
            continue
        output.append(received_entity)

    yaml_text = str()
    for l in output:
        skip = Skip()
        yaml_text += bprint(l,
                            stream=str,
                            max_str_len=64,
                            max_bytes_len=64,
                            indent="  ",
                            skip_predicate=skip,
                            maximum_depth=4,
                            inline_singular=True,
                            skip_cyclic=False)

    await event.reply(yaml_text, parse_mode=parse_pre)
Exemplo n.º 3
0
def parse_pre(text):
    """
    Parser (for parse_mode) that makes everything monospace
    """
    text = text.strip()
    return (
        text,
        [MessageEntityPre(offset=0, length=len(add_surrogate(text)), language='')]
    )
Exemplo n.º 4
0
def parse(message, old_entities=None):
    try:
        entities = []
        old_entities = sorted(old_entities or [], key=lambda e: e.offset)

        i = 0
        after = 0
        message = add_surrogate(message)
        while i < len(message):
            for after, e in enumerate(old_entities[after:], start=after):
                # If the next entity is strictly to our right, we're done here
                if i < e.offset:
                    break
                # Skip already existing entities if we're at one
                if i == e.offset:
                    i += e.length
            else:
                after += 1

            # Find the first pattern that matches
            for pattern, parser in MATCHERS:
                match = pattern.match(message, pos=i)
                if match:
                    break
            else:
                i += 1
                continue

            text, entity = parser(match)

            # Shift old entities after our current position (so they stay in
            # place)
            shift = len(text) - len(match[0])
            if shift:
                for e in old_entities[after:]:
                    e.offset += shift

            # Replace whole match with text from parser
            message = "".join(
                (message[:match.start()], text, message[match.end():]))

            # Append entity if we got one
            if entity:
                entities.append(entity)

            # Skip past the match
            i += len(text)

        return del_surrogate(message), entities + old_entities
    except Exception as e:
        LOGS.info(str(e))