blob: b2cef3daa71d3485fa4cd5d57d335c30cd56f87d [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 Hettinger5da60392012-07-03 13:13:52 -070013def combine_range(lines, start, end):
14 'Join content from a range of lines between start and end'
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070015 (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]]
Raymond Hettinger5da60392012-07-03 13:13:52 -070020 return ''.join(rows), end
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070021
Raymond Hettinger5da60392012-07-03 13:13:52 -070022def isolate_tokens(source):
23 'Generate chunks of source and indentify chunks to be highlighted'
Raymond Hettingerac5f8462012-07-03 00:15:59 -070024 lines = source.splitlines(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)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070030 for tok in tokenize.generate_tokens(readline):
31 prev_tok_type, prev_tok_str = tok_type, tok_str
32 tok_type, tok_str, (srow, scol), (erow, ecol), logical_lineno = tok
Raymond Hettingercf6eac42012-07-03 00:12:27 -070033 kind = ''
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070034 if tok_type == tokenize.COMMENT:
35 kind = 'comment'
Raymond Hettingere4870b52012-07-01 00:37:05 -070036 elif tok_type == tokenize.OP and tok_str[:1] not in '{}[](),.:;':
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070037 kind = 'operator'
38 elif tok_type == tokenize.STRING:
39 kind = 'string'
40 if prev_tok_type == tokenize.INDENT or scol==0:
41 kind = 'docstring'
42 elif tok_type == tokenize.NAME:
43 if tok_str in ('def', 'class', 'import', 'from'):
44 kind = 'definition'
45 elif prev_tok_str in ('def', 'class'):
46 kind = 'defname'
47 elif keyword.iskeyword(tok_str):
48 kind = 'keyword'
49 elif is_builtin(tok_str) and prev_tok_str != '.':
50 kind = 'builtin'
Raymond Hettinger5da60392012-07-03 13:13:52 -070051 line_upto_token, written = combine_range(lines, written, (srow, scol))
52 line_thru_token, written = combine_range(lines, written, (erow, ecol))
53 yield kind, line_upto_token, line_thru_token
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070054
Raymond Hettinger5da60392012-07-03 13:13:52 -070055def colorize(source):
56 'Convert Python source code to an HTML fragment with colorized markup'
57 result = ['<pre class="python">\n']
58 for kind, line_upto_token, line_thru_token in isolate_tokens(source):
59 if kind:
60 result += [cgi.escape(line_upto_token),
61 '<span class="%s">' % kind,
62 cgi.escape(line_thru_token),
63 '</span>']
64 else:
65 result += [cgi.escape(line_upto_token),
66 cgi.escape(line_thru_token)]
67 result += ['</pre>\n']
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070068 return ''.join(result)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070069
70default_css = {
71 '.comment': '{color: crimson;}',
72 '.string': '{color: forestgreen;}',
Raymond Hettinger5da60392012-07-03 13:13:52 -070073 '.docstring': '{color: forestgreen; font-style:italic;}',
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070074 '.keyword': '{color: darkorange;}',
75 '.builtin': '{color: purple;}',
76 '.definition': '{color: darkorange; font-weight:bold;}',
77 '.defname': '{color: blue;}',
78 '.operator': '{color: brown;}',
79}
80
81default_html = '''\
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070082<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
83 "http://www.w3.org/TR/html4/strict.dtd">
84<html>
85<head>
86<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
Raymond Hettingerecea0fb2012-07-02 17:17:16 -070087<title> {title} </title>
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070088<style type="text/css">
Raymond Hettingerecea0fb2012-07-02 17:17:16 -070089{css}
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070090</style>
91</head>
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070092<body>
Raymond Hettingerecea0fb2012-07-02 17:17:16 -070093{body}
Raymond Hettingerfd490cc2012-06-30 22:19:04 -070094</body>
95</html>
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070096'''
97
Raymond Hettinger9b8ede62012-06-30 23:19:30 -070098def build_page(source, title='python', css=default_css, html=default_html):
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070099 'Create a complete HTML page with colorized Python source code'
Raymond Hettingerfd490cc2012-06-30 22:19:04 -0700100 css_str = '\n'.join(['%s %s' % item for item in css.items()])
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700101 result = colorize(source)
Raymond Hettinger9b8ede62012-06-30 23:19:30 -0700102 title = cgi.escape(title)
Raymond Hettingerecea0fb2012-07-02 17:17:16 -0700103 return html.format(title=title, css=css_str, body=result)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700104
105
106if __name__ == '__main__':
107 import sys, argparse, webbrowser, os
108
109 parser = argparse.ArgumentParser(
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700110 description = 'Convert Python source code to colorized HTML')
111 parser.add_argument('sourcefile', metavar = 'SOURCEFILE',
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700112 help = 'File containing Python sourcecode')
113 parser.add_argument('-b', '--browser', action = 'store_true',
114 help = 'launch a browser to show results')
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700115 parser.add_argument('-s', '--section', action = 'store_true',
116 help = 'show an HTML section rather than a complete webpage')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700117 args = parser.parse_args()
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700118 if args.browser and args.section:
119 parser.error('The -s/--section option is incompatible with '
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700120 'the -b/--browser option')
121
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700122 sourcefile = args.sourcefile
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700123 with open(sourcefile) as f:
124 page = f.read()
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700125 html = colorize(page) if args.section else build_page(page, title=sourcefile)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700126 if args.browser:
127 htmlfile = os.path.splitext(os.path.basename(sourcefile))[0] + '.html'
128 with open(htmlfile, 'w') as f:
129 f.write(html)
130 webbrowser.open('file://' + os.path.abspath(htmlfile))
131 else:
132 sys.stdout.write(html)