Python pygments 模块,lexers() 实例源码

我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用pygments.lexers()

项目:zing    作者:evernote    | 项目源码 | 文件源码
def setup(app):
    # enable Pygments json lexer
    try:
        import pygments
        if pygments.__version__ >= '1.5':
            # use JSON lexer included in recent versions of Pygments
            from pygments.lexers import JsonLexer
        else:
            # use JSON lexer from pygments-json if installed
            from pygson.json_lexer import JSONLexer as JsonLexer
    except ImportError:
        pass  # not fatal if we have old (or no) Pygments and no pygments-json
    else:
        app.add_lexer('json', JsonLexer())

    return {"parallel_read_safe": True}
项目:sahriswiki    作者:prologic    | 项目源码 | 文件源码
def highlight(text, mime=None, lang=None, linenos=False, title=""):
    formatter = HTMLFormatter(
        cssclass="code",
        linenos=linenos,
        full=True,
        title=title
    )

    try:
        if mime:
            lexer = pygments.lexers.get_lexer_for_mimetype(mime)
        elif lang:
            lexer = pygments.lexers.get_lexer_by_name(lang)
        else:
            lexer = pygments.lexers.guess_lexer(text)
    except pygments.util.ClassNotFound:
        return tag.pre(text)

    return Markup(pygments.highlight(text, lexer, formatter))
项目:os-http    作者:openstack-dev    | 项目源码 | 文件源码
def format_resp(resp):
    # I can see no way to get the HTTP version
    headers = ["HTTP/1.1 %d %s" % (resp.status_code, resp.reason or '')]
    headers.extend('%s: %s' % k for k in resp.headers.items())
    headers = '\n'.join(headers)

    if 'json' in resp.headers.get('Content-Type', '').lower():
        body = json.dumps(resp.json(), sort_keys=True, indent=4)
    else:
        body = resp.content

    if pygments:
        mime = resp.headers.get('Content-Type')
        http_lexer = pygments.lexers.get_lexer_by_name('http')
        formatter = pygments.formatters.get_formatter_by_name(formatter_name)

        try:
            body_lexer = pygments.lexers.get_lexer_for_mimetype(mime)
        except pygments.util.ClassNotFound:
            body_lexer = pygments.lexers.get_lexer_by_name('text')

        headers = pygments.highlight(headers, http_lexer, formatter)
        body = pygments.highlight(body, body_lexer, formatter)

    return '\n'.join([headers, '', body])
项目:style_transfer    作者:crowsonkb    | 项目源码 | 文件源码
def print_args():
    """Prints out all command-line parameters."""
    bg = terminal_bg()
    if bg is True:
        style = 'xcode'
    elif bg is False:
        style = 'monokai'

    pprint = print_
    try:
        if bg is not None:
            import pygments
            from pygments.lexers import Python3Lexer
            from pygments.formatters import Terminal256Formatter
            pprint = partial(pygments.highlight, lexer=Python3Lexer(),
                             formatter=Terminal256Formatter(style=style), outfile=sys.stdout)
    except ImportError:
        pass
    print_('Parameters:')
    for key in sorted(ARGS):
        v = repr(getattr(ARGS, key))
        print_('% 14s: ' % key, end='')
        pprint(v)
    print_()
项目:nodepy    作者:nodepy    | 项目源码 | 文件源码
def format_html(fp, exclude=()):
  frames = stackframes()
  fp.write('<!DOCTYPE html>\n')
  fp.write('<html><head><title>{} Traces</title></head><body>\n'.format(len(frames)))
  for thread_id, stack in sorted(frames.items(), key=lambda x: x[0]):
    name = 'Thread {}'.format(thread_id)
    if thread_id == threading.get_ident():
      name += ' (tracing thread)'
    elif thread_id == main_thread.ident:
      name += ' (main)'
    fp.write('<h3>{}</h3>\n'.format(name))
    tbstr = format_stack(stack)
    if pygments:
      formatter = pygments.formatters.HtmlFormatter(full=False, noclasses=True)
      lexer = pygments.lexers.PythonLexer()
      tbstr = pygments.highlight(tbstr, lexer, formatter)
    fp.write(tbstr)
    fp.write('\n')
  fp.write('</body>\n')
项目:TCP-IP    作者:JackZ0    | 项目源码 | 文件源码
def setup(app):
    # enable Pygments json lexer
    try:
        import pygments
        if pygments.__version__ >= '1.5':
            # use JSON lexer included in recent versions of Pygments
            from pygments.lexers import JsonLexer
        else:
            # use JSON lexer from pygments-json if installed
            from pygson.json_lexer import JSONLexer as JsonLexer
    except ImportError:
        pass  # not fatal if we have old (or no) Pygments and no pygments-json
    else:
        app.add_lexer('json', JsonLexer())
