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

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

项目:pdb    作者:antocuni    | 项目源码 | 文件源码
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
项目:incubator-airflow-old    作者:apache    | 项目源码 | 文件源码
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'))
项目:airflow    作者:apache-airflow    | 项目源码 | 文件源码
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'))
项目:girlfriend    作者:chihongze    | 项目源码 | 文件源码
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())
项目:pinder    作者:dotwaffle    | 项目源码 | 文件源码
def pprint_color(obj):
    print highlight(pformat(obj), PythonLexer(), Terminal256Formatter(style='trac'))
项目:git-reviewers    作者:johnnadratowski    | 项目源码 | 文件源码
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))
项目:healthchecks_asgards    作者:andela    | 项目源码 | 文件源码
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())
项目:flake8-html    作者:lordmauve    | 项目源码 | 文件源码
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()
        }
项目:leetcode    作者:thomasyimgit    | 项目源码 | 文件源码
def pylight(code):
    return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))

# builtin docstrings to ignore
项目:cdbcli    作者:kevinjqiu    | 项目源码 | 文件源码
def python(code):
    return pygments.highlight(code, lexers.PythonLexer(), formatters.TerminalFormatter())
项目:fandango    作者:tango-controls    | 项目源码 | 文件源码
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()
项目:lddmm-ot    作者:jeanfeydy    | 项目源码 | 文件源码
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()))
项目:Price-Comparator    作者:Thejas-1    | 项目源码 | 文件源码
def python_highlight(string):
        return highlight(string, PythonLexer(encoding="Utf-8"),
                         Terminal256Formatter(style='monokai',
                                              encoding="Utf-8"))
项目:Price-Comparator    作者:Thejas-1    | 项目源码 | 文件源码
def python_html_highlight(string):
        return highlight(string, PythonLexer(encode="Utf-8"),
                         HtmlFormatter(noclasses=True, encoding="UTf-8"))
项目:driveboardapp    作者:nortd    | 项目源码 | 文件源码
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()))
        """)
项目:codenn    作者:sriniiyer    | 项目源码 | 文件源码
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
项目:pygameweb    作者:pygame    | 项目源码 | 文件源码
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}'
项目:freesound-datasets    作者:MTG    | 项目源码 | 文件源码
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})
项目:Repobot    作者:Desgard    | 项目源码 | 文件源码
def pylight(code):
    return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))

# builtin docstrings to ignore
项目:tbvaccine    作者:skorokithakis    | 项目源码 | 文件源码
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
项目:tbvaccine    作者:skorokithakis    | 项目源码 | 文件源码
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"))
项目:iprofiler    作者:j-towns    | 项目源码 | 文件源码
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)
项目:pyta    作者:pyta-uoft    | 项目源码 | 文件源码
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
项目:mac-package-build    作者:persepolisdm    | 项目源码 | 文件源码
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()))
        """)
项目:blender    作者:gastrodia    | 项目源码 | 文件源码
def pylight(code):
    return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))

# builtin docstrings to ignore
项目:yatta_reader    作者:sound88    | 项目源码 | 文件源码
def pylight(code):
    return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True))

# builtin docstrings to ignore
项目:VisualPython    作者:RobinManoli    | 项目源码 | 文件源码
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