Пример #1
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()
Пример #2
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()
Пример #3
0
 def highlight(data, language=None, default='python'):
     """Simple wrapper around pygments"""
     try:
         lexer = get_lexer_by_name(language, stripall=True, encoding='UTF-8')
     except ValueError:
         lexer = get_lexer_by_name(default, stripall=True, encoding='UTF-8')
     
     formatter = formatters.HtmlFormatter()
     return syntax_highlight(data, lexer, formatter)
Пример #4
0
def render(message):
    message = unquote(message)
    lexer = get_lexer_by_name("diff", stripall=True)
    # lexer = guess_lexer(message)
    formatter = ImageFormatter(font_name=font,
                               style="colorful",
                               font_size=12,
                               line_pad=4)
    result = syntax_highlight(message, lexer, formatter, outfile=None)
    return result
Пример #5
0
 def highlight(self):
     try:
         from pygments import formatters
         formatter = formatters.HtmlFormatter
         from pygments import highlight as syntax_highlight
         return mark_safe(syntax_highlight(self.code,
                                           self.get_lexer(),
                                           formatters.HtmlFormatter(linenos='table',
                                                                    lineanchors='line',
                                                                    anchorlinenos=True)))
     except ImportError:
         return mark_safe(self.code)
Пример #6
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()