<s> import json , sys , os , re <EOL> script_version = "<STR_LIT>" <EOL> html_escape_table = { <EOL> "<STR_LIT:&>" : "<STR_LIT>" , <EOL> '<STR_LIT:">' : "<STR_LIT>" , <EOL> "<STR_LIT:'>" : "<STR_LIT>" , <EOL> "<STR_LIT:>>" : "<STR_LIT>" , <EOL> "<STR_LIT:<>" : "<STR_LIT>" , <EOL> } <EOL> def html_escape ( text ) : <EOL> return '<STR_LIT>' . join ( html_escape_table . get ( c , c ) for c in text ) <EOL> def sanitize ( string ) : <EOL> res = '<STR_LIT>' <EOL> string = html_escape ( string ) <EOL> for i in range ( len ( string ) ) : <EOL> if ord ( string [ i ] ) > <NUM_LIT> : <EOL> res += '<STR_LIT>' % ord ( string [ i ] ) <EOL> else : <EOL> res += string [ i ] <EOL> return res <EOL> def html_for_node ( node ) : <EOL> if '<STR_LIT:url>' in node : <EOL> return html_for_url_node ( node ) <EOL> elif '<STR_LIT>' in node : <EOL> return html_for_parent_node ( node ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def html_for_url_node ( node ) : <EOL> if not re . match ( "<STR_LIT>" , node [ '<STR_LIT:url>' ] ) : <EOL> return '<STR_LIT>' % ( sanitize ( node [ '<STR_LIT:url>' ] ) , sanitize ( node [ '<STR_LIT:name>' ] ) ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> def html_for_parent_node ( node ) : <EOL> return '<STR_LIT>' % ( sanitize ( node [ '<STR_LIT:name>' ] ) , <EOL> '<STR_LIT>' . join ( [ html_for_node ( n ) for n in node [ '<STR_LIT>' ] ] ) ) <EOL> def version_text ( ) : <EOL> old_out = sys . stdout <EOL> sys . stdout = sys . stderr <EOL> print "<STR_LIT>" , script_version <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> sys . stdout = old_out <EOL> def help_text ( ) : <EOL> version_text ( ) <EOL> old_out = sys . stdout <EOL> sys . stdout = sys . stderr <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> sys . stdout = old_out <EOL> if "<STR_LIT>" in sys . argv or "<STR_LIT>" in sys . argv : <EOL> version_text ( ) <EOL> exit ( ) <EOL> if len ( sys . argv ) != <NUM_LIT:3> or "<STR_LIT>" in sys . argv or "<STR_LIT>" in sys . argv : <EOL> help_text ( ) <EOL> exit ( ) <EOL> in_file = os . path . expanduser ( sys . argv [ <NUM_LIT:1> ] ) <EOL> out_file = os . path . expanduser ( sys . argv [ <NUM_LIT:2> ] ) <EOL> try : <EOL> f = open ( in_file , '<STR_LIT:r>' ) <EOL> except IOError , e : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> print >> sys . stderr , e <EOL> exit ( ) <EOL> j = json . loads ( f . read ( ) ) <EOL> f . close ( ) <EOL> try : <EOL> out = open ( out_file , '<STR_LIT:w>' ) <EOL> except IOError , e : <EOL> print >> sys . stderr , "<STR_LIT>" <EOL> print >> sys . stderr , e <EOL> exit ( ) <EOL> out . write ( """<STR_LIT>""" <EOL> % { '<STR_LIT>' : html_for_node ( j [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : html_for_node ( j [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) } ) <EOL> out . close ( ) </s>
