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 | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 13 | def 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 Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 22 | def colorize(source): |
| 23 | 'Convert Python source code to an HTML fragment with colorized markup' |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame^] | 24 | lines = source.splitlines(keepends=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) |
| 30 | result = [] |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 31 | 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 Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame^] | 34 | kind = '' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 35 | if tok_type == tokenize.COMMENT: |
| 36 | kind = 'comment' |
Raymond Hettinger | e4870b5 | 2012-07-01 00:37:05 -0700 | [diff] [blame] | 37 | elif tok_type == tokenize.OP and tok_str[:1] not in '{}[](),.:;': |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 38 | 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 Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 53 | 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 Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 60 | |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 61 | result.insert(0, '<pre class="python">\n') |
| 62 | result.append('</pre>\n') |
| 63 | return ''.join(result) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 64 | |
| 65 | default_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 | |
| 76 | default_html = '''\ |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 77 | <!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 Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 82 | <title> {title} </title> |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 83 | <style type="text/css"> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 84 | {css} |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 85 | </style> |
| 86 | </head> |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 87 | <body> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 88 | {body} |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 89 | </body> |
| 90 | </html> |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 91 | ''' |
| 92 | |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 93 | def build_page(source, title='python', css=default_css, html=default_html): |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 94 | 'Create a complete HTML page with colorized Python source code' |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 95 | css_str = '\n'.join(['%s %s' % item for item in css.items()]) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 96 | result = colorize(source) |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 97 | title = cgi.escape(title) |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 98 | return html.format(title=title, css=css_str, body=result) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 99 | |
| 100 | |
| 101 | if __name__ == '__main__': |
| 102 | import sys, argparse, webbrowser, os |
| 103 | |
| 104 | parser = argparse.ArgumentParser( |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame^] | 105 | description = 'Convert Python source code to colorized HTML') |
| 106 | parser.add_argument('sourcefile', metavar = 'SOURCEFILE', |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 107 | help = 'File containing Python sourcecode') |
| 108 | parser.add_argument('-b', '--browser', action = 'store_true', |
| 109 | help = 'launch a browser to show results') |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame^] | 110 | parser.add_argument('-s', '--section', action = 'store_true', |
| 111 | help = 'show an HTML section rather than a complete webpage') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 112 | args = parser.parse_args() |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame^] | 113 | if args.browser and args.section: |
| 114 | parser.error('The -s/--section option is incompatible with ' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 115 | 'the -b/--browser option') |
| 116 | |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame^] | 117 | sourcefile = args.sourcefile |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 118 | with open(sourcefile) as f: |
| 119 | page = f.read() |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame^] | 120 | html = colorize(page) if args.section else build_page(page, title=sourcefile) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 121 | 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) |