我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用pygments.lexers()。
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}
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))
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])
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_()
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')
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())
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
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
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
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)
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())