Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 1 | #!/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 Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 5 | __author__ = 'Raymond Hettinger' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 6 | |
| 7 | import keyword, tokenize, cgi, functools |
| 8 | |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 9 | def is_builtin(s): |
| 10 | 'Return True if s is the name of a builtin' |
| 11 | return s in vars(__builtins__) |
| 12 | |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame^] | 13 | def combine_range(lines, start, end): |
| 14 | 'Join content from a range of lines between start and end' |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 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]] |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame^] | 20 | return ''.join(rows), end |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 21 | |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame^] | 22 | def isolate_tokens(source): |
| 23 | 'Generate chunks of source and indentify chunks to be highlighted' |
Raymond Hettinger | ac5f846 | 2012-07-03 00:15:59 -0700 | [diff] [blame] | 24 | lines = source.splitlines(True) |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 25 | lines.append('') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 26 | readline = functools.partial(next, iter(lines), '') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 27 | kind = tok_str = '' |
| 28 | tok_type = tokenize.COMMENT |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 29 | written = (1, 0) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 30 | 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 Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 33 | kind = '' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 34 | if tok_type == tokenize.COMMENT: |
| 35 | kind = 'comment' |
Raymond Hettinger | e4870b5 | 2012-07-01 00:37:05 -0700 | [diff] [blame] | 36 | elif tok_type == tokenize.OP and tok_str[:1] not in '{}[](),.:;': |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 37 | 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 Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame^] | 51 | 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 Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 54 | |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame^] | 55 | def 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 Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 68 | return ''.join(result) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 69 | |
| 70 | default_css = { |
| 71 | '.comment': '{color: crimson;}', |
| 72 | '.string': '{color: forestgreen;}', |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame^] | 73 | '.docstring': '{color: forestgreen; font-style:italic;}', |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 74 | '.keyword': '{color: darkorange;}', |
| 75 | '.builtin': '{color: purple;}', |
| 76 | '.definition': '{color: darkorange; font-weight:bold;}', |
| 77 | '.defname': '{color: blue;}', |
| 78 | '.operator': '{color: brown;}', |
| 79 | } |
| 80 | |
| 81 | default_html = '''\ |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 82 | <!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 Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 87 | <title> {title} </title> |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 88 | <style type="text/css"> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 89 | {css} |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 90 | </style> |
| 91 | </head> |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 92 | <body> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 93 | {body} |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 94 | </body> |
| 95 | </html> |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 96 | ''' |
| 97 | |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 98 | def build_page(source, title='python', css=default_css, html=default_html): |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 99 | 'Create a complete HTML page with colorized Python source code' |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 100 | css_str = '\n'.join(['%s %s' % item for item in css.items()]) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 101 | result = colorize(source) |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 102 | title = cgi.escape(title) |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 103 | return html.format(title=title, css=css_str, body=result) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 104 | |
| 105 | |
| 106 | if __name__ == '__main__': |
| 107 | import sys, argparse, webbrowser, os |
| 108 | |
| 109 | parser = argparse.ArgumentParser( |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 110 | description = 'Convert Python source code to colorized HTML') |
| 111 | parser.add_argument('sourcefile', metavar = 'SOURCEFILE', |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 112 | help = 'File containing Python sourcecode') |
| 113 | parser.add_argument('-b', '--browser', action = 'store_true', |
| 114 | help = 'launch a browser to show results') |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 115 | parser.add_argument('-s', '--section', action = 'store_true', |
| 116 | help = 'show an HTML section rather than a complete webpage') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 117 | args = parser.parse_args() |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 118 | if args.browser and args.section: |
| 119 | parser.error('The -s/--section option is incompatible with ' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 120 | 'the -b/--browser option') |
| 121 | |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 122 | sourcefile = args.sourcefile |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 123 | with open(sourcefile) as f: |
| 124 | page = f.read() |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 125 | html = colorize(page) if args.section else build_page(page, title=sourcefile) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 126 | 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) |