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'] |
| 5 | |
| 6 | import keyword, tokenize, cgi, functools |
| 7 | |
| 8 | def insert(s, i, text): |
| 9 | 'Insert text at position i in string s' |
| 10 | return s[:i] + text + s[i:] |
| 11 | |
| 12 | def is_builtin(s): |
| 13 | 'Return True if s is the name of a builtin' |
| 14 | return s in vars(__builtins__) |
| 15 | |
| 16 | def colorize(source): |
| 17 | 'Convert Python source code to an HTML fragment with colorized markup' |
| 18 | text = cgi.escape(source) |
| 19 | lines = text.splitlines(True) |
| 20 | readline = functools.partial(next, iter(lines), '') |
| 21 | actions = [] |
| 22 | kind = tok_str = '' |
| 23 | tok_type = tokenize.COMMENT |
| 24 | for tok in tokenize.generate_tokens(readline): |
| 25 | prev_tok_type, prev_tok_str = tok_type, tok_str |
| 26 | tok_type, tok_str, (srow, scol), (erow, ecol), logical_lineno = tok |
| 27 | kind, prev_kind = '', kind |
| 28 | if tok_type == tokenize.COMMENT: |
| 29 | kind = 'comment' |
| 30 | elif tok_type == tokenize.OP: |
| 31 | kind = 'operator' |
| 32 | elif tok_type == tokenize.STRING: |
| 33 | kind = 'string' |
| 34 | if prev_tok_type == tokenize.INDENT or scol==0: |
| 35 | kind = 'docstring' |
| 36 | elif tok_type == tokenize.NAME: |
| 37 | if tok_str in ('def', 'class', 'import', 'from'): |
| 38 | kind = 'definition' |
| 39 | elif prev_tok_str in ('def', 'class'): |
| 40 | kind = 'defname' |
| 41 | elif keyword.iskeyword(tok_str): |
| 42 | kind = 'keyword' |
| 43 | elif is_builtin(tok_str) and prev_tok_str != '.': |
| 44 | kind = 'builtin' |
| 45 | if kind: |
| 46 | actions.append(((srow, scol), (erow, ecol), kind)) |
| 47 | |
| 48 | for (srow, scol), (erow, ecol), kind in reversed(actions): |
| 49 | lines[erow-1] = insert(lines[erow-1], ecol, '</span>') |
| 50 | lines[srow-1] = insert(lines[srow-1], scol, '<span class="%s">' % kind) |
| 51 | |
| 52 | lines.insert(0, '<pre class="python">\n') |
| 53 | lines.append('</pre>\n') |
| 54 | return ''.join(lines) |
| 55 | |
| 56 | default_css = { |
| 57 | '.comment': '{color: crimson;}', |
| 58 | '.string': '{color: forestgreen;}', |
| 59 | '.docstring': '{color: forestgreen; font-style:italic}', |
| 60 | '.keyword': '{color: darkorange;}', |
| 61 | '.builtin': '{color: purple;}', |
| 62 | '.definition': '{color: darkorange; font-weight:bold;}', |
| 63 | '.defname': '{color: blue;}', |
| 64 | '.operator': '{color: brown;}', |
| 65 | } |
| 66 | |
| 67 | default_html = '''\ |
| 68 | <html><head><style type="text/css"> |
| 69 | %s |
| 70 | </style></head> |
| 71 | <body> |
| 72 | %s |
| 73 | </body></html> |
| 74 | ''' |
| 75 | |
| 76 | def build_page(source, html=default_html, css=default_css): |
| 77 | 'Create a complete HTML page with colorized Python source code' |
| 78 | css_str = ''.join(['%s %s\n' % item for item in default_css.items()]) |
| 79 | result = colorize(source) |
| 80 | return html % (css_str, result) |
| 81 | |
| 82 | |
| 83 | if __name__ == '__main__': |
| 84 | import sys, argparse, webbrowser, os |
| 85 | |
| 86 | parser = argparse.ArgumentParser( |
| 87 | description = 'Convert Python source code to colorized HTML') |
| 88 | parser.add_argument('sourcefile', metavar = 'SOURCEFILE', nargs = 1, |
| 89 | help = 'File containing Python sourcecode') |
| 90 | parser.add_argument('-b', '--browser', action = 'store_true', |
| 91 | help = 'launch a browser to show results') |
| 92 | parser.add_argument('-s', '--standalone', action = 'store_true', |
| 93 | help = 'show a standalone snippet rather than a complete webpage') |
| 94 | args = parser.parse_args() |
| 95 | if args.browser and args.standalone: |
| 96 | parser.error('The -s/--standalone option is incompatible with ' |
| 97 | 'the -b/--browser option') |
| 98 | |
| 99 | sourcefile = args.sourcefile[0] |
| 100 | with open(sourcefile) as f: |
| 101 | page = f.read() |
| 102 | html = colorize(page) if args.standalone else build_page(page) |
| 103 | if args.browser: |
| 104 | htmlfile = os.path.splitext(os.path.basename(sourcefile))[0] + '.html' |
| 105 | with open(htmlfile, 'w') as f: |
| 106 | f.write(html) |
| 107 | webbrowser.open('file://' + os.path.abspath(htmlfile)) |
| 108 | else: |
| 109 | sys.stdout.write(html) |