<s> """<STR_LIT>""" <EOL> from warnings import warnpy3k <EOL> warnpy3k ( "<STR_LIT>" , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> del warnpy3k <EOL> import sgmllib <EOL> from formatter import AS_IS <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class HTMLParseError ( sgmllib . SGMLParseError ) : <EOL> """<STR_LIT>""" <EOL> class HTMLParser ( sgmllib . SGMLParser ) : <EOL> """<STR_LIT>""" <EOL> from htmlentitydefs import entitydefs <EOL> def __init__ ( self , formatter , verbose = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> sgmllib . SGMLParser . __init__ ( self , verbose ) <EOL> self . formatter = formatter <EOL> def error ( self , message ) : <EOL> raise HTMLParseError ( message ) <EOL> def reset ( self ) : <EOL> sgmllib . SGMLParser . reset ( self ) <EOL> self . savedata = None <EOL> self . isindex = <NUM_LIT:0> <EOL> self . title = None <EOL> self . base = None <EOL> self . anchor = None <EOL> self . anchorlist = [ ] <EOL> self . nofill = <NUM_LIT:0> <EOL> self . list_stack = [ ] <EOL> def handle_data ( self , data ) : <EOL> if self . savedata is not None : <EOL> self . savedata = self . savedata + data <EOL> else : <EOL> if self . nofill : <EOL> self . formatter . add_literal_data ( data ) <EOL> else : <EOL> self . formatter . add_flowing_data ( data ) <EOL> def save_bgn ( self ) : <EOL> """<STR_LIT>""" <EOL> self . savedata = '<STR_LIT>' <EOL> def save_end ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . savedata <EOL> self . savedata = None <EOL> if not self . nofill : <EOL> data = '<STR_LIT:U+0020>' . join ( data . split ( ) ) <EOL> return data <EOL> def anchor_bgn ( self , href , name , type ) : <EOL> """<STR_LIT>""" <EOL> self . anchor = href <EOL> if self . anchor : <EOL> self . anchorlist . append ( href ) <EOL> def anchor_end ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . anchor : <EOL> self . handle_data ( "<STR_LIT>" % len ( self . anchorlist ) ) <EOL> self . anchor = None <EOL> def handle_image ( self , src , alt , * args ) : <EOL> """<STR_LIT>""" <EOL> self . handle_data ( alt ) <EOL> def start_html ( self , attrs ) : pass <EOL> def end_html ( self ) : pass <EOL> def start_head ( self , attrs ) : pass <EOL> def end_head ( self ) : pass <EOL> def start_body ( self , attrs ) : pass <EOL> def end_body ( self ) : pass <EOL> def start_title ( self , attrs ) : <EOL> self . save_bgn ( ) <EOL> def end_title ( self ) : <EOL> self . title = self . save_end ( ) <EOL> def do_base ( self , attrs ) : <EOL> for a , v in attrs : <EOL> if a == '<STR_LIT>' : <EOL> self . base = v <EOL> def do_isindex ( self , attrs ) : <EOL> self . isindex = <NUM_LIT:1> <EOL> def do_link ( self , attrs ) : <EOL> pass <EOL> def do_meta ( self , attrs ) : <EOL> pass <EOL> def do_nextid ( self , attrs ) : <EOL> pass <EOL> def start_h1 ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . push_font ( ( '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def end_h1 ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . pop_font ( ) <EOL> def start_h2 ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . push_font ( ( '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def end_h2 ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . pop_font ( ) <EOL> def start_h3 ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . push_font ( ( '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def end_h3 ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . pop_font ( ) <EOL> def start_h4 ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . push_font ( ( '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def end_h4 ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . pop_font ( ) <EOL> def start_h5 ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . push_font ( ( '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def end_h5 ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . pop_font ( ) <EOL> def start_h6 ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . push_font ( ( '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> def end_h6 ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . pop_font ( ) <EOL> def do_p ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> def start_pre ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . push_font ( ( AS_IS , AS_IS , AS_IS , <NUM_LIT:1> ) ) <EOL> self . nofill = self . nofill + <NUM_LIT:1> <EOL> def end_pre ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . pop_font ( ) <EOL> self . nofill = max ( <NUM_LIT:0> , self . nofill - <NUM_LIT:1> ) <EOL> def start_xmp ( self , attrs ) : <EOL> self . start_pre ( attrs ) <EOL> self . setliteral ( '<STR_LIT>' ) <EOL> def end_xmp ( self ) : <EOL> self . end_pre ( ) <EOL> def start_listing ( self , attrs ) : <EOL> self . start_pre ( attrs ) <EOL> self . setliteral ( '<STR_LIT>' ) <EOL> def end_listing ( self ) : <EOL> self . end_pre ( ) <EOL> def start_address ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:0> ) <EOL> self . formatter . push_font ( ( AS_IS , <NUM_LIT:1> , AS_IS , AS_IS ) ) <EOL> def end_address ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:0> ) <EOL> self . formatter . pop_font ( ) <EOL> def start_blockquote ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . push_margin ( '<STR_LIT>' ) <EOL> def end_blockquote ( self ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . formatter . pop_margin ( ) <EOL> def start_ul ( self , attrs ) : <EOL> self . formatter . end_paragraph ( not self . list_stack ) <EOL> self . formatter . push_margin ( '<STR_LIT>' ) <EOL> self . list_stack . append ( [ '<STR_LIT>' , '<STR_LIT:*>' , <NUM_LIT:0> ] ) <EOL> def end_ul ( self ) : <EOL> if self . list_stack : del self . list_stack [ - <NUM_LIT:1> ] <EOL> self . formatter . end_paragraph ( not self . list_stack ) <EOL> self . formatter . pop_margin ( ) <EOL> def do_li ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:0> ) <EOL> if self . list_stack : <EOL> [ dummy , label , counter ] = top = self . list_stack [ - <NUM_LIT:1> ] <EOL> top [ <NUM_LIT:2> ] = counter = counter + <NUM_LIT:1> <EOL> else : <EOL> label , counter = '<STR_LIT:*>' , <NUM_LIT:0> <EOL> self . formatter . add_label_data ( label , counter ) <EOL> def start_ol ( self , attrs ) : <EOL> self . formatter . end_paragraph ( not self . list_stack ) <EOL> self . formatter . push_margin ( '<STR_LIT>' ) <EOL> label = '<STR_LIT>' <EOL> for a , v in attrs : <EOL> if a == '<STR_LIT:type>' : <EOL> if len ( v ) == <NUM_LIT:1> : v = v + '<STR_LIT:.>' <EOL> label = v <EOL> self . list_stack . append ( [ '<STR_LIT>' , label , <NUM_LIT:0> ] ) <EOL> def end_ol ( self ) : <EOL> if self . list_stack : del self . list_stack [ - <NUM_LIT:1> ] <EOL> self . formatter . end_paragraph ( not self . list_stack ) <EOL> self . formatter . pop_margin ( ) <EOL> def start_menu ( self , attrs ) : <EOL> self . start_ul ( attrs ) <EOL> def end_menu ( self ) : <EOL> self . end_ul ( ) <EOL> def start_dir ( self , attrs ) : <EOL> self . start_ul ( attrs ) <EOL> def end_dir ( self ) : <EOL> self . end_ul ( ) <EOL> def start_dl ( self , attrs ) : <EOL> self . formatter . end_paragraph ( <NUM_LIT:1> ) <EOL> self . list_stack . append ( [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> ] ) <EOL> def end_dl ( self ) : <EOL> self . ddpop ( <NUM_LIT:1> ) <EOL> if self . list_stack : del self . list_stack [ - <NUM_LIT:1> ] <EOL> def do_dt ( self , attrs ) : <EOL> self . ddpop ( ) <EOL> def do_dd ( self , attrs ) : <EOL> self . ddpop ( ) <EOL> self . formatter . push_margin ( '<STR_LIT>' ) <EOL> self . list_stack . append ( [ '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> ] ) <EOL> def ddpop ( self , bl = <NUM_LIT:0> ) : <EOL> self . formatter . end_paragraph ( bl ) <EOL> if self . list_stack : <EOL> if self . list_stack [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> del self . list_stack [ - <NUM_LIT:1> ] <EOL> self . formatter . pop_margin ( ) <EOL> def start_cite ( self , attrs ) : self . start_i ( attrs ) <EOL> def end_cite ( self ) : self . end_i ( ) <EOL> def start_code ( self , attrs ) : self . start_tt ( attrs ) <EOL> def end_code ( self ) : self . end_tt ( ) <EOL> def start_em ( self , attrs ) : self . start_i ( attrs ) <EOL> def end_em ( self ) : self . end_i ( ) <EOL> def start_kbd ( self , attrs ) : self . start_tt ( attrs ) <EOL> def end_kbd ( self ) : self . end_tt ( ) <EOL> def start_samp ( self , attrs ) : self . start_tt ( attrs ) <EOL> def end_samp ( self ) : self . end_tt ( ) <EOL> def start_strong ( self , attrs ) : self . start_b ( attrs ) <EOL> def end_strong ( self ) : self . end_b ( ) <EOL> def start_var ( self , attrs ) : self . start_i ( attrs ) <EOL> def end_var ( self ) : self . end_i ( ) <EOL> def start_i ( self , attrs ) : <EOL> self . formatter . push_font ( ( AS_IS , <NUM_LIT:1> , AS_IS , AS_IS ) ) <EOL> def end_i ( self ) : <EOL> self . formatter . pop_font ( ) <EOL> def start_b ( self , attrs ) : <EOL> self . formatter . push_font ( ( AS_IS , AS_IS , <NUM_LIT:1> , AS_IS ) ) <EOL> def end_b ( self ) : <EOL> self . formatter . pop_font ( ) <EOL> def start_tt ( self , attrs ) : <EOL> self . formatter . push_font ( ( AS_IS , AS_IS , AS_IS , <NUM_LIT:1> ) ) <EOL> def end_tt ( self ) : <EOL> self . formatter . pop_font ( ) <EOL> def start_a ( self , attrs ) : <EOL> href = '<STR_LIT>' <EOL> name = '<STR_LIT>' <EOL> type = '<STR_LIT>' <EOL> for attrname , value in attrs : <EOL> value = value . strip ( ) <EOL> if attrname == '<STR_LIT>' : <EOL> href = value <EOL> if attrname == '<STR_LIT:name>' : <EOL> name = value <EOL> if attrname == '<STR_LIT:type>' : <EOL> type = value . lower ( ) <EOL> self . anchor_bgn ( href , name , type ) <EOL> def end_a ( self ) : <EOL> self . anchor_end ( ) <EOL> def do_br ( self , attrs ) : <EOL> self . formatter . add_line_break ( ) <EOL> def do_hr ( self , attrs ) : <EOL> self . formatter . add_hor_rule ( ) <EOL> def do_img ( self , attrs ) : <EOL> align = '<STR_LIT>' <EOL> alt = '<STR_LIT>' <EOL> ismap = '<STR_LIT>' <EOL> src = '<STR_LIT>' <EOL> width = <NUM_LIT:0> <EOL> height = <NUM_LIT:0> <EOL> for attrname , value in attrs : <EOL> if attrname == '<STR_LIT>' : <EOL> align = value <EOL> if attrname == '<STR_LIT>' : <EOL> alt = value <EOL> if attrname == '<STR_LIT>' : <EOL> ismap = value <EOL> if attrname == '<STR_LIT:src>' : <EOL> src = value <EOL> if attrname == '<STR_LIT:width>' : <EOL> try : width = int ( value ) <EOL> except ValueError : pass <EOL> if attrname == '<STR_LIT>' : <EOL> try : height = int ( value ) <EOL> except ValueError : pass <EOL> self . handle_image ( src , alt , ismap , align , width , height ) <EOL> def do_plaintext ( self , attrs ) : <EOL> self . start_pre ( attrs ) <EOL> self . setnomoretags ( ) <EOL> def unknown_starttag ( self , tag , attrs ) : <EOL> pass <EOL> def unknown_endtag ( self , tag ) : <EOL> pass <EOL> def test ( args = None ) : <EOL> import sys , formatter <EOL> if not args : <EOL> args = sys . argv [ <NUM_LIT:1> : ] <EOL> silent = args and args [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> if silent : <EOL> del args [ <NUM_LIT:0> ] <EOL> if args : <EOL> file = args [ <NUM_LIT:0> ] <EOL> else : <EOL> file = '<STR_LIT>' <EOL> if file == '<STR_LIT:->' : <EOL> f = sys . stdin <EOL> else : <EOL> try : <EOL> f = open ( file , '<STR_LIT:r>' ) <EOL> except IOError , msg : <EOL> print file , "<STR_LIT::>" , msg <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> data = f . read ( ) <EOL> if f is not sys . stdin : <EOL> f . close ( ) <EOL> if silent : <EOL> f = formatter . NullFormatter ( ) <EOL> else : <EOL> f = formatter . AbstractFormatter ( formatter . DumbWriter ( ) ) <EOL> p = HTMLParser ( f ) <EOL> p . feed ( data ) <EOL> p . close ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test ( ) </s>
