好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

编写Python脚本使得web页面上的代码高亮显示

做了一个在线代码高亮的项目,强大的Python一如既往没让我失望,一个强大的Pygments模块可以对多种(很多)语言进行代码高亮

下面来介绍一下它:

首先安装很简单,使用easy_install来进行安装:

easy_install pygments

 

安装完后我们来使用,Python的简单不会让大家失望:

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

 

结果

'   

1 

查看更多关于编写Python脚本使得web页面上的代码高亮显示的详细内容...

  阅读:46次