• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python pygments.highlight函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pygments.highlight函数的典型用法代码示例。如果您正苦于以下问题:Python highlight函数的具体用法?Python highlight怎么用?Python highlight使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了highlight函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: pygments2xpre

def pygments2xpre(s, language="python"):
    "Return markup suitable for XPreformatted"
    try:
        from pygments import highlight
        from pygments.formatters import HtmlFormatter
    except ImportError:
        return s

    from pygments.lexers import get_lexer_by_name

    l = get_lexer_by_name(language)

    h = HtmlFormatter()
    # XXX: Does not work in Python 2, since pygments creates non-unicode
    # outpur snippets.
    # from io import StringIO
    from six import StringIO

    out = StringIO()
    highlight(s, l, h, out)
    styles = [
        (cls, style.split(";")[0].split(":")[1].strip())
        for cls, (style, ttype, level) in h.class2style.items()
        if cls and style and style.startswith("color:")
    ]
    from reportlab.lib.pygments2xpre import _2xpre

    return _2xpre(out.getvalue(), styles)
开发者ID:nikunj1,项目名称:z3c.rml,代码行数:28,代码来源:flowable.py


示例2: send_message

 def send_message(self, mess):
     if self.demo_mode:
         print(self.md_ansi.convert(mess.body))
     else:
         bar = '\n╌╌[{mode}]' + ('╌' * 60)
         super().send_message(mess)
         print(bar.format(mode='MD  '))
         if ANSI:
             print(highlight(mess.body, self.md_lexer, self.terminal_formatter))
         else:
             print(mess.body)
         print(bar.format(mode='HTML'))
         html = self.md_html.convert(mess.body)
         if ANSI:
             print(highlight(html, self.html_lexer, self.terminal_formatter))
         else:
             print(html)
         print(bar.format(mode='TEXT'))
         print(self.md_text.convert(mess.body))
         print(bar.format(mode='IM  '))
         print(self.md_im.convert(mess.body))
         if ANSI:
             print(bar.format(mode='ANSI'))
             print(self.md_ansi.convert(mess.body))
             print(bar.format(mode='BORDERLESS'))
             print(self.md_borderless_ansi.convert(mess.body))
         print('\n\n')
开发者ID:nyimbi,项目名称:errbot,代码行数:27,代码来源:text.py


示例3: highlightBlock

    def highlightBlock(self, text):
        """Takes a block, applies format to the document.
        according to what's in it.
        """

        # I need to know where in the document we are,
        # because our formatting info is global to
        # the document
        cb = self.currentBlock()
        p = cb.position()

        # The \n is not really needed, but sometimes  
        # you are in an empty last block, so your position is
        # **after** the end of the document.
        text=unicode(self.document().toPlainText())+'\n'

        # Yes, re-highlight the whole document.
        # There **must** be some optimizacion possibilities
        # but it seems fast enough.
        highlight(text,self.lexer,self.formatter)

        # Just apply the formatting to this block.
        # For titles, it may be necessary to backtrack
        # and format a couple of blocks **earlier**.
        for i in range(len(unicode(text))):
            try:
                self.setFormat(i,1,self.formatter.data[p+i])
            except IndexError:
                pass

        # I may need to do something about this being called
        # too quickly.
        self.tstamp=time.time()
开发者ID:astromme,项目名称:Chestnut,代码行数:33,代码来源:side_by_side.py


示例4: testExternalMultipleMemFuncs

    def testExternalMultipleMemFuncs( self ):
        className = "ClassName"
        funcName = "FuncName"
        funcName2 = "FuncName2"

        code =  """  
        int %s::%s (int count, char* mess ) { {} {}  } 
        int %s::%s (int count, char* mess ) {  } 
        """ % ( className, funcName, className, funcName2  )
        
        
        highlight( code, self.lexer, self.formatter)
        myRTags = FakeRTags()
        test = CppSemantics( myRTags )
        
        for t,v,num in self.formatter.current_line:
            test.Feed( t,v,num )

        self.assertEqual( myRTags.GetClass()[0].name, className, 
        "Class Name did not match %s" % myRTags.GetClass()[0].name )

        mf = myRTags.GetClass()[0].GetElements()[0]

        self.assertEqual( len(myRTags.GetClass()), 1, 
        "Too many classes (%d) created." % len(myRTags.GetClass()) )


        self.assertEqual( mf['method'][0].GetName(), funcName, 
        "Function Name %s did not match %s" % (mf['method'][0].GetName(), funcName) )

        self.assertEqual( test.state, 'start', 
        "Semantics not in correct state '%s'" % ( test.state ))
