@@ -21,7 +21,7 @@ def escape_range(lines, start, end):
2121
2222def colorize (source ):
2323 'Convert Python source code to an HTML fragment with colorized markup'
24- lines = source .splitlines (True )
24+ lines = source .splitlines (keepends = True )
2525 lines .append ('' )
2626 readline = functools .partial (next , iter (lines ), '' )
2727 kind = tok_str = ''
@@ -31,7 +31,7 @@ def colorize(source):
3131 for tok in tokenize .generate_tokens (readline ):
3232 prev_tok_type , prev_tok_str = tok_type , tok_str
3333 tok_type , tok_str , (srow , scol ), (erow , ecol ), logical_lineno = tok
34- kind , prev_kind = '' , kind
34+ kind = ''
3535 if tok_type == tokenize .COMMENT :
3636 kind = 'comment'
3737 elif tok_type == tokenize .OP and tok_str [:1 ] not in '{}[](),.:;' :
@@ -102,22 +102,22 @@ def build_page(source, title='python', css=default_css, html=default_html):
102102 import sys , argparse , webbrowser , os
103103
104104 parser = argparse .ArgumentParser (
105- description = 'Convert Python source code to colorized HTML' )
106- parser .add_argument ('sourcefile' , metavar = 'SOURCEFILE' , nargs = 1 ,
105+ description = 'Convert Python source code to colorized HTML' )
106+ parser .add_argument ('sourcefile' , metavar = 'SOURCEFILE' ,
107107 help = 'File containing Python sourcecode' )
108108 parser .add_argument ('-b' , '--browser' , action = 'store_true' ,
109109 help = 'launch a browser to show results' )
110- parser .add_argument ('-s' , '--standalone ' , action = 'store_true' ,
111- help = 'show a standalone snippet rather than a complete webpage' )
110+ parser .add_argument ('-s' , '--section ' , action = 'store_true' ,
111+ help = 'show an HTML section rather than a complete webpage' )
112112 args = parser .parse_args ()
113- if args .browser and args .standalone :
114- parser .error ('The -s/--standalone option is incompatible with '
113+ if args .browser and args .section :
114+ parser .error ('The -s/--section option is incompatible with '
115115 'the -b/--browser option' )
116116
117- sourcefile = args .sourcefile [ 0 ]
117+ sourcefile = args .sourcefile
118118 with open (sourcefile ) as f :
119119 page = f .read ()
120- html = colorize (page ) if args .standalone else build_page (page , title = sourcefile )
120+ html = colorize (page ) if args .section else build_page (page , title = sourcefile )
121121 if args .browser :
122122 htmlfile = os .path .splitext (os .path .basename (sourcefile ))[0 ] + '.html'
123123 with open (htmlfile , 'w' ) as f :
0 commit comments