我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用pygments.lexers.PythonLexer()。
def _init_pygments(self): if not self.config.use_pygments: return False try: from pygments.lexers import PythonLexer from pygments.formatters import TerminalFormatter, Terminal256Formatter except ImportError: return False if hasattr(self, '_fmt'): return True if hasattr(self.config, 'formatter'): self._fmt = self.config.formatter else: Formatter = (Terminal256Formatter if self.config.use_terminal256formatter and '256color' in os.environ.get('TERM', '') else TerminalFormatter) self._fmt = Formatter(bg=self.config.bg, colorscheme=self.config.colorscheme) self._lexer = PythonLexer() return True
def code(self): dag_id = request.args.get('dag_id') dag = dagbag.get_dag(dag_id) title = dag_id try: with open(dag.fileloc, 'r') as f: code = f.read() html_code = highlight( code, lexers.PythonLexer(), HtmlFormatter(linenos=True)) except IOError as e: html_code = str(e) return self.render( 'airflow/dag_code.html', html_code=html_code, dag=dag, title=title, root=request.args.get('root'), demo_mode=conf.getboolean('webserver', 'demo_mode'))
def code(self): dag_id = request.args.get('dag_id') dag = dagbag.get_dag(dag_id) title = dag_id try: m = importlib.import_module(dag.module_name) code = inspect.getsource(m) html_code = highlight( code, lexers.PythonLexer(), HtmlFormatter(linenos=True)) except IOError as e: html_code = str(e) return self.render( 'airflow/dag_code.html', html_code=html_code, dag=dag, title=title, root=request.args.get('root'), demo_mode=conf.getboolean('webserver', 'demo_mode'))
def do_show(self, line): """Show me the code! """ code = autopep8.fix_code("".join(self._generate_workflow_code())) if self.options.no_highlight: print code else: print highlight(code, PythonLexer(), TerminalFormatter())
def pprint_color(obj): print highlight(pformat(obj), PythonLexer(), Terminal256Formatter(style='trac'))
def print_contributer_lines(contributer, diff_infos): output = [] for diff_info in diff_infos: lines = diff_info["reviewers"].get(contributer) if not lines: continue shl.print_section(shl.BOLD, diff_info["from_hash"], diff_info["file"], file=output) prev_line = None for line in lines: try: from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import TerminalFormatter code = highlight(line["code_line"], PythonLexer(), TerminalFormatter()) except ImportError: code = line["code_line"] cur_line = int(line["line_num"]) if prev_line and prev_line + 1 < cur_line: output.append(" .") output.append(" .") output.append(" .") output.append("{line_num: >5}|\t{code_line}".format(line_num=line["line_num"], code_line=code.rstrip())) prev_line = cur_line output.append("\n\n") pydoc.pager("\n".join(output))
def handle(self, *args, **options): try: from pygments import lexers except ImportError: self.stdout.write("This command requires Pygments package.") self.stdout.write("Please install it with:\n\n") self.stdout.write(" pip install Pygments\n\n") return # Invocation examples _process("bash", lexers.BashLexer()) _process("browser", lexers.JavascriptLexer()) _process("crontab", lexers.BashLexer()) _process("python", lexers.PythonLexer()) _process("php", lexers.PhpLexer()) _process("powershell", lexers.shell.PowerShellLexer()) _process("node", lexers.JavascriptLexer()) # API examples _process("list_checks_request", lexers.BashLexer()) _process("list_checks_response", lexers.JsonLexer()) _process("create_check_request", lexers.BashLexer()) _process("create_check_response", lexers.JsonLexer()) _process("pause_check_request", lexers.BashLexer()) _process("pause_check_response", lexers.JsonLexer())
def _format_source(self, source): formatter = HtmlFormatter(nowrap=True) html = highlight(source, PythonLexer(), formatter) return { 'html_lines': [Markup(l) for l in html.splitlines()], 'css': formatter.get_style_defs() }
def pylight(code): return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True)) # builtin docstrings to ignore
def python(code): return pygments.highlight(code, lexers.PythonLexer(), formatters.TerminalFormatter())
def __init__(self,parent=None,model='',filename='~/.qeval_history'): #'import fandango'): import fandango.web, fandango.functional print('%s()'%type(self).__name__) Qt.QWidget.__init__(self,parent) try: self.name = type(self).__name__ self._locals = {'self':self,'load':self.setModel,'printf':self.printf,'Qt':Qt} self._locals.update([(k,v) for k,v in fandango.functional.__dict__.items() if isCallable(v)]) self._locals['mdir'] = self.dir_module self._locals['help'] = self.help self._locals['doc'] = lambda x:x.__doc__ self._locals['run'] = fandango.objects.loadModule self._locals.update((k,getattr(fandango.web,k)) for k in ('table','bold','color')) self._modules = {} self._instances = {} self.history = [] self.filename = filename.replace('~',os.getenv('HOME')) if filename.startswith('~') else filename try:#Enabling Syntax Highlighting from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter lexer,frmt=PythonLexer(),HtmlFormatter() self.css=frmt.get_style_defs('.highlight') self.highlight = lambda t,l=lexer,f=frmt: highlight(t,l,f) #html='<head><style>%s</style><body>%s</body>'%(css,highlight(code,lexer,frmt)) except: traceback.print_exc() self.css = None self.highlight = lambda t: '<pre>%s</pre>'%t self.evalQ('import fandango') self.evalQ('import fandango.qt') self.evalQ('f=fandango') self.setup_ui() self.setEval() self.setModel(model or '') except: traceback.print_exc()
def show_code(func): if type(func) is str : code = func else : code = inspect.getsourcelines(func)[0] code = ''.join(code) print(highlight(code, PythonLexer(), Terminal256Formatter()))
def python_highlight(string): return highlight(string, PythonLexer(encoding="Utf-8"), Terminal256Formatter(style='monokai', encoding="Utf-8"))
def python_html_highlight(string): return highlight(string, PythonLexer(encode="Utf-8"), HtmlFormatter(noclasses=True, encoding="UTf-8"))
def test_pygments(pyi_builder): pyi_builder.test_source( """ # This sample code is taken from http://pygments.org/docs/quickstart/. from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter code = 'print "Hello World"' print(highlight(code, PythonLexer(), HtmlFormatter())) """)
def _format_sql(sql, data, format='html'): popts = {} if data.get('remove_comments'): popts['strip_comments'] = True if data.get('keyword_case', 'undefined') not in ('undefined', ''): popts['keyword_case'] = data.get('keyword_case') if data.get('identifier_case', 'undefined') not in ('undefined', ''): popts['identifier_case'] = data.get('identifier_case') if data.get('n_indents', None) is not None: val = data.get('n_indents') try: popts['indent_width'] = max(1, min(1000, int(val))) popts['reindent'] = True except (ValueError, TypeError): pass if (not 'indent_width' in popts and data.get('reindent', '').lower() in ('1', 'true', 't')): popts['indent_width'] = 2 popts['reindent'] = True if data.get('output_format', None) is not None: popts['output_format'] = data.get('output_format') logging.debug('Format: %s, POPTS: %r', format, popts) logging.debug(sql) sql = sqlparse.format(sql, **popts) if format in ('html', 'json'): if data.get('highlight', False): if popts['output_format'] == 'python': lexer = PythonLexer() elif popts['output_format'] == 'php': lexer = PhpLexer() else: lexer = SqlLexer() sql = highlight(sql, lexer, HtmlFormatter()) else: sql = ('<textarea class="resizable" ' 'style="height: 350px; margin-top: 1em;">%s</textarea>' % sql) return sql
def _wiki_code_callback(matchobj): m = matchobj.groups() content = m[1] content = re.sub(r'^\s*', '', content, re.S) content = re.sub(r'\s*$', '', content, re.S) # code_class = m[0] formatter = HtmlFormatter() styles = formatter.get_style_defs('.highlight') code = highlight(content, PythonLexer(), formatter) return f'<style>{styles}</style>{code}'
def download_release(request, short_name, release_tag): dataset = get_object_or_404(Dataset, short_name=short_name) release = get_object_or_404(DatasetRelease, dataset=dataset, release_tag=release_tag) if release.type is not 'PU' and not dataset.user_is_maintainer(request.user): raise HttpResponseNotAllowed script = utils.generate_download_script(dataset) formatted_script = highlight(script, PythonLexer(), HtmlFormatter()) highlighting_styles = HtmlFormatter().get_style_defs('.highlight') return render(request, 'datasets/download.html', {'dataset': dataset, 'release': release, 'formatted_script': formatted_script, 'highlighting_styles': highlighting_styles})
def _process_var_line(self, line): """ Process a line of variables in the traceback. """ if self._show_vars is False or (self._isolate and not self._file_in_dir()): # Don't print. return False else: line = highlight(line, PythonLexer(), TerminalFormatter(style="monokai")) self._print(line.rstrip("\r\n"), max_length=self._max_length) return True
def _process_code_line(self, line): """ Process a line of code in the traceback. """ if self._isolate and not self._file_in_dir(): # Print without colors. self._print(line) else: if self._isolate: line = line[1:] self._print(">", fg="red", style="bright") line = highlight(line, PythonLexer(), TerminalFormatter(style="monokai")) self._print(line.rstrip("\r\n"))
def generate_lprofile(self, fun): """ Generate div containing profiled source code with timings of each line, taken from iline_profiler. """ self.value_lprofile = "" try: filename = fun.co_filename firstlineno = fun.co_firstlineno name = fun.co_name except AttributeError: return ltimings_key = (filename, firstlineno, name) try: ltimings = self.lprofile.timings[ltimings_key] except KeyError: return # Currently the correct filename is stored at the end of ltimings. # This is a work-around to fix cProfiler giving useless filenames for # zipped packages. filename = ltimings[-1] if filename.endswith(('.pyc', '.pyo')): filename = openpy.source_from_cache(filename) if ".egg/" in filename: add_zipped_file_to_linecache(filename) raw_code = "" linenos = range(firstlineno, ltimings[-2][0] + 1) for lineno in linenos: raw_code += ulinecache.getline(filename, lineno) formatter = LProfileFormatter(firstlineno, ltimings, noclasses=True) self.value_lprofile = highlight(raw_code, PythonLexer(), formatter)
def _vendor_wrap(self, colour_class, text): """Override in reporters that wrap snippet lines in vendor styles, e.g. pygments.""" if '-line' not in colour_class: text = highlight(text, PythonLexer(), HtmlFormatter(nowrap=True, lineseparator='', classprefix='pygments-')) return text
def tokens(self, event=None): """ Highlight tokens as rendered by Pygments. Seems to only work after textarea is updated, though calling update_idletasks has no effect. The problem can be solved by recalling the function if there is no bbox, (as with update_linenumbers), or figure out what is not updated when running this function (bbox was the case in update_linenumbers). """ # http://stackoverflow.com/a/30199105 from pygments import lex, highlight from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter # don't use because multiline strings can start at beginning and end in visible view #tv = self.mainframe.texthelper.top_visible(self.textarea) # use since highlight works if multiline str not properly closed bv = self.mainframe.texthelper.bottom_visible(self.textarea) data = self.textarea.get("1.0", bv) # "end-1c" if data == self.prevdata: return self.clear_tokens() #print( highlight(data, PythonLexer(), HtmlFormatter())) prev_content = '' i = 0 for token, content in lex(data, PythonLexer()): lencontent = len(content) # this happens sometimes in lubuntu if not content: #print('no content in HighLight.tokens() loop') continue #str(token) == 'Token.Literal.String.Doc' \ if self.mainframe.texthelper.visible(self.textarea, '1.0 + %dc' % i) \ or self.mainframe.texthelper.visible(self.textarea, '1.0 + %dc' % (i+lencontent)): self.textarea.mark_set("range_start", "1.0 + %dc" %i ) self.textarea.mark_set("range_end", "range_start + %dc" % lencontent) self.textarea.tag_add(str(token), "range_start", "range_end") i += lencontent self.prevdata = data