Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 2 | '''Add syntax highlighting to Python source code''' |
Raymond Hettinger | 0712f40 | 2012-07-03 14:42:33 -0700 | [diff] [blame] | 3 | |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 4 | __author__ = 'Raymond Hettinger' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 5 | |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 6 | import keyword, tokenize, cgi, re, functools |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 7 | try: |
| 8 | import builtins |
| 9 | except ImportError: |
| 10 | import __builtin__ as builtins |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 11 | |
| 12 | #### Analyze Python Source ################################# |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 13 | |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 14 | def is_builtin(s): |
| 15 | 'Return True if s is the name of a builtin' |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 16 | return hasattr(builtins, s) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 17 | |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 18 | def combine_range(lines, start, end): |
| 19 | 'Join content from a range of lines between start and end' |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 20 | (srow, scol), (erow, ecol) = start, end |
| 21 | if srow == erow: |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 22 | return lines[srow-1][scol:ecol], end |
| 23 | 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] | 24 | return ''.join(rows), end |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 25 | |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 26 | def analyze_python(source): |
| 27 | '''Generate and classify chunks of Python for syntax highlighting. |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 28 | Yields tuples in the form: (category, categorized_text). |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 29 | ''' |
Raymond Hettinger | ac5f846 | 2012-07-03 00:15:59 -0700 | [diff] [blame] | 30 | lines = source.splitlines(True) |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 31 | lines.append('') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 32 | readline = functools.partial(next, iter(lines), '') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 33 | kind = tok_str = '' |
| 34 | tok_type = tokenize.COMMENT |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 35 | written = (1, 0) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 36 | for tok in tokenize.generate_tokens(readline): |
| 37 | prev_tok_type, prev_tok_str = tok_type, tok_str |
| 38 | tok_type, tok_str, (srow, scol), (erow, ecol), logical_lineno = tok |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 39 | kind = '' |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 40 | if tok_type == tokenize.COMMENT: |
| 41 | kind = 'comment' |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 42 | elif tok_type == tokenize.OP and tok_str[:1] not in '{}[](),.:;@': |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 43 | kind = 'operator' |
| 44 | elif tok_type == tokenize.STRING: |
| 45 | kind = 'string' |
| 46 | if prev_tok_type == tokenize.INDENT or scol==0: |
| 47 | kind = 'docstring' |
| 48 | elif tok_type == tokenize.NAME: |
| 49 | if tok_str in ('def', 'class', 'import', 'from'): |
| 50 | kind = 'definition' |
| 51 | elif prev_tok_str in ('def', 'class'): |
| 52 | kind = 'defname' |
| 53 | elif keyword.iskeyword(tok_str): |
| 54 | kind = 'keyword' |
| 55 | elif is_builtin(tok_str) and prev_tok_str != '.': |
| 56 | kind = 'builtin' |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 57 | if kind: |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 58 | text, written = combine_range(lines, written, (srow, scol)) |
| 59 | yield '', text |
Raymond Hettinger | d3f63d3 | 2012-07-23 00:24:24 -0500 | [diff] [blame^] | 60 | text, written = tok_str, (erow, ecol) |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 61 | yield kind, text |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 62 | line_upto_token, written = combine_range(lines, written, (erow, ecol)) |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 63 | yield '', line_upto_token |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 64 | |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 65 | #### Raw Output ########################################### |
| 66 | |
| 67 | def raw_highlight(classified_text): |
| 68 | 'Straight text display of text classifications' |
| 69 | result = [] |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 70 | for kind, text in classified_text: |
| 71 | result.append('%15s: %r\n' % (kind or 'plain', text)) |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 72 | return ''.join(result) |
| 73 | |
| 74 | #### ANSI Output ########################################### |
| 75 | |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 76 | default_ansi = { |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 77 | 'comment': ('\033[0;31m', '\033[0m'), |
| 78 | 'string': ('\033[0;32m', '\033[0m'), |
| 79 | 'docstring': ('\033[0;32m', '\033[0m'), |
| 80 | 'keyword': ('\033[0;33m', '\033[0m'), |
| 81 | 'builtin': ('\033[0;35m', '\033[0m'), |
| 82 | 'definition': ('\033[0;33m', '\033[0m'), |
| 83 | 'defname': ('\033[0;34m', '\033[0m'), |
| 84 | 'operator': ('\033[0;33m', '\033[0m'), |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 87 | def ansi_highlight(classified_text, colors=default_ansi): |
| 88 | 'Add syntax highlighting to source code using ANSI escape sequences' |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 89 | # http://en.wikipedia.org/wiki/ANSI_escape_code |
| 90 | result = [] |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 91 | for kind, text in classified_text: |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 92 | opener, closer = colors.get(kind, ('', '')) |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 93 | result += [opener, text, closer] |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 94 | return ''.join(result) |
| 95 | |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 96 | #### HTML Output ########################################### |
| 97 | |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 98 | def html_highlight(classified_text,opener='<pre class="python">\n', closer='</pre>\n'): |
| 99 | 'Convert classified text to an HTML fragment' |
| 100 | result = [opener] |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 101 | for kind, text in classified_text: |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 102 | if kind: |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 103 | result.append('<span class="%s">' % kind) |
| 104 | result.append(cgi.escape(text)) |
| 105 | if kind: |
| 106 | result.append('</span>') |
| 107 | result.append(closer) |
Raymond Hettinger | f2cc352 | 2012-07-02 13:29:57 -0700 | [diff] [blame] | 108 | return ''.join(result) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 109 | |
| 110 | default_css = { |
| 111 | '.comment': '{color: crimson;}', |
| 112 | '.string': '{color: forestgreen;}', |
Raymond Hettinger | 5da6039 | 2012-07-03 13:13:52 -0700 | [diff] [blame] | 113 | '.docstring': '{color: forestgreen; font-style:italic;}', |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 114 | '.keyword': '{color: darkorange;}', |
| 115 | '.builtin': '{color: purple;}', |
| 116 | '.definition': '{color: darkorange; font-weight:bold;}', |
| 117 | '.defname': '{color: blue;}', |
| 118 | '.operator': '{color: brown;}', |
| 119 | } |
| 120 | |
| 121 | default_html = '''\ |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 122 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 123 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 124 | <html> |
| 125 | <head> |
| 126 | <meta http-equiv="Content-type" content="text/html;charset=UTF-8"> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 127 | <title> {title} </title> |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 128 | <style type="text/css"> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 129 | {css} |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 130 | </style> |
| 131 | </head> |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 132 | <body> |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 133 | {body} |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 134 | </body> |
| 135 | </html> |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 136 | ''' |
| 137 | |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 138 | def build_html_page(classified_text, title='python', |
| 139 | css=default_css, html=default_html): |
| 140 | 'Create a complete HTML page with colorized source code' |
Raymond Hettinger | fd490cc | 2012-06-30 22:19:04 -0700 | [diff] [blame] | 141 | css_str = '\n'.join(['%s %s' % item for item in css.items()]) |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 142 | result = html_highlight(classified_text) |
Raymond Hettinger | 9b8ede6 | 2012-06-30 23:19:30 -0700 | [diff] [blame] | 143 | title = cgi.escape(title) |
Raymond Hettinger | ecea0fb | 2012-07-02 17:17:16 -0700 | [diff] [blame] | 144 | return html.format(title=title, css=css_str, body=result) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 145 | |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 146 | #### LaTeX Output ########################################## |
| 147 | |
Raymond Hettinger | c4ac789 | 2012-07-14 17:58:29 -0700 | [diff] [blame] | 148 | default_latex_commands = { |
| 149 | 'comment': '{\color{red}#1}', |
| 150 | 'string': '{\color{ForestGreen}#1}', |
| 151 | 'docstring': '{\emph{\color{ForestGreen}#1}}', |
| 152 | 'keyword': '{\color{orange}#1}', |
| 153 | 'builtin': '{\color{purple}#1}', |
| 154 | 'definition': '{\color{orange}#1}', |
| 155 | 'defname': '{\color{blue}#1}', |
| 156 | 'operator': '{\color{brown}#1}', |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | default_latex_document = r''' |
| 160 | \documentclass{article} |
| 161 | \usepackage{alltt} |
Raymond Hettinger | c4ac789 | 2012-07-14 17:58:29 -0700 | [diff] [blame] | 162 | \usepackage{upquote} |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 163 | \usepackage{color} |
| 164 | \usepackage[usenames,dvipsnames]{xcolor} |
| 165 | \usepackage[cm]{fullpage} |
Raymond Hettinger | c4ac789 | 2012-07-14 17:58:29 -0700 | [diff] [blame] | 166 | %(macros)s |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 167 | \begin{document} |
| 168 | \center{\LARGE{%(title)s}} |
| 169 | \begin{alltt} |
| 170 | %(body)s |
| 171 | \end{alltt} |
| 172 | \end{document} |
| 173 | ''' |
| 174 | |
Raymond Hettinger | d3f63d3 | 2012-07-23 00:24:24 -0500 | [diff] [blame^] | 175 | def alltt_escape(s): |
| 176 | 'Replace backslash and braces with their escaped equivalents' |
| 177 | xlat = {'{': r'\{', '}': r'\}', '\\': r'\textbackslash{}'} |
| 178 | return re.sub(r'[\\{}]', lambda mo: xlat[mo.group()], s) |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 179 | |
| 180 | def latex_highlight(classified_text, title = 'python', |
Raymond Hettinger | c4ac789 | 2012-07-14 17:58:29 -0700 | [diff] [blame] | 181 | commands = default_latex_commands, |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 182 | document = default_latex_document): |
| 183 | 'Create a complete LaTeX document with colorized source code' |
Raymond Hettinger | c4ac789 | 2012-07-14 17:58:29 -0700 | [diff] [blame] | 184 | macros = '\n'.join(r'\newcommand{\py%s}[1]{%s}' % c for c in commands.items()) |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 185 | result = [] |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 186 | for kind, text in classified_text: |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 187 | if kind: |
Raymond Hettinger | c4ac789 | 2012-07-14 17:58:29 -0700 | [diff] [blame] | 188 | result.append(r'\py%s{' % kind) |
Raymond Hettinger | d3f63d3 | 2012-07-23 00:24:24 -0500 | [diff] [blame^] | 189 | result.append(alltt_escape(text)) |
Raymond Hettinger | fb20a1a | 2012-07-13 11:52:45 -0700 | [diff] [blame] | 190 | if kind: |
| 191 | result.append('}') |
Raymond Hettinger | c4ac789 | 2012-07-14 17:58:29 -0700 | [diff] [blame] | 192 | return default_latex_document % dict(title=title, macros=macros, body=''.join(result)) |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 193 | |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 194 | |
| 195 | if __name__ == '__main__': |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 196 | import sys, argparse, webbrowser, os, textwrap |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 197 | |
| 198 | parser = argparse.ArgumentParser( |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 199 | description = 'Add syntax highlighting to Python source code', |
| 200 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 201 | epilog = textwrap.dedent(''' |
| 202 | examples: |
| 203 | |
| 204 | # Show syntax highlighted code in the terminal window |
| 205 | $ ./highlight.py myfile.py |
| 206 | |
| 207 | # Colorize myfile.py and display in a browser |
| 208 | $ ./highlight.py -b myfile.py |
| 209 | |
| 210 | # Create an HTML section to embed in an existing webpage |
| 211 | ./highlight.py -s myfile.py |
| 212 | |
| 213 | # Create a complete HTML file |
| 214 | $ ./highlight.py -c myfile.py > myfile.html |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 215 | |
| 216 | # Create a PDF using LaTeX |
| 217 | $ ./highlight.py -l myfile.py | pdflatex |
| 218 | |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 219 | ''')) |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 220 | parser.add_argument('sourcefile', metavar = 'SOURCEFILE', |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 221 | help = 'file containing Python sourcecode') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 222 | parser.add_argument('-b', '--browser', action = 'store_true', |
| 223 | help = 'launch a browser to show results') |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame] | 224 | parser.add_argument('-c', '--complete', action = 'store_true', |
| 225 | help = 'build a complete html webpage') |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 226 | parser.add_argument('-l', '--latex', action = 'store_true', |
| 227 | help = 'build a LaTeX document') |
| 228 | parser.add_argument('-r', '--raw', action = 'store_true', |
| 229 | help = 'raw parse of categorized text') |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 230 | parser.add_argument('-s', '--section', action = 'store_true', |
| 231 | help = 'show an HTML section rather than a complete webpage') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 232 | args = parser.parse_args() |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 233 | |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame] | 234 | if args.section and (args.browser or args.complete): |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 235 | parser.error('The -s/--section option is incompatible with ' |
Raymond Hettinger | 5b381a3 | 2012-07-03 17:55:23 -0700 | [diff] [blame] | 236 | 'the -b/--browser or -c/--complete options') |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 237 | |
Raymond Hettinger | cf6eac4 | 2012-07-03 00:12:27 -0700 | [diff] [blame] | 238 | sourcefile = args.sourcefile |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 239 | with open(sourcefile) as f: |
Raymond Hettinger | 0712f40 | 2012-07-03 14:42:33 -0700 | [diff] [blame] | 240 | source = f.read() |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 241 | classified_text = analyze_python(source) |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 242 | |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 243 | if args.raw: |
| 244 | encoded = raw_highlight(classified_text) |
| 245 | elif args.complete or args.browser: |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 246 | encoded = build_html_page(classified_text, title=sourcefile) |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 247 | elif args.section: |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 248 | encoded = html_highlight(classified_text) |
Raymond Hettinger | 7d39055 | 2012-07-09 23:52:08 -0700 | [diff] [blame] | 249 | elif args.latex: |
| 250 | encoded = latex_highlight(classified_text, title=sourcefile) |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 251 | else: |
Raymond Hettinger | 42a5f4a | 2012-07-08 15:42:54 -0700 | [diff] [blame] | 252 | encoded = ansi_highlight(classified_text) |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 253 | |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 254 | if args.browser: |
| 255 | htmlfile = os.path.splitext(os.path.basename(sourcefile))[0] + '.html' |
| 256 | with open(htmlfile, 'w') as f: |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 257 | f.write(encoded) |
Raymond Hettinger | bc09cf1 | 2012-06-30 16:58:06 -0700 | [diff] [blame] | 258 | webbrowser.open('file://' + os.path.abspath(htmlfile)) |
| 259 | else: |
Raymond Hettinger | 3a96161 | 2012-07-03 14:11:40 -0700 | [diff] [blame] | 260 | sys.stdout.write(encoded) |