<s> import re <EOL> import textutils <EOL> from htmlutils import htmlify <EOL> re_tag = re . compile ( "<STR_LIT>" ) <EOL> re_decimal_entity = re . compile ( "<STR_LIT>" ) <EOL> class Tag : <EOL> def __init__ ( self , value ) : self . value = value <EOL> def __str__ ( self ) : return self . value <EOL> def __nonzero__ ( self ) : return False <EOL> def __repr__ ( self ) : return "<STR_LIT>" % self . value <EOL> def splitTags ( line ) : <EOL> def process ( token ) : <EOL> if token [ <NUM_LIT:0> ] == '<STR_LIT:<>' : <EOL> return Tag ( token ) <EOL> else : <EOL> def replace_decimal ( match ) : <EOL> return unichr ( int ( match . group ( <NUM_LIT:1> ) ) ) <EOL> token = textutils . decode ( token ) <EOL> token = re_decimal_entity . sub ( replace_decimal , token ) <EOL> token = token . encode ( "<STR_LIT:utf-8>" ) <EOL> return token . replace ( "<STR_LIT>" , "<STR_LIT:<>" ) . replace ( "<STR_LIT>" , "<STR_LIT:>>" ) . replace ( "<STR_LIT>" , "<STR_LIT:&>" ) <EOL> return map ( process , filter ( None , re_tag . split ( line ) ) ) <EOL> def joinTags ( tags ) : <EOL> def process ( token ) : <EOL> if token : return htmlify ( token ) <EOL> else : return str ( token ) <EOL> return "<STR_LIT>" . join ( map ( process , tags ) ) <EOL> def insertTag ( tags , offset , newTag ) : <EOL> newTag = Tag ( newTag ) <EOL> index = <NUM_LIT:0> <EOL> while index < len ( tags ) : <EOL> tag = tags [ index ] <EOL> if tag : <EOL> if len ( tag ) < offset : offset -= len ( tag ) <EOL> else : <EOL> if len ( tag ) == offset : tags . insert ( index + <NUM_LIT:1> , newTag ) <EOL> elif not offset : tags . insert ( index , newTag ) <EOL> else : tags [ index : index + <NUM_LIT:1> ] = tag [ : offset ] , newTag , tag [ offset : ] <EOL> return <EOL> index += <NUM_LIT:1> <EOL> tags . append ( newTag ) <EOL> def lineDiffHTML ( ops , old , new ) : <EOL> old = splitTags ( old ) <EOL> new = splitTags ( new ) <EOL> for op in ops . split ( '<STR_LIT:U+002C>' ) : <EOL> old_lines = None <EOL> oldType = None <EOL> new_lines = None <EOL> newType = None <EOL> if op [ <NUM_LIT:0> ] == '<STR_LIT:r>' : <EOL> old_lines , new_lines = op [ <NUM_LIT:1> : ] . split ( '<STR_LIT:=>' ) <EOL> oldType = '<STR_LIT:r>' <EOL> newType = '<STR_LIT:r>' <EOL> elif op [ <NUM_LIT:0> ] == '<STR_LIT:d>' : <EOL> old_lines = op [ <NUM_LIT:1> : ] <EOL> oldType = '<STR_LIT:d>' <EOL> else : <EOL> new_lines = op [ <NUM_LIT:1> : ] <EOL> newType = '<STR_LIT:i>' <EOL> if old_lines : <EOL> start , end = old_lines . split ( '<STR_LIT:->' ) <EOL> insertTag ( old , int ( start ) , "<STR_LIT>" % oldType ) <EOL> insertTag ( old , int ( end ) , "<STR_LIT>" ) <EOL> if new_lines : <EOL> start , end = new_lines . split ( '<STR_LIT:->' ) <EOL> insertTag ( new , int ( start ) , "<STR_LIT>" % newType ) <EOL> insertTag ( new , int ( end ) , "<STR_LIT>" ) <EOL> return joinTags ( old ) , joinTags ( new ) </s>
