<s> import HTMLParser as _HTMLParser <EOL> import re <EOL> import sys <EOL> current_version = sys . version_info <EOL> use_workaround = ( <EOL> ( current_version < ( <NUM_LIT:2> , <NUM_LIT:7> , <NUM_LIT:3> ) ) or <EOL> ( current_version >= ( <NUM_LIT:3> , <NUM_LIT:0> ) and current_version < ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:3> ) ) <EOL> ) <EOL> if not use_workaround : <EOL> HTMLParser = _HTMLParser . HTMLParser <EOL> else : <EOL> class HTMLParser ( _HTMLParser . HTMLParser ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> _HTMLParser . HTMLParser . __init__ ( self ) <EOL> self . cdata_tag = None <EOL> def set_cdata_mode ( self , tag ) : <EOL> try : <EOL> self . interesting = _HTMLParser . interesting_cdata <EOL> except AttributeError : <EOL> self . interesting = re . compile ( r'<STR_LIT>' % tag . lower ( ) , re . I ) <EOL> self . cdata_tag = tag . lower ( ) <EOL> def clear_cdata_mode ( self ) : <EOL> self . interesting = _HTMLParser . interesting_normal <EOL> self . cdata_tag = None <EOL> def parse_starttag ( self , i ) : <EOL> self . __starttag_text = None <EOL> endpos = self . check_for_whole_start_tag ( i ) <EOL> if endpos < <NUM_LIT:0> : <EOL> return endpos <EOL> rawdata = self . rawdata <EOL> self . __starttag_text = rawdata [ i : endpos ] <EOL> attrs = [ ] <EOL> match = _HTMLParser . tagfind . match ( rawdata , i + <NUM_LIT:1> ) <EOL> assert match , '<STR_LIT>' <EOL> k = match . end ( ) <EOL> self . lasttag = tag = rawdata [ i + <NUM_LIT:1> : k ] . lower ( ) <EOL> while k < endpos : <EOL> m = _HTMLParser . attrfind . match ( rawdata , k ) <EOL> if not m : <EOL> break <EOL> attrname , rest , attrvalue = m . group ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ) <EOL> if not rest : <EOL> attrvalue = None <EOL> elif attrvalue [ : <NUM_LIT:1> ] == '<STR_LIT>' == attrvalue [ - <NUM_LIT:1> : ] or attrvalue [ : <NUM_LIT:1> ] == '<STR_LIT:">' == attrvalue [ - <NUM_LIT:1> : ] : <EOL> attrvalue = attrvalue [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> attrvalue = self . unescape ( attrvalue ) <EOL> attrs . append ( ( attrname . lower ( ) , attrvalue ) ) <EOL> k = m . end ( ) <EOL> end = rawdata [ k : endpos ] . strip ( ) <EOL> if end not in ( "<STR_LIT:>>" , "<STR_LIT>" ) : <EOL> lineno , offset = self . getpos ( ) <EOL> if "<STR_LIT:\n>" in self . __starttag_text : <EOL> lineno = lineno + self . __starttag_text . count ( "<STR_LIT:\n>" ) <EOL> offset = len ( self . __starttag_text ) - self . __starttag_text . rfind ( "<STR_LIT:\n>" ) <EOL> else : <EOL> offset = offset + len ( self . __starttag_text ) <EOL> self . error ( "<STR_LIT>" <EOL> % ( rawdata [ k : endpos ] [ : <NUM_LIT:20> ] , ) ) <EOL> if end . endswith ( '<STR_LIT>' ) : <EOL> self . handle_startendtag ( tag , attrs ) <EOL> else : <EOL> self . handle_starttag ( tag , attrs ) <EOL> if tag in self . CDATA_CONTENT_ELEMENTS : <EOL> self . set_cdata_mode ( tag ) <EOL> return endpos <EOL> def parse_endtag ( self , i ) : <EOL> rawdata = self . rawdata <EOL> assert rawdata [ i : i + <NUM_LIT:2> ] == "<STR_LIT>" , "<STR_LIT>" <EOL> match = _HTMLParser . endendtag . search ( rawdata , i + <NUM_LIT:1> ) <EOL> if not match : <EOL> return - <NUM_LIT:1> <EOL> j = match . end ( ) <EOL> match = _HTMLParser . endtagfind . match ( rawdata , i ) <EOL> if not match : <EOL> if self . cdata_tag is not None : <EOL> self . handle_data ( rawdata [ i : j ] ) <EOL> return j <EOL> self . error ( "<STR_LIT>" % ( rawdata [ i : j ] , ) ) <EOL> tag = match . group ( <NUM_LIT:1> ) . strip ( ) <EOL> if self . cdata_tag is not None : <EOL> if tag . lower ( ) != self . cdata_tag : <EOL> self . handle_data ( rawdata [ i : j ] ) <EOL> return j <EOL> self . handle_endtag ( tag . lower ( ) ) <EOL> self . clear_cdata_mode ( ) <EOL> return j </s>