开发者ID:2015E8007361074,项目名称:wxPython,代码行数:32,代码来源:testCppSemantics.py


示例5: print_action_exception

 def print_action_exception(e):
     if isinstance(e.inner_exception, (ExecCommandFailed, QueryException)):
         print_exception(e.inner_exception)
     else:
         print '-'*79
         print highlight(e.traceback, PythonTracebackLexer(), Formatter())
         print '-'*79
开发者ID:Duologic,项目名称:python-deployer,代码行数:7,代码来源:default.py


示例6: pygments2xpre

def pygments2xpre(s, language="python"):
    "Return markup suitable for XPreformatted"
    try:
        from pygments import highlight
        from pygments.formatters import HtmlFormatter
    except ImportError:
        return s

    from pygments.lexers import get_lexer_by_name
    rconv = lambda x: x
    if isPy3:
        out = getStringIO()
    else:
        if isUnicode(s):
            s = asBytes(s)
            rconv = asUnicode
        out = getBytesIO()

    l = get_lexer_by_name(language)
    
    h = HtmlFormatter()
    highlight(s,l,h,out)
    styles = [(cls, style.split(';')[0].split(':')[1].strip())
                for cls, (style, ttype, level) in h.class2style.items()
                if cls and style and style.startswith('color:')]
    return rconv(_2xpre(out.getvalue(),styles))
开发者ID:bellyflopp,项目名称:HealthNet,代码行数:26,代码来源:pygments2xpre.py


示例7: testMemFuncs

    def testMemFuncs( self ):
        className = "myclassname"
        funcName = "myFuncName"

        code =  """  
        class %s {
        int %s (int count, char* mess ) { {} {}  } 
        };
        """% ( className, funcName )
        
        
        highlight( code, self.lexer, self.formatter)
        myRTags = FakeRTags()
        test = CppSemantics( myRTags )
        
        for t,v,num in self.formatter.current_line:
            test.Feed( t,v,num )

        self.assertEqual( myRTags.GetClass()[0].name, className, 
        "Class Name did not match %s" % myRTags.GetClass()[0].name )

        mf = myRTags.GetClass()[0].GetElements()[0]

        self.assertEqual( mf['method'][0].GetName(), funcName, 
        "Function Name %s did not match %s" % (mf['method'][0].GetName(), funcName) )

        self.assertEqual( test.state, 'start', 
        "Semantics not in correct state '%s'" % ( test.state ))
开发者ID:2015E8007361074,项目名称:wxPython,代码行数:28,代码来源:testCppSemantics.py


示例8: pcat

def pcat(filename, target='ipython'):

    code = read_file_or_url(filename)

    HTML_TEMPLATE = """<style>
    {}
    </style>
    {}
    """

    from pygments.lexers import get_lexer_for_filename
    lexer = get_lexer_for_filename(filename, stripall=True)

    from pygments.formatters import HtmlFormatter, TerminalFormatter
    from pygments import highlight

    try:
        assert(target=='ipython')
        from IPython.display import HTML, display
        from pygments.formatters import HtmlFormatter
        formatter = HtmlFormatter(linenos=True, cssclass="source")
        html_code = highlight(code, lexer, formatter)
        css = formatter.get_style_defs()
        html = HTML_TEMPLATE.format(css, html_code)
        htmlres = HTML(html)

        return htmlres

    except Exception as e:
        print(e)
        pass

    formatter = TerminalFormatter()
    output = highlight(code,lexer,formatter)
    print(output)
开发者ID:EconForge,项目名称:dolo,代码行数:35,代码来源:display.py


示例9: template

