使用pygments进行代码高亮

简单例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
def get_hightlight_src(code,srcname,linenos=False,stripall=True):
try:
lexer = get_lexer_by_name(srcname, stripall=stripall)
except:
return code
formatter = HtmlFormatter(linenos=linenos, cssclass="source")
result = highlight(code, lexer, formatter)
return result

资料

吴羽舒 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!