"""Pretty-print Python code to colorized, hyperlinked html. In python, do: py2html.convert_files(['file1.py', 'file2.py', ...]) From the shell, do: python py2html.py *.py""" import re, string, time, os id = r'[a-zA-Z_][a-zA-Z_0-9]*' ## RE for a Python identifier g1, g2, g3, g4 = r'\1 \2 \3 \4'.split() ## groups for re.matches def b(text): return '%s' % text def i(text): return '%s' % text def color(rgb, text): return '%s' % (rgb, text) def link(url, anchor): return '%s' % (url, anchor) def hilite(text, bg="ffff00"): return '%s' % ( bg, text, text) def modulelink(module, baseurl=''): """Hyperlink to a module, either locally or on python.org""" if module+'.py' not in local_files: baseurl = 'http://www.python.org/doc/current/lib/module-' return link(baseurl+module+'.html', module) def importer(m): "Turn text such as 'utils, math, re' into a string of HTML links." modules = [modulelink(mod.strip()) for mod in m.group(2).split(',')] return (m.group(1) + ', '.join(modules) + m.group(3)) def find1(regex, str): return (re.findall(regex, str) or [' '])[0] def convert_files(filenames, local_filenames=None, tblfile='readme.htm'): "Convert files of python code to colorized HTML." global local_files local_files = local_filenames or filenames summary_table = {} for f in filenames: fulltext = '\n'.join(map(string.rstrip, open(f).readlines())) text = fulltext for (pattern, repl) in replacements: text = re.sub(pattern, repl, text) text = '<>
%s
<