def template(template_id):
    """
    The view where we display the result
    in syntax-highlighted HTML and CSS
    """
    
    template  = Template.query.filter(Template.id == long(template_id)).first()
    if not template:
        return "The requested template doesn't exist", 404
    hashtml = len(template.html.strip()) > 0
    
    cssperma = 'http://%s/static/cssserve/%s.css'%(SITE_DOMAIN, str(template.id))
    pygmented_css_link =  highlight('<link rel="stylesheet" type="text/css" href="%s">'%cssperma,
                                    CssLexer(),
                                    HtmlFormatter(style = 'bw',
                                                  linenos = 'table'))
    return render_template('saved_template.html',
                           template = template,
                            pygmented_css_link_code = pygmented_css_link, 

                            pygmented_html_code = highlight(template.html,
                                                            HtmlLexer(),
                                                            HtmlFormatter(style = 'bw',
                                                                          linenos = 'table')), 
                            pygmented_css_code = highlight(template.css,
                                                           CssLexer(),
                                                           HtmlFormatter(style = 'bw',
                                                                         linenos = 'table')), 

                            pygments_style = HtmlFormatter(style = 'bw').get_style_defs('.highlight'),

                            hashtml = hashtml,
                            )
开发者ID:weasky,项目名称:example,代码行数:33,代码来源:webapp.py


示例10: format_codechunks

    def format_codechunks(self, chunk):
        from pygments import highlight
        from pygments.lexers import PythonLexer, TextLexer, PythonConsoleLexer

        # from IPythonLexer import IPythonLexer
        from pygments.formatters import LatexFormatter

        chunk["content"] = highlight(
            chunk["content"],
            PythonLexer(),
            LatexFormatter(verboptions="frame=single,fontsize=\small, xleftmargin=0.5em"),
        )
        if len(chunk["result"].strip()) > 0 and chunk["results"] == "verbatim":
            if chunk["term"]:
                chunk["result"] = highlight(
                    chunk["result"],
                    PythonLexer(),
                    LatexFormatter(verboptions="frame=single,fontsize=\small, xleftmargin=0.5em"),
                )
            else:
                chunk["result"] = highlight(
                    chunk["result"],
                    TextLexer(),
                    LatexFormatter(verboptions="frame=leftline,fontsize=\small, xleftmargin=0.5em"),
                )
        return PwebFormatter.format_codechunks(self, chunk)
开发者ID:edgimar,项目名称:Pweave--text-processing-framework,代码行数:26,代码来源:formatters.py


示例11: pretty_print

def pretty_print (src, out):
    """ `src' is a filepath to be formatted. `out' is a file object
        to be written. """
    f = os.path.basename(src)
    ext = os.path.splitext(src)[1]

    if ext != "json":
        s = open(src).read()

    if ext == "json":
        s = json.dumps(json.load(open(src)), indent=2)
    elif ext == "":
        # is executable script?
        if os.access(f, os.X_OK) and s.startswith("#!"):
            # this is a script without extention. read shebang
            shebang = s.split("\n", 1)[0]
            try:
                interpreter = shebang.split("env ")[1]
            except IndexError:
                interpreter = os.path.basename(shebang[2:])
            ext = _interpreter2ext(interpreter)
            f += "." + ext

    # colorful!
    try:
      lexer = pygments.lexers.get_lexer_for_filename(f)
    except pygments.util.ClassNotFound:
      lexer = pygments.lexers.get_lexer_for_mimetype("text/plain")
    fmt = pygments.formatters.Terminal256Formatter(encoding="utf-8")
    pygments.highlight(s, lexer, fmt, out)
开发者ID:ale-rt,项目名称:azcat,代码行数:30,代码来源:pretty_print.py


示例12: inner

    def inner():
        assert re.match(r'^[a-zA-Z._-]+$', script)
        exec_path = "scripts/" + script + ".py"
        cmd = ["python3", "-u", exec_path]  # -u: don't buffer output

        error = False

        proc = subprocess.Popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

        for line in proc.stdout:
            yield highlight(line, BashLexer(), HtmlFormatter())

        # Maybe there is more stdout after an error...
        for line in proc.stderr:
            error = True
            yield highlight(line, BashLexer(), HtmlFormatter())

        if error:
            yield "<script>parent.stream_error()</script>"
        else:
            yield "<script>parent.stream_success()</script>"
