blob: 88b709ff298cca5fb0fc53a93441d05efd8c5483 [file] [log] [blame]
Raymond Hettingerbc09cf12012-06-30 16:58:06 -07001#!/usr/bin/env python3
Raymond Hettinger3a961612012-07-03 14:11:40 -07002'Add syntax highlighting to Python source code'
Raymond Hettingerbc09cf12012-06-30 16:58:06 -07003
Raymond Hettinger3a961612012-07-03 14:11:40 -07004__all__ = ['colorize', 'build_page', 'default_css', 'default_html',
5 'ansi_colorize', 'default_ansi']
Raymond Hettinger9b8ede62012-06-30 23:19:30 -07006__author__ = 'Raymond Hettinger'
Raymond Hettingerbc09cf12012-06-30 16:58:06 -07007
8import keyword, tokenize, cgi, functools
9
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070010def is_builtin(s):
11 'Return True if s is the name of a builtin'
12 return s in vars(__builtins__)
13
Raymond Hettinger5da60392012-07-03 13:13:52 -070014def combine_range(lines, start, end):
15 'Join content from a range of lines between start and end'
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070016 (srow, scol), (erow, ecol) = start, end
17 if srow == erow:
18 rows = [lines[srow-1][scol:ecol]]
19 else:
20 rows = [lines[srow-1][scol:]] + lines[srow: erow-1] + [lines[erow-1][:ecol]]
Raymond Hettinger5da60392012-07-03 13:13:52 -070021 return ''.join(rows), end
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070022
Raymond Hettinger5da60392012-07-03 13:13:52 -070023def isolate_tokens(source):
24 'Generate chunks of source and indentify chunks to be highlighted'
Raymond Hettingerac5f8462012-07-03 00:15:59 -070025 lines = source.splitlines(True)
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070026 lines.append('')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070027 readline = functools.partial(next, iter(lines), '')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070028 kind = tok_str = ''
29 tok_type = tokenize.COMMENT
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070030 written = (1, 0)
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'
Raymond Hettinger5da60392012-07-03 13:13:52 -070052 line_upto_token, written = combine_range(lines, written, (srow, scol))
53 line_thru_token, written = combine_range(lines, written, (erow, ecol))
54 yield kind, line_upto_token, line_thru_token
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070055
Raymond Hettinger3a961612012-07-03 14:11:40 -070056default_ansi = {
57 'comment': '\033[0;31m',
58 'string': '\033[0;32m',
59 'docstring': '\033[0;32m',
60 'keyword': '\033[0;33m',
61 'builtin': '\033[0;35m',
62 'definition': '\033[0;33m',
63 'defname': '\033[0;34m',
64 'operator': '\033[0;33m',
65}
66
67def colorize_ansi(source, colors=default_ansi):
68 'Add syntax highlighting to Python source code using ANSI escape sequences'
69 # http://en.wikipedia.org/wiki/ANSI_escape_code
70 result = []
71 for kind, line_upto_token, line_thru_token in isolate_tokens(source):
72 if kind:
73 result += [line_upto_token, colors[kind], line_thru_token, '\033[0m']
74 else:
75 result += [line_upto_token, line_thru_token]
76 return ''.join(result)
77
78def colorize_html(source):
Raymond Hettinger5da60392012-07-03 13:13:52 -070079 'Convert Python source code to an HTML fragment with colorized markup'
80 result = ['<pre class="python">\n']
81 for kind, line_upto_token, line_thru_token in isolate_tokens(source):
82 if kind:
83 result += [cgi.escape(line_upto_token),
84 '<span class="%s">' % kind,
85 cgi.escape(line_thru_token),
86 '</span>']
87 else:
88 result += [cgi.escape(line_upto_token),
89 cgi.escape(line_thru_token)]
90 result += ['</pre>\n']
Raymond Hettingerf2cc3522012-07-02 13:29:57 -070091 return ''.join(result)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070092
93default_css = {
94 '.comment': '{color: crimson;}',
95 '.string': '{color: forestgreen;}',
Raymond Hettinger5da60392012-07-03 13:13:52 -070096 '.docstring': '{color: forestgreen; font-style:italic;}',
Raymond Hettingerbc09cf12012-06-30 16:58:06 -070097 '.keyword': '{color: darkorange;}',
98 '.builtin': '{color: purple;}',
99 '.definition': '{color: darkorange; font-weight:bold;}',
100 '.defname': '{color: blue;}',
101 '.operator': '{color: brown;}',
102}
103
104default_html = '''\
Raymond Hettingerfd490cc2012-06-30 22:19:04 -0700105<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
106 "http://www.w3.org/TR/html4/strict.dtd">
107<html>
108<head>
109<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
Raymond Hettingerecea0fb2012-07-02 17:17:16 -0700110<title> {title} </title>
Raymond Hettingerfd490cc2012-06-30 22:19:04 -0700111<style type="text/css">
Raymond Hettingerecea0fb2012-07-02 17:17:16 -0700112{css}
Raymond Hettingerfd490cc2012-06-30 22:19:04 -0700113</style>
114</head>
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700115<body>
Raymond Hettingerecea0fb2012-07-02 17:17:16 -0700116{body}
Raymond Hettingerfd490cc2012-06-30 22:19:04 -0700117</body>
118</html>
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700119'''
120
Raymond Hettinger9b8ede62012-06-30 23:19:30 -0700121def build_page(source, title='python', css=default_css, html=default_html):
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700122 'Create a complete HTML page with colorized Python source code'
Raymond Hettingerfd490cc2012-06-30 22:19:04 -0700123 css_str = '\n'.join(['%s %s' % item for item in css.items()])
Raymond Hettinger3a961612012-07-03 14:11:40 -0700124 result = colorize_html(source)
Raymond Hettinger9b8ede62012-06-30 23:19:30 -0700125 title = cgi.escape(title)
Raymond Hettingerecea0fb2012-07-02 17:17:16 -0700126 return html.format(title=title, css=css_str, body=result)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700127
128
129if __name__ == '__main__':
130 import sys, argparse, webbrowser, os
131
132 parser = argparse.ArgumentParser(
Raymond Hettinger3a961612012-07-03 14:11:40 -0700133 description = 'Add syntax highlighting to Python source')
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700134 parser.add_argument('sourcefile', metavar = 'SOURCEFILE',
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700135 help = 'File containing Python sourcecode')
Raymond Hettinger3a961612012-07-03 14:11:40 -0700136 parser.add_argument('-a', '--ansi', action = 'store_true',
137 help = 'emit ANSI escape highlighted source')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700138 parser.add_argument('-b', '--browser', action = 'store_true',
139 help = 'launch a browser to show results')
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700140 parser.add_argument('-s', '--section', action = 'store_true',
141 help = 'show an HTML section rather than a complete webpage')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700142 args = parser.parse_args()
Raymond Hettinger3a961612012-07-03 14:11:40 -0700143
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700144 if args.browser and args.section:
145 parser.error('The -s/--section option is incompatible with '
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700146 'the -b/--browser option')
Raymond Hettinger3a961612012-07-03 14:11:40 -0700147 if args.ansi and (args.browser or args.section):
148 parser.error('The -a/--ansi option is incompatible with '
149 'the -b/--browser and -s/--section options')
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700150
Raymond Hettingercf6eac42012-07-03 00:12:27 -0700151 sourcefile = args.sourcefile
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700152 with open(sourcefile) as f:
153 page = f.read()
Raymond Hettinger3a961612012-07-03 14:11:40 -0700154
155 if args.ansi:
156 encoded = colorize_ansi(page)
157 elif args.section:
158 encoded = colorize_html(page)
159 else:
160 encoded = build_page(page, title=sourcefile)
161
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700162 if args.browser:
163 htmlfile = os.path.splitext(os.path.basename(sourcefile))[0] + '.html'
164 with open(htmlfile, 'w') as f:
Raymond Hettinger3a961612012-07-03 14:11:40 -0700165 f.write(encoded)
Raymond Hettingerbc09cf12012-06-30 16:58:06 -0700166 webbrowser.open('file://' + os.path.abspath(htmlfile))
167 else:
Raymond Hettinger3a961612012-07-03 14:11:40 -0700168 sys.stdout.write(encoded)