项目:aws-cfn-plex    作者:lordmuffin    | 项目源码 | 文件源码
def _add_syntax_highlighting(self, insource, language):
        lexer = pygments.lexers.get_lexer_by_name(language, stripall=True)
        if language in ('latex', 'tex'):
            fmtr = OdtPygmentsLaTeXFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        else:
            fmtr = OdtPygmentsProgFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        outsource = pygments.highlight(insource, lexer, fmtr)
        return outsource
项目:AshsSDK    作者:thehappydinoa    | 项目源码 | 文件源码
def _add_syntax_highlighting(self, insource, language):
        lexer = pygments.lexers.get_lexer_by_name(language, stripall=True)
        if language in ('latex', 'tex'):
            fmtr = OdtPygmentsLaTeXFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        else:
            fmtr = OdtPygmentsProgFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        outsource = pygments.highlight(insource, lexer, fmtr)
        return outsource
项目:chalktalk_docs    作者:loremIpsum1771    | 项目源码 | 文件源码
def _add_syntax_highlighting(self, insource, language):
        lexer = pygments.lexers.get_lexer_by_name(language, stripall=True)
        if language in ('latex', 'tex'):
            fmtr = OdtPygmentsLaTeXFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        else:
            fmtr = OdtPygmentsProgFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        outsource = pygments.highlight(insource, lexer, fmtr)
        return outsource
项目:ceph-lcm    作者:Mirantis    | 项目源码 | 文件源码
def colorize(text, color, lexer):
    if pygments is None or not color:
        return text

    lexer_obj = pygments.lexers.get_lexer_by_name(lexer, ensurenl=False)
    formatter_obj = pygments.formatters.get_formatter_by_name(
        "terminal", bg=color)
    colorized = pygments.highlight(text, lexer_obj, formatter_obj)

    return colorized
项目:dotnet-binary-deserializer    作者:koutto    | 项目源码 | 文件源码
def print_xml_highlighted(xml):
        if pygments is not None:
            print pygments.highlight(xml, 
                                     pygments.lexers.get_lexer_by_name('XML'), 
                                     pygments.formatters.get_formatter_by_name('terminal'))
        else:
            print xml
项目:jatumba-backend    作者:YetAnotherTeam    | 项目源码 | 文件源码
def format(self, record):
        # Check if Pygments is available for coloring
        try:
            import pygments
            from pygments.lexers import SqlLexer
            from pygments.formatters import Terminal256Formatter
        except ImportError:
            pygments = None

        # Check if sqlparse is available for indentation
        try:
            import sqlparse
        except ImportError:
            sqlparse = None

        # Remove leading and trailing whitespacespip
        sql = record.sql.strip()

        if sqlparse:
            # Indent the SQL query
            sql = sqlparse.format(sql, reindent=True)

        if pygments:
            # Highlight the SQL query
            sql = pygments.highlight(
                sql,
                SqlLexer(),
                Terminal256Formatter(style='monokai')
            )

        # Set the record's statement to the formatted query
        record.statement = sql
        return super(SQLFormatter, self).format(record)
项目:blackmamba    作者:zrzka    | 项目源码 | 文件源码
def _add_syntax_highlighting(self, insource, language):
        lexer = pygments.lexers.get_lexer_by_name(language, stripall=True)
        if language in ('latex', 'tex'):
            fmtr = OdtPygmentsLaTeXFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        else:
            fmtr = OdtPygmentsProgFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        outsource = pygments.highlight(insource, lexer, fmtr)
        return outsource
项目:RST-vscode    作者:tht13    | 项目源码 | 文件源码
def _add_syntax_highlighting(self, insource, language):
        lexer = pygments.lexers.get_lexer_by_name(language, stripall=True)
        if language in ('latex', 'tex'):
            fmtr = OdtPygmentsLaTeXFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        else:
            fmtr = OdtPygmentsProgFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        outsource = pygments.highlight(insource, lexer, fmtr)
        return outsource
项目:net.tcp-proxy    作者:ernw    | 项目源码 | 文件源码
def parse(data, key):
    fp = BytesIO(data)
    build_dictionary(fp, key)
    records = Record.parse(fp)
    out = StringIO()
    print_records(records, fp=out)
    out.seek(0)

    if pygments is not None:
        print(pygments.highlight(out.read(),
                                 pygments.lexers.get_lexer_by_name('XML'),
                                 pygments.formatters.get_formatter_by_name('terminal')))
    else:
        print(out.read())
项目:tf_aws_ecs_instance_draining_on_scale_in    作者:terraform-community-modules    | 项目源码 | 文件源码
def _add_syntax_highlighting(self, insource, language):
        lexer = pygments.lexers.get_lexer_by_name(language, stripall=True)
        if language in ('latex', 'tex'):
            fmtr = OdtPygmentsLaTeXFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        else:
            fmtr = OdtPygmentsProgFormatter(lambda name, parameters=():
                self.rststyle(name, parameters),
                escape_function=escape_cdata)
        outsource = pygments.highlight(insource, lexer, fmtr)
        return outsource