<s> import htmlentitydefs <EOL> import re , string , sys <EOL> import mimetools , StringIO <EOL> import ElementTree <EOL> AUTOCLOSE = "<STR_LIT:p>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:body>" <EOL> IGNOREEND = "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> if sys . version [ : <NUM_LIT:3> ] == "<STR_LIT>" : <EOL> is_not_ascii = re . compile ( r"<STR_LIT>" ) . search <EOL> else : <EOL> is_not_ascii = re . compile ( eval ( r'<STR_LIT>' ) ) . search <EOL> try : <EOL> from HTMLParser import HTMLParser <EOL> except ImportError : <EOL> from sgmllib import SGMLParser <EOL> class HTMLParser ( SGMLParser ) : <EOL> def unknown_starttag ( self , tag , attrs ) : <EOL> self . handle_starttag ( tag , attrs ) <EOL> def unknown_endtag ( self , tag ) : <EOL> self . handle_endtag ( tag ) <EOL> class HTMLTreeBuilder ( HTMLParser ) : <EOL> def __init__ ( self , builder = None , encoding = None ) : <EOL> self . __stack = [ ] <EOL> if builder is None : <EOL> builder = ElementTree . TreeBuilder ( ) <EOL> self . __builder = builder <EOL> self . encoding = encoding or "<STR_LIT>" <EOL> HTMLParser . __init__ ( self ) <EOL> def close ( self ) : <EOL> HTMLParser . close ( self ) <EOL> return self . __builder . close ( ) <EOL> def handle_starttag ( self , tag , attrs ) : <EOL> if tag == "<STR_LIT>" : <EOL> http_equiv = content = None <EOL> for k , v in attrs : <EOL> if k == "<STR_LIT>" : <EOL> http_equiv = string . lower ( v ) <EOL> elif k == "<STR_LIT:content>" : <EOL> content = v <EOL> if http_equiv == "<STR_LIT>" and content : <EOL> header = mimetools . Message ( <EOL> StringIO . StringIO ( "<STR_LIT>" % ( http_equiv , content ) ) <EOL> ) <EOL> encoding = header . getparam ( "<STR_LIT>" ) <EOL> if encoding : <EOL> self . encoding = encoding <EOL> if tag in AUTOCLOSE : <EOL> if self . __stack and self . __stack [ - <NUM_LIT:1> ] == tag : <EOL> self . handle_endtag ( tag ) <EOL> self . __stack . append ( tag ) <EOL> attrib = { } <EOL> if attrs : <EOL> for k , v in attrs : <EOL> attrib [ string . lower ( k ) ] = v <EOL> self . __builder . start ( tag , attrib ) <EOL> if tag in IGNOREEND : <EOL> self . __stack . pop ( ) <EOL> self . __builder . end ( tag ) <EOL> def handle_endtag ( self , tag ) : <EOL> if tag in IGNOREEND : <EOL> return <EOL> lasttag = self . __stack . pop ( ) <EOL> if tag != lasttag and lasttag in AUTOCLOSE : <EOL> self . handle_endtag ( lasttag ) <EOL> self . __builder . end ( tag ) <EOL> def handle_charref ( self , char ) : <EOL> if char [ : <NUM_LIT:1> ] == "<STR_LIT:x>" : <EOL> char = int ( char [ <NUM_LIT:1> : ] , <NUM_LIT:16> ) <EOL> else : <EOL> char = int ( char ) <EOL> if <NUM_LIT:0> <= char < <NUM_LIT> : <EOL> self . __builder . data ( chr ( char ) ) <EOL> else : <EOL> self . __builder . data ( unichr ( char ) ) <EOL> def handle_entityref ( self , name ) : <EOL> entity = htmlentitydefs . entitydefs . get ( name ) <EOL> if entity : <EOL> if len ( entity ) == <NUM_LIT:1> : <EOL> entity = ord ( entity ) <EOL> else : <EOL> entity = int ( entity [ <NUM_LIT:2> : - <NUM_LIT:1> ] ) <EOL> if <NUM_LIT:0> <= entity < <NUM_LIT> : <EOL> self . __builder . data ( chr ( entity ) ) <EOL> else : <EOL> self . __builder . data ( unichr ( entity ) ) <EOL> else : <EOL> self . unknown_entityref ( name ) <EOL> def handle_data ( self , data ) : <EOL> if isinstance ( data , type ( '<STR_LIT>' ) ) and is_not_ascii ( data ) : <EOL> data = unicode ( data , self . encoding , "<STR_LIT:ignore>" ) <EOL> self . __builder . data ( data ) <EOL> def unknown_entityref ( self , name ) : <EOL> pass <EOL> TreeBuilder = HTMLTreeBuilder <EOL> def parse ( source , encoding = None ) : <EOL> return ElementTree . parse ( source , HTMLTreeBuilder ( encoding = encoding ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys <EOL> ElementTree . dump ( parse ( open ( sys . argv [ <NUM_LIT:1> ] ) ) ) </s>
