Exemplo n.º 1
0
async def highlight(context):
    """ Generates syntax highlighted images. """
    if context.fwd_from:
        return
    reply = await context.get_reply_message()
    reply_id = None
    await context.edit(lang('highlight_processing'))
    if reply:
        reply_id = reply.id
        target_file_path = await context.client.download_media(
            await context.get_reply_message())
        if target_file_path is None:
            message = reply.text
        else:
            if Magic(mime=True).from_file(target_file_path) != 'text/plain':
                message = reply.text
            else:
                with open(target_file_path, 'r') as file:
                    message = file.read()
            remove(target_file_path)
    else:
        if context.arguments:
            message = context.arguments
        else:
            await context.edit(lang('highlight_no_file'))
            return
    lexer = guess_lexer(message)
    formatter = img.JpgImageFormatter(style="colorful")
    result = syntax_highlight(message, lexer, formatter, outfile=None)
    await context.edit(lang('highlight_uploading'))
    await context.client.send_file(context.chat_id, result, reply_to=reply_id)
    await context.delete()
Exemplo n.º 2
0
async def highlight(context):
    """ Generates syntax highlighted images. """
    if not context.text[0].isalpha() and context.text[0] not in ("/", "#", "@",
                                                                 "!"):
        if context.fwd_from:
            return
        reply = await context.get_reply_message()
        reply_id = None
        await context.edit("Rendering image, please wait . . .")
        if reply:
            reply_id = reply.id
            message = reply.text
        else:
            if context.pattern_match.group(1):
                message = context.pattern_match.group(1)
            else:
                await context.edit("`Unable to retrieve target message.`")
                return
        lexer = guess_lexer(message)
        formatter = img.JpgImageFormatter(style="colorful")
        result = syntax_highlight(message, lexer, formatter, outfile=None)
        await context.edit("Uploading image . . .")
        await context.client.send_file(context.chat_id,
                                       result,
                                       reply_to=reply_id)
        await context.delete()
Exemplo n.º 3
0
async def highlight(context):
    """ Generates syntax highlighted images. """
    if context.fwd_from:
        return
    reply = await context.get_reply_message()
    reply_id = None
    if not silent:
        await context.edit(lang('highlight_processing'))
    if reply:
        reply_id = reply.id
        target_file_path = await context.client.download_media(
            await context.get_reply_message())
        if target_file_path is None:
            message = reply.text
        else:
            if Magic(mime=True).from_file(target_file_path) != 'text/plain':
                message = reply.text
            else:
                with open(target_file_path, 'r') as file:
                    message = file.read()
            remove(target_file_path)
    else:
        if context.arguments:
            message = context.arguments
        else:
            await context.edit(lang('highlight_no_file'))
            return
    lexer = guess_lexer(message)
    try:
        formatter = img.JpgImageFormatter(style="colorful")
    except img.FontNotFound:
        await context.edit(lang('caption_error'))
        return
    except FileNotFoundError:
        await context.edit(lang('caption_error'))
        return
    try:
        result = syntax_highlight(message, lexer, formatter, outfile=None)
    except OSError:
        await context.edit(lang('caption_error'))
        return
    if not silent:
        await context.edit(lang('highlight_uploading'))
    try:
        await context.client.send_file(context.chat_id,
                                       result,
                                       reply_to=reply_id)
    except PhotoInvalidDimensionsError:
        await context.edit(lang('caption_error'))
        return
    await context.delete()