blob: 85ce0919a26a40d72d780e0bcfbfd7cd70960a3c [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 Hettinger9b8ede62012-06-30 23:19:30 -07005__author__ = 'Raymond Hettinger'
Raymond Hettingerbc09cf12012-06-30 16:58:06 -07006
7import keyword, tokenize, cgi, functools
8
Raymond Hettingerbc09cf12012-06-30 16:58:06 -07009def is_builtin(s):
10 'Return True if s is the name of a builtin'
11 return s in vars(__builtins__)
12
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070013def escape_range(lines, start, end):
14 'Return escaped content from a range of lines between start and end'
15 (srow, scol), (erow, ecol) = start, end
16 if srow == erow:
17 rows = [lines[srow-1][scol:ecol]]
18 else:
19 rows = [lines[srow-1][scol:]] + lines[srow: erow-1] + [lines[erow-1][:ecol]]
20 return cgi.escape(''.join(rows)), end
21
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070022def colorize(source):
23 'Convert Python source code to an HTML fragment with colorized markup'
Raymond Hettingercf6eac42012-07-03 00:12:27 -070024 lines = source.splitlines(keepends=True)
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070025 lines.append('')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070026 readline = functools.partial(next, iter(lines), '')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070027 kind = tok_str = ''
28 tok_type = tokenize.COMMENT
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070029 written = (1, 0)
30 result = []
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070031 for tok in tokenize.generate_tokens(readline):
32 prev_tok_type, prev_tok_str = tok_type, tok_str
33 tok_type, tok_str, (srow, scol), (erow, ecol), logical_lineno = tok
Raymond Hettingercf6eac42012-07-03 00:12:27 -070034 kind = ''
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070035 if tok_type == tokenize.COMMENT:
36 kind = 'comment'
Raymond Hettingere4870b52012-07-01 00:37:05 -070037 elif tok_type == tokenize.OP and tok_str[:1] not in '{}[](),.:;':
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070038 kind = 'operator'
39 elif tok_type == tokenize.STRING:
40 kind = 'string'
41 if prev_tok_type == tokenize.INDENT or scol==0:
42 kind = 'docstring'
43 elif tok_type == tokenize.NAME:
44 if tok_str in ('def', 'class', 'import', 'from'):
45 kind = 'definition'
46 elif prev_tok_str in ('def', 'class'):
47 kind = 'defname'
48 elif keyword.iskeyword(tok_str):
49 kind = 'keyword'
50 elif is_builtin(tok_str) and prev_tok_str != '.':
51 kind = 'builtin'
52 if kind:
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070053 line_upto_token, written = escape_range(lines, written, (srow, scol))
54 line_thru_token, written = escape_range(lines, written, (erow, ecol))
55 result += [line_upto_token, '<span class="%s">' % kind,
56 line_thru_token, '</span>']
57 else:
58 line_thru_token, written = escape_range(lines, written, (erow, ecol))
59 result += [line_thru_token]
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070060
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070061 result.insert(0, '<pre class="python">\n')
62 result.append('</pre>\n')
63 return ''.join(result)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070064
65default_css = {
66 '.comment': '{color: crimson;}',
67 '.string': '{color: forestgreen;}',
68 '.docstring': '{color: forestgreen; font-style:italic}',
69 '.keyword': '{color: darkorange;}',
70 '.builtin': '{color: purple;}',
71 '.definition': '{color: darkorange; font-weight:bold;}',
72 '.defname': '{color: blue;}',
73 '.operator': '{color: brown;}',
74}
75
76default_html = '''\
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070077<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
78 "http://www.w3.org/TR/html4/strict.dtd">
79<html>
80<head>
81<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
Raymond Hettingerecea0fb2012-07-02 17:17:16 -070082<title> {title} </title>
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070083<style type="text/css">
Raymond Hettingerecea0fb2012-07-02 17:17:16 -070084{css}
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070085</style>
86</head>
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070087<body>
Raymond Hettingerecea0fb2012-07-02 17:17:16 -070088{body}
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070089</body>
90</html>
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070091'''
92
Raymond Hettinger9b8ede62012-06-30 23:19:30 -070093def build_page(source, title='python', css=default_css, html=default_html):
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070094 'Create a complete HTML page with colorized Python source code'
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070095 css_str = '\n'.join(['%s %s' % item for item in css.items()])
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070096 result = colorize(source)
Raymond Hettinger9b8ede62012-06-30 23:19:30 -070097 title = cgi.escape(title)
Raymond Hettingerecea0fb2012-07-02 17:17:16 -070098 return html.format(title=title, css=css_str, body=result)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070099
100
101if __name__ == '__main__':
102 import sys, argparse, webbrowser, os
103
104 parser = argparse.ArgumentParser(
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700105 description = 'Convert Python source code to colorized HTML')
106 parser.add_argument('sourcefile', metavar = 'SOURCEFILE',
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700107 help = 'File containing Python sourcecode')
108 parser.add_argument('-b', '--browser', action = 'store_true',
109 help = 'launch a browser to show results')
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700110 parser.add_argument('-s', '--section', action = 'store_true',
111 help = 'show an HTML section rather than a complete webpage')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700112 args = parser.parse_args()
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700113 if args.browser and args.section:
114 parser.error('The -s/--section option is incompatible with '
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700115 'the -b/--browser option')
116
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700117 sourcefile = args.sourcefile
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700118 with open(sourcefile) as f:
119 page = f.read()
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700120 html = colorize(page) if args.section else build_page(page, title=sourcefile)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700121 if args.browser:
122 htmlfile = os.path.splitext(os.path.basename(sourcefile))[0] + '.html'
123 with open(htmlfile, 'w') as f:
124 f.write(html)
125 webbrowser.open('file://' + os.path.abspath(htmlfile))
126 else:
127 sys.stdout.write(html)