搜索热词
下面是编程之家 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
将Python源代码导出成HTML文件并带语法高亮
## {{{ /recipes/578178/ (r11)
\Add Syntax highlighting to Python source code\
__all__ = [colorize,uild_page,default_css,default_html,ansi_colorize,default_ansi]
__author__ = Raymond Hettinger
import keyword,tokenize,cgi,functools
def is_builtin(s):
Return True if s is the name of a builtin
return s in vars(__builtins__)
def combine_range(lines,start,end):
Join content from a range of lines between start and end
(srow,scol),(erow,ecol) = start,end
if srow == erow:
rows = [lines[srow-1][scol:ecol]]
else:
rows = [lines[srow-1][scol:]] + lines[srow: erow-1] + [lines[erow-1][:ecol]]
return \.join(rows),end
def isolate_tokens(source):
Generate chunks of source and identify chunks to be highlighted
lines = source.splitlines(True)
lines.append(\)
readline = functools.partial(next,iter(lines),\)
kind = tok_str = \
tok_type = MENT
written = (1,0)
for tok in tokenize.generate_token