Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Raymond Hettinger | 0712f40 | 2012-07-03 14:42:33 -0700 | [diff] [blame] | 2 | '''Add syntax highlighting to Python source code |
| 3 | |
| 4 | Example command-line calls: |
| 5 | |
| 6 | # Show syntax highlighted code in the terminal window |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame^] | 7 | $ ./highlight.py myfile.py |
Raymond Hettinger | 0712f40 | 2012-07-03 14:42:33 -0700 | [diff] [blame] | 8 | |
| 9 | # Colorize myfile.py and display in a browser |
| 10 | $ ./highlight.py -b myfile.py |
| 11 | |
| 12 | # Create an HTML section that can be embedded in an existing webpage |
| 13 | ./highlight.py -s myfile.py |
| 14 | |
| 15 | # Create a complete HTML file |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame^] | 16 | $ ./highlight.py -c myfile.py > myfile.html |
Raymond Hettinger | 0712f40 | 2012-07-03 14:42:33 -0700 | [diff] [blame] | 17 | |
| 18 | ''' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 19 | |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 20 | __all__ = ['colorize', 'build_page', 'default_css', 'default_html', |
| 21 | 'ansi_colorize', 'default_ansi'] |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 22 | __author__ = 'Raymond Hettinger' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 23 | |
| 24 | import keyword, tokenize, cgi, functools |
| 25 | |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 26 | def is_builtin(s): |
| 27 | 'Return True if s is the name of a builtin' |
| 28 | return s in vars(__builtins__) |
| 29 | |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 30 | def combine_range(lines, start, end): |
| 31 | 'Join content from a range of lines between start and end' |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 32 | (srow, scol), (erow, ecol) = start, end |
| 33 | if srow == erow: |
| 34 | rows = [lines[srow-1][scol:ecol]] |
| 35 | else: |
| 36 | rows = [lines[srow-1][scol:]] + lines[srow: erow-1] + [lines[erow-1][:ecol]] |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 37 | return ''.join(rows), end |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 38 | |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 39 | def isolate_tokens(source): |
Raymond Hettinger | 1087d9c | 2012-07-03 14:25:16 -0700 | [diff] [blame] | 40 | 'Generate chunks of source and identify chunks to be highlighted' |
Raymond Hettinger | ac5f846 | 2012-07-03 00:15:59 -0700 | [diff] [blame] | 41 | lines = source.splitlines(True) |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 42 | lines.append('') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 43 | readline = functools.partial(next, iter(lines), '') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 44 | kind = tok_str = '' |
| 45 | tok_type = tokenize.COMMENT |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 46 | written = (1, 0) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 47 | for tok in tokenize.generate_tokens(readline): |
| 48 | prev_tok_type, prev_tok_str = tok_type, tok_str |
| 49 | tok_type, tok_str, (srow, scol), (erow, ecol), logical_lineno = tok |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 50 | kind = '' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 51 | if tok_type == tokenize.COMMENT: |
| 52 | kind = 'comment' |
Raymond Hettinger | e4870b5 | 2012-07-01 00:37:05 -0700 | [diff] [blame] | 53 | elif tok_type == tokenize.OP and tok_str[:1] not in '{}[](),.:;': |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 54 | kind = 'operator' |
| 55 | elif tok_type == tokenize.STRING: |
| 56 | kind = 'string' |
| 57 | if prev_tok_type == tokenize.INDENT or scol==0: |
| 58 | kind = 'docstring' |
| 59 | elif tok_type == tokenize.NAME: |
| 60 | if tok_str in ('def', 'class', 'import', 'from'): |
| 61 | kind = 'definition' |
| 62 | elif prev_tok_str in ('def', 'class'): |
| 63 | kind = 'defname' |
| 64 | elif keyword.iskeyword(tok_str): |
| 65 | kind = 'keyword' |
| 66 | elif is_builtin(tok_str) and prev_tok_str != '.': |
| 67 | kind = 'builtin' |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 68 | line_upto_token, written = combine_range(lines, written, (srow, scol)) |
| 69 | line_thru_token, written = combine_range(lines, written, (erow, ecol)) |
| 70 | yield kind, line_upto_token, line_thru_token |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 71 | |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 72 | default_ansi = { |
| 73 | 'comment': '\033[0;31m', |
| 74 | 'string': '\033[0;32m', |
| 75 | 'docstring': '\033[0;32m', |
| 76 | 'keyword': '\033[0;33m', |
| 77 | 'builtin': '\033[0;35m', |
| 78 | 'definition': '\033[0;33m', |
| 79 | 'defname': '\033[0;34m', |
| 80 | 'operator': '\033[0;33m', |
| 81 | } |
| 82 | |
| 83 | def colorize_ansi(source, colors=default_ansi): |
| 84 | 'Add syntax highlighting to Python source code using ANSI escape sequences' |
| 85 | # http://en.wikipedia.org/wiki/ANSI_escape_code |
| 86 | result = [] |
| 87 | for kind, line_upto_token, line_thru_token in isolate_tokens(source): |
| 88 | if kind: |
| 89 | result += [line_upto_token, colors[kind], line_thru_token, '\033[0m'] |
| 90 | else: |
| 91 | result += [line_upto_token, line_thru_token] |
| 92 | return ''.join(result) |
| 93 | |
| 94 | def colorize_html(source): |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 95 | 'Convert Python source code to an HTML fragment with colorized markup' |
| 96 | result = ['<pre class="python">\n'] |
| 97 | for kind, line_upto_token, line_thru_token in isolate_tokens(source): |
| 98 | if kind: |
| 99 | result += [cgi.escape(line_upto_token), |
| 100 | '<span class="%s">' % kind, |
| 101 | cgi.escape(line_thru_token), |
| 102 | '</span>'] |
| 103 | else: |
| 104 | result += [cgi.escape(line_upto_token), |
| 105 | cgi.escape(line_thru_token)] |
| 106 | result += ['</pre>\n'] |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 107 | return ''.join(result) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 108 | |
| 109 | default_css = { |
| 110 | '.comment': '{color: crimson;}', |
| 111 | '.string': '{color: forestgreen;}', |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 112 | '.docstring': '{color: forestgreen; font-style:italic;}', |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 113 | '.keyword': '{color: darkorange;}', |
| 114 | '.builtin': '{color: purple;}', |
| 115 | '.definition': '{color: darkorange; font-weight:bold;}', |
| 116 | '.defname': '{color: blue;}', |
| 117 | '.operator': '{color: brown;}', |
| 118 | } |
| 119 | |
| 120 | default_html = '''\ |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 121 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 122 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 123 | <html> |
| 124 | <head> |
| 125 | <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 126 | <title> {title} </title> |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 127 | <style type="text/css"> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 128 | {css} |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 129 | </style> |
| 130 | </head> |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 131 | <body> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 132 | {body} |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 133 | </body> |
| 134 | </html> |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 135 | ''' |
| 136 | |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 137 | def build_page(source, title='python', css=default_css, html=default_html): |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 138 | 'Create a complete HTML page with colorized Python source code' |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 139 | css_str = '\n'.join(['%s %s' % item for item in css.items()]) |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 140 | result = colorize_html(source) |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 141 | title = cgi.escape(title) |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 142 | return html.format(title=title, css=css_str, body=result) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 143 | |
| 144 | |
| 145 | if __name__ == '__main__': |
| 146 | import sys, argparse, webbrowser, os |
| 147 | |
| 148 | parser = argparse.ArgumentParser( |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 149 | description = 'Add syntax highlighting to Python source') |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 150 | parser.add_argument('sourcefile', metavar = 'SOURCEFILE', |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 151 | help = 'File containing Python sourcecode') |
| 152 | parser.add_argument('-b', '--browser', action = 'store_true', |
| 153 | help = 'launch a browser to show results') |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame^] | 154 | parser.add_argument('-c', '--complete', action = 'store_true', |
| 155 | help = 'build a complete html webpage') |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 156 | parser.add_argument('-s', '--section', action = 'store_true', |
| 157 | help = 'show an HTML section rather than a complete webpage') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 158 | args = parser.parse_args() |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 159 | |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame^] | 160 | if args.section and (args.browser or args.complete): |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 161 | parser.error('The -s/--section option is incompatible with ' |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame^] | 162 | 'the -b/--browser or -c/--complete options') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 163 | |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 164 | sourcefile = args.sourcefile |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 165 | with open(sourcefile) as f: |
Raymond Hettinger | 0712f40 | 2012-07-03 14:42:33 -0700 | [diff] [blame] | 166 | source = f.read() |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 167 | |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame^] | 168 | if args.complete or args.browser: |
| 169 | encoded = build_page(source, title=sourcefile) |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 170 | elif args.section: |
Raymond Hettinger | 0712f40 | 2012-07-03 14:42:33 -0700 | [diff] [blame] | 171 | encoded = colorize_html(source) |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 172 | else: |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame^] | 173 | encoded = colorize_ansi(source) |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 174 | |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 175 | if args.browser: |
| 176 | htmlfile = os.path.splitext(os.path.basename(sourcefile))[0] + '.html' |
| 177 | with open(htmlfile, 'w') as f: |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 178 | f.write(encoded) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 179 | webbrowser.open('file://' + os.path.abspath(htmlfile)) |
| 180 | else: |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 181 | sys.stdout.write(encoded) |