<s> """<STR_LIT>""" <EOL> from future import standard_library <EOL> standard_library . install_aliases ( ) <EOL> from builtins import chr <EOL> from builtins import object <EOL> from xml . sax . saxutils import quoteattr <EOL> from html . parser import HTMLParser <EOL> from html . entities import name2codepoint <EOL> def attrs2xml ( attrs ) : <EOL> return '<STR_LIT:U+0020>' . join ( [ '<STR_LIT>' % ( k , quoteattr ( v ) ) for k , v in attrs ] ) <EOL> INLINE_TAGS = frozenset ( """<STR_LIT>""" . split ( ) ) <EOL> def is_inline_tag ( tag ) : <EOL> return tag in INLINE_TAGS <EOL> class TagEntry ( object ) : <EOL> def __init__ ( self , name , ignored = False ) : <EOL> self . name = name <EOL> self . ignored = ignored <EOL> class MyHTMLParser ( HTMLParser ) : <EOL> def __init__ ( self , * args , ** kw ) : <EOL> HTMLParser . __init__ ( self , * args , ** kw ) <EOL> self . result = '<STR_LIT>' <EOL> self . context = [ ] <EOL> self . inline_stack = [ ] <EOL> self . is_inline = False <EOL> def write_startendtag ( self , tag , attrs ) : <EOL> if attrs : <EOL> self . result += '<STR_LIT>' % ( tag , attrs2xml ( attrs ) ) <EOL> else : <EOL> self . result += '<STR_LIT>' % tag <EOL> def write_starttag ( self , tag , attrs ) : <EOL> if attrs : <EOL> self . result += '<STR_LIT>' % ( tag , attrs2xml ( attrs ) ) <EOL> else : <EOL> self . result += '<STR_LIT>' % tag <EOL> def write_endtag ( self , tag ) : <EOL> self . result += '<STR_LIT>' % tag <EOL> def before_tag ( self , tag ) : <EOL> if is_inline_tag ( tag ) : <EOL> if not self . is_inline : <EOL> for itag , iattrs in self . pending_inline_tags ( ) : <EOL> self . handle_starttag ( itag , iattrs ) <EOL> self . is_inline = True <EOL> elif self . is_inline : <EOL> self . is_inline = False <EOL> def handle_startendtag ( self , tag , attrs ) : <EOL> if not self . allow_tag_in_context ( tag ) : <EOL> return <EOL> self . write_startendtag ( tag , attrs ) <EOL> def handle_starttag ( self , tag , attrs ) : <EOL> if tag in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . handle_startendtag ( tag , attrs ) <EOL> else : <EOL> if tag == '<STR_LIT:p>' : <EOL> if len ( self . context ) > <NUM_LIT:0> : <EOL> if self . context [ - <NUM_LIT:1> ] . name == '<STR_LIT:p>' : <EOL> self . handle_endtag ( tag ) <EOL> self . result += '<STR_LIT:\n>' <EOL> if not self . allow_tag_in_context ( tag ) : <EOL> self . context . append ( TagEntry ( tag , True ) ) <EOL> return <EOL> self . context . append ( TagEntry ( tag , False ) ) <EOL> self . write_starttag ( tag , attrs ) <EOL> def handle_endtag ( self , tag ) : <EOL> if len ( self . context ) > <NUM_LIT:0> : <EOL> expected = self . context . pop ( ) <EOL> if expected . name != tag : <EOL> raise Exception ( "<STR_LIT>" % <EOL> ( expected . name , tag ) ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" % <EOL> tag ) <EOL> if not expected . ignored : <EOL> self . write_endtag ( tag ) <EOL> def allow_tag_in_context ( self , tag ) : <EOL> if tag == '<STR_LIT>' : <EOL> for te in self . context : <EOL> if te . name == '<STR_LIT>' : <EOL> return False <EOL> return True <EOL> def handle_data ( self , data ) : <EOL> self . result += data <EOL> def handle_entityref ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . handle_data ( "<STR_LIT>" % name ) <EOL> return <EOL> self . handle_data ( chr ( name2codepoint [ name ] ) ) <EOL> def html2xhtml ( html , ** options ) : <EOL> """<STR_LIT>""" <EOL> p = MyHTMLParser ( ) <EOL> p . feed ( html ) <EOL> p . close ( ) <EOL> while len ( p . context ) > <NUM_LIT:0> : <EOL> e = p . context . pop ( ) <EOL> p . write_endtag ( e . name ) <EOL> p . result += '<STR_LIT:\n>' <EOL> if len ( p . context ) > <NUM_LIT:0> : <EOL> expected = '<STR_LIT>' . join ( [ "<STR_LIT>" % te . name for te in p . context ] ) <EOL> raise Exception ( "<STR_LIT>" % <EOL> expected ) <EOL> return p . result . rstrip ( ) </s>
