亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

編寫Python腳本使得web頁面上的代碼高亮顯示

 更新時(shí)間:2015年04月24日 11:19:43   作者:C Wong  
這篇文章主要介紹了編寫Python腳本使得web頁面上的代碼高亮顯示,主要使用了pygments工具,需要的朋友可以參考下

做了一個(gè)在線代碼高亮的項(xiàng)目,強(qiáng)大的Python一如既往沒讓我失望,一個(gè)強(qiáng)大的Pygments模塊可以對(duì)多種(很多)語言進(jìn)行代碼高亮

下面來介紹一下它:

首先安裝很簡(jiǎn)單,使用easy_install來進(jìn)行安裝:

easy_install pygments

安裝完后我們來使用,Python的簡(jiǎn)單不會(huì)讓大家失望:

from pygments.lexers import PythonLexver
from pygments.formatters import HtmlFormatter
from pygments import highlight

formatter = HtmlFormatter(encoding='utf-8', style = 'emacs', linenos = True)
code = highlight('print "hello, world"', PythonLexer(), formatter)

print code

結(jié)果

'<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre><span class="k">print</span> <span class="s">&quot;hello, world&quot;</span>\n</pre></div>\n</td></tr></table>'

這樣就簡(jiǎn)單的對(duì)代碼進(jìn)行了高亮,當(dāng)然如果你做了上面操作,然后把內(nèi)容輸入到一個(gè)文件里查看,肯定大呼坑爹,因?yàn)楦緵]高亮,因?yàn)槟J(rèn)是不會(huì)輸出css的 我們還要獲取css加入到html中去:

css = formatter.get_style_defs()

然后把css內(nèi)容和上面的html一起寫入到html文件就可以看到高亮的代碼了(千萬不要告訴我你不知道css應(yīng)該放在什么位置)

相關(guān)文章

最新評(píng)論