<s> from __future__ import division <EOL> from itertools import izip <EOL> import re <EOL> def parse_vt_command ( opt_arg , arg ) : <EOL> opt_dict = { } <EOL> for opt_str in opt_arg [ <NUM_LIT:1> : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) : <EOL> opt_str = opt_str . strip ( ) <EOL> opt_list = opt_str . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( opt_list ) > <NUM_LIT:1> : <EOL> if opt_list [ <NUM_LIT:0> ] . strip ( ) : <EOL> opt_dict [ opt_list [ <NUM_LIT:0> ] ] = opt_list [ <NUM_LIT:1> ] <EOL> else : <EOL> if opt_list [ <NUM_LIT:0> ] . strip ( ) : <EOL> opt_dict [ opt_list [ <NUM_LIT:0> ] ] = None <EOL> opt_dict [ '<STR_LIT>' ] = arg [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> return opt_dict <EOL> def build_vt_command ( opt_dict ) : <EOL> if '<STR_LIT>' in opt_dict : <EOL> args = opt_dict [ '<STR_LIT>' ] <EOL> else : <EOL> args = '<STR_LIT>' <EOL> opt_str = '<STR_LIT>' <EOL> for k , v in opt_dict . iteritems ( ) : <EOL> if k == '<STR_LIT>' : <EOL> continue <EOL> if v is None : <EOL> opt_str += '<STR_LIT>' % k <EOL> else : <EOL> opt_str += '<STR_LIT>' % ( k , v ) <EOL> if opt_str : <EOL> full_str = "<STR_LIT>" % ( opt_str , args ) <EOL> else : <EOL> full_str = "<STR_LIT>" % args <EOL> return full_str <EOL> def parse_latex_file ( fname ) : <EOL> vt_begin_re = re . compile ( r"<STR_LIT>" ) <EOL> vt_bracket_re = re . compile ( r"<STR_LIT>" ) <EOL> vt_brace_re = re . compile ( r"<STR_LIT>" ) <EOL> f = open ( fname ) <EOL> raw_text = [ "<STR_LIT>" ] <EOL> raw_idx = <NUM_LIT:0> <EOL> vt_text = [ ] <EOL> for line in f : <EOL> m = vt_begin_re . search ( line ) <EOL> if not m : <EOL> raw_text [ raw_idx ] += line <EOL> else : <EOL> raw_text [ raw_idx ] += line [ : m . start ( <NUM_LIT:1> ) ] <EOL> raw_text . append ( "<STR_LIT>" ) <EOL> raw_idx += <NUM_LIT:1> <EOL> cmd_text = "<STR_LIT>" <EOL> line = line [ m . end ( <NUM_LIT:1> ) - <NUM_LIT:1> : ] <EOL> if line [ <NUM_LIT:0> ] == '<STR_LIT:[>' : <EOL> brackets_complete = False <EOL> found_start = False <EOL> brackets = <NUM_LIT:0> <EOL> while not brackets_complete : <EOL> for m in vt_bracket_re . finditer ( line ) : <EOL> bracket_char = line [ m . start ( ) ] <EOL> if bracket_char == '<STR_LIT:[>' : <EOL> brackets += <NUM_LIT:1> <EOL> found_start = True <EOL> else : <EOL> brackets -= <NUM_LIT:1> <EOL> if found_start and brackets == <NUM_LIT:0> : <EOL> brackets_complete = True <EOL> cmd_text += line [ : m . end ( ) ] <EOL> line = line [ m . end ( ) : ] <EOL> break <EOL> if brackets_complete : <EOL> break <EOL> cmd_text += line <EOL> line = f . next ( ) <EOL> opt_cmd_text = cmd_text <EOL> cmd_text = "<STR_LIT>" <EOL> braces_complete = False <EOL> found_start = False <EOL> braces = <NUM_LIT:0> <EOL> while not braces_complete : <EOL> for m in vt_brace_re . finditer ( line ) : <EOL> brace_char = line [ m . start ( ) ] <EOL> if brace_char == '<STR_LIT:{>' : <EOL> braces += <NUM_LIT:1> <EOL> found_start = True <EOL> else : <EOL> braces -= <NUM_LIT:1> <EOL> if found_start and braces == <NUM_LIT:0> : <EOL> braces_complete = True <EOL> cmd_text += line [ : m . end ( ) ] <EOL> break <EOL> if braces_complete : <EOL> break <EOL> cmd_text += line <EOL> line = f . next ( ) <EOL> vt_text . append ( ( opt_cmd_text , cmd_text ) ) <EOL> raw_text [ raw_idx ] += line [ m . end ( ) : ] <EOL> f . close ( ) <EOL> return ( raw_text , vt_text ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> if len ( sys . argv ) < <NUM_LIT:2> : <EOL> print "<STR_LIT>" % ( sys . executable , sys . argv [ <NUM_LIT:0> ] ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> parse_latex_file ( sys . argv [ <NUM_LIT:1> ] ) </s>