开发者ID:n2o,项目名称:pi-dashboard,代码行数:25,代码来源:views.py


示例13: pretty_print_body

def pretty_print_body(fmt, body):
    try:
        if fmt.lower() == 'json':
            d = json.loads(body.strip())
            s = json.dumps(d, indent=4, sort_keys=True)
            print pygments.highlight(s, JsonLexer(), TerminalFormatter())
        elif fmt.lower() == 'form':
            qs = repeatable_parse_qs(body)
            for k, v in qs.all_pairs():
                s = Colors.GREEN
                s += '%s: ' % urllib.unquote(k)
                s += Colors.ENDC
                s += urllib.unquote(v)
                print s
        elif fmt.lower() == 'text':
            print body
        elif fmt.lower() == 'xml':
            import xml.dom.minidom
            xml = xml.dom.minidom.parseString(body)
            print pygments.highlight(xml.toprettyxml(), XmlLexer(), TerminalFormatter())
        else:
            raise PappyException('"%s" is not a valid format' % fmt)
    except PappyException as e:
        raise e
    except:
        raise PappyException('Body could not be parsed as "%s"' % fmt)
开发者ID:amuntner,项目名称:pappy-proxy,代码行数:26,代码来源:view.py


示例14: get

    def get(self):
        self.values["project"] = "http://www.proven-corporation.com/software/app-engine-console/"

        if self.values["subpage"] == "usage":
            for exampleNum in range(len(self.examples)):
                key = "example%d" % (exampleNum + 1)
                val = util.trim(self.examples[exampleNum])
                val = pygments.highlight(val, self.resultLexer, self.outputFormatter).strip()
                self.values[key] = val
        elif self.values["subpage"] == "integration":
            self.values["example1"] = pygments.highlight(
                util.trim(
                    """
                def is_dev():
                    import os
                    return os.environ['SERVER_SOFTWARE'].startswith('Dev')
            """
                ),
                self.pythonLexer,
                self.outputFormatter,
            ).strip()
            self.values["example2"] = pygments.highlight(
                util.trim(
                    """
                >>> is_dev()
                True
            """
                ),
                self.resultLexer,
                self.outputFormatter,
            ).strip()
开发者ID:PatrickKennedy,项目名称:Sybil,代码行数:31,代码来源:console.py


示例15: pretty_print

def pretty_print (src, s, out, with_formatter, ext=None):
    """ `src' is a filepath to be formatted. `out' is a file object
        to be written."""

    if ext is None:
        ext = guess_ext_by_filename(src)
        if ext == "":
            ext = guess_ext_by_contents(s)

    # format
    if with_formatter:
        f = _load_formatter(ext)
        if f is not None:
            ext,s = f.format(s)

    # highlight
    h = _load_highlighter(ext)
    if h is None:
        try:
            lexer = pygments.lexers.get_lexer_by_name(ext)
        except pygments.util.ClassNotFound:
            lexer = pygments.lexers.get_lexer_for_mimetype("text/plain")
        fmt = pygments.formatters.Terminal256Formatter(encoding="utf-8")
        pygments.highlight(s, lexer, fmt, out)
    else:
        h.highlight(out, s)
        out.close()
开发者ID:msabramo,项目名称:azcat,代码行数:27,代码来源:pretty_print.py


示例16: print_colored_text

def print_colored_text(texts, format_name):
    if pygments_available:
        highlight(
            texts, get_lexer_by_name(format_name),
            TerminalFormatter(), sys.stdout)
    else:
        print(texts)
开发者ID:boyska,项目名称:aur,代码行数:7,代码来源:local_to_remote.py


示例17: main

def main(server, files_to_upload, log_level, ftp_dir, finish, user, passwd):
    api = dsapi.DataStreamAPI(None, None, None, ftp_server=server,ftp_dir=ftp_dir, ftp_user=user, ftp_pass=passwd)
    for file in files_to_upload:
        api.ftp_upload(file)
    if(finish):
        finish_return = api.ftp_finish()
        print pygments.highlight(json.dumps(finish_return.json()),JsonLexer(),TerminalFormatter(bg="dark"))
开发者ID:QIAGENDatastream,项目名称:dsapi,代码行数:7,代码来源:ds_ftp_upload.py


示例18: colorize

def colorize(language, title, text):
    """Colorize the text syntax.

    Guess the language of the text and colorize it.

    Returns a tuple containing the colorized text and the language name.
    """
    formatter = HtmlFormatter(
        linenos=True, style=PygmentsStyle, noclasses=True, nobackground=True)
    #Try to get the lexer by name
    try:
        lexer = get_lexer_by_name(language.lower())
        return highlight(text, lexer, formatter), lexer.name
    except LexerNotFound:
        pass
    #Try to get the lexer by filename
    try:
        lexer = get_lexer_for_filename(title.lower())
        return highlight(text, lexer, formatter), lexer.name
    except LexerNotFound:
        pass
    #Try to guess the lexer from the text
    try:
        lexer = guess_lexer(text)
        if lexer.analyse_text(text) > .3:
            return highlight(text, lexer, formatter), lexer.name
    except LexerNotFound:
        pass
    #Fallback to the plain/text lexer
    lexer = get_lexer_by_name('text')
    return highlight(text, lexer, formatter), lexer.name
开发者ID:Kozea,项目名称:PastaBin,代码行数:31,代码来源:pastabin.py


示例19: setError

    def setError(self, err_type=None, err_value=None, err_traceback=None):
        """Translates the given error object into an HTML string and places it
        it the message panel

        :param error: an error object (typically an exception object)
        :type error: object"""

        msgbox = self._msgbox
        html_orig = '<html><head><style type="text/css">{style}</style>' "</head><body>"
        style, formatter = "", None
        if pygments is not None:
            formatter = HtmlFormatter()
            style = formatter.get_style_defs()
        html = html_orig.format(style=style)
        for de in err_value:
            e_html = """<pre>{reason}: {desc}</pre>{origin}<hr>"""
            origin, reason, desc = de.origin, de.reason, de.desc
            if reason.startswith("PyDs_") and pygments is not None:
                origin = highlight(origin, PythonTracebackLexer(), formatter)
            else:
                origin = "<pre>%s</pre>" % origin
            html += e_html.format(desc=desc, origin=origin, reason=reason)
        html += "</body></html>"
        msgbox.setText(err_value[0].desc)
        msgbox.setDetailedHtml(html)

        exc_info = "".join(traceback.format_exception(err_type, err_value, err_traceback))
        html = html_orig.format(style=style)
        if pygments is None:
            html += "<pre>%s</pre>" % exc_info
        else:
            html += highlight(exc_info, PythonTracebackLexer(), formatter)
        html += "</body></html>"
        msgbox.setOriginHtml(html)
开发者ID:cpascual,项目名称:taurus,代码行数:34,代码来源:taurusmessagepanel.py


示例20: testExternalMemFuncDestructor

    def testExternalMemFuncDestructor( self ):
        className = "myclassname"
        funcName = "~myDesctructorName"

        code = """  
        void %s::%s( char* param1 ) {
            return param1;
        };
        """ % ( className, funcName )
        highlight( code, self.lexer, self.formatter)
        myRTags = FakeRTags()
        test = CppSemantics( myRTags )
        
        for t,v,num in self.formatter.current_line:
            test.Feed( t,v,num )
        
        self.assertEqual( myRTags.GetClass()[0].name, className, 
        "Class Name did not match %s" % myRTags.GetClass()[0].name )

        mf = myRTags.GetClass()[0].GetElements()[0]

        self.assertEqual( mf['method'][0].GetName(), funcName, 
        "Function Name %s did not match %s" % (mf['method'][0].GetName(), funcName) )

        self.assertEqual( test.state, 'start', 
        "Semantics not in correct state '%s'" % ( test.state ))
开发者ID:2015E8007361074,项目名称:wxPython,代码行数:26,代码来源:testCppSemantics.py



注:本文中的pygments.highlight函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python pygments.lex函数代码示例发布时间:2022-05-25
下一篇:
Python pygments.format函数代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap