blob: c5c96110d82ccb283389b19a99e2120296be5c06 [file] [log] [blame]
Raymond Hettingerbc09cf12012-06-30 16:58:06 -07001#!/usr/bin/env python3
2'Convert Python source code to HTML with colorized markup'
3
4__all__ = ['colorize', 'build_page', 'default_css', 'default_html']
Raymond Hettinger461fcaa2012-06-30 17:00:14 -07005__author__ = 'Raymond Hettinger'
Raymond Hettingerbc09cf12012-06-30 16:58:06 -07006
7import keyword, tokenize, cgi, functools
8
9def insert(s, i, text):
10 'Insert text at position i in string s'
11 return s[:i] + text + s[i:]
12
13def is_builtin(s):
14 'Return True if s is the name of a builtin'
15 return s in vars(__builtins__)
16
17def colorize(source):
18 'Convert Python source code to an HTML fragment with colorized markup'
19 text = cgi.escape(source)
20 lines = text.splitlines(True)
21 readline = functools.partial(next, iter(lines), '')
22 actions = []
23 kind = tok_str = ''
24 tok_type = tokenize.COMMENT
25 for tok in tokenize.generate_tokens(readline):
26 prev_tok_type, prev_tok_str = tok_type, tok_str
27 tok_type, tok_str, (srow, scol), (erow, ecol), logical_lineno = tok
28 kind, prev_kind = '', kind
29 if tok_type == tokenize.COMMENT:
30 kind = 'comment'
31 elif tok_type == tokenize.OP:
32 kind = 'operator'
33 elif tok_type == tokenize.STRING:
34 kind = 'string'
35 if prev_tok_type == tokenize.INDENT or scol==0:
36 kind = 'docstring'
37 elif tok_type == tokenize.NAME:
38 if tok_str in ('def', 'class', 'import', 'from'):
39 kind = 'definition'
40 elif prev_tok_str in ('def', 'class'):
41 kind = 'defname'
42 elif keyword.iskeyword(tok_str):
43 kind = 'keyword'
44 elif is_builtin(tok_str) and prev_tok_str != '.':
45 kind = 'builtin'
46 if kind:
47 actions.append(((srow, scol), (erow, ecol), kind))
48
49 for (srow, scol), (erow, ecol), kind in reversed(actions):
50 lines[erow-1] = insert(lines[erow-1], ecol, '</span>')
51 lines[srow-1] = insert(lines[srow-1], scol, '<span class="%s">' % kind)
52
53 lines.insert(0, '<pre class="python">\n')
54 lines.append('</pre>\n')
55 return ''.join(lines)
56
57default_css = {
58 '.comment': '{color: crimson;}',
59 '.string': '{color: forestgreen;}',
60 '.docstring': '{color: forestgreen; font-style:italic}',
61 '.keyword': '{color: darkorange;}',
62 '.builtin': '{color: purple;}',
63 '.definition': '{color: darkorange; font-weight:bold;}',
64 '.defname': '{color: blue;}',
65 '.operator': '{color: brown;}',
66}
67
68default_html = '''\
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070069<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
70 "http://www.w3.org/TR/html4/strict.dtd">
71<html>
72<head>
73<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
74<title> Python Code </title>
75<style type="text/css">
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070076%s
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070077</style>
78</head>
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070079<body>
80%s
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070081</body>
82</html>
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070083'''
84
85def build_page(source, html=default_html, css=default_css):
86 'Create a complete HTML page with colorized Python source code'
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070087 css_str = '\n'.join(['%s %s' % item for item in css.items()])
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070088 result = colorize(source)
89 return html % (css_str, result)
90
91
92if __name__ == '__main__':
93 import sys, argparse, webbrowser, os
94
95 parser = argparse.ArgumentParser(
96 description = 'Convert Python source code to colorized HTML')
97 parser.add_argument('sourcefile', metavar = 'SOURCEFILE', nargs = 1,
98 help = 'File containing Python sourcecode')
99 parser.add_argument('-b', '--browser', action = 'store_true',
100 help = 'launch a browser to show results')
101 parser.add_argument('-s', '--standalone', action = 'store_true',
102 help = 'show a standalone snippet rather than a complete webpage')
103 args = parser.parse_args()
104 if args.browser and args.standalone:
105 parser.error('The -s/--standalone option is incompatible with '
106 'the -b/--browser option')
107
108 sourcefile = args.sourcefile[0]
109 with open(sourcefile) as f:
110 page = f.read()
111 html = colorize(page) if args.standalone else build_page(page)
112 if args.browser:
113 htmlfile = os.path.splitext(os.path.basename(sourcefile))[0] + '.html'
114 with open(htmlfile, 'w') as f:
115 f.write(html)
116 webbrowser.open('file://' + os.path.abspath(htmlfile))
117 else:
118 sys.stdout.write(html)