<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> from future import standard_library <EOL> standard_library . install_aliases ( ) <EOL> from builtins import chr <EOL> from builtins import str <EOL> from builtins import range <EOL> from builtins import object <EOL> from past . utils import old_div <EOL> __version__ = "<STR_LIT>" <EOL> __author__ = "<STR_LIT>" <EOL> __copyright__ = "<STR_LIT>" <EOL> __contributors__ = [ "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> try : <EOL> True <EOL> except NameError : <EOL> setattr ( __builtins__ , '<STR_LIT:True>' , <NUM_LIT:1> ) <EOL> setattr ( __builtins__ , '<STR_LIT:False>' , <NUM_LIT:0> ) <EOL> def has_key ( x , y ) : <EOL> if hasattr ( x , '<STR_LIT>' ) : <EOL> return y in x <EOL> else : <EOL> return y in x <EOL> import html . entities as htmlentitydefs <EOL> import urllib . parse as urlparse <EOL> from html . parser import HTMLParser <EOL> try : <EOL> import urllib . request as urllib <EOL> except : <EOL> import urllib . request , urllib . parse , urllib . error <EOL> import optparse <EOL> import re <EOL> import sys <EOL> import codecs <EOL> import types <EOL> try : <EOL> from textwrap import wrap <EOL> except : <EOL> pass <EOL> UNICODE_SNOB = <NUM_LIT:1> <EOL> LINKS_EACH_PARAGRAPH = <NUM_LIT:0> <EOL> BODY_WIDTH = <NUM_LIT> <EOL> SKIP_INTERNAL_LINKS = True <EOL> INLINE_LINKS = True <EOL> GOOGLE_LIST_INDENT = <NUM_LIT> <EOL> IGNORE_ANCHORS = False <EOL> IGNORE_IMAGES = False <EOL> def name2cp ( k ) : <EOL> if k == '<STR_LIT>' : <EOL> return ord ( "<STR_LIT:'>" ) <EOL> if hasattr ( htmlentitydefs , "<STR_LIT>" ) : <EOL> return htmlentitydefs . name2codepoint [ k ] <EOL> else : <EOL> k = htmlentitydefs . entitydefs [ k ] <EOL> if k . startswith ( "<STR_LIT>" ) and k . endswith ( "<STR_LIT:;>" ) : <EOL> return int ( k [ <NUM_LIT:2> : - <NUM_LIT:1> ] ) <EOL> return ord ( codecs . latin_1_decode ( k ) [ <NUM_LIT:0> ] ) <EOL> unifiable = { '<STR_LIT>' : "<STR_LIT:'>" , '<STR_LIT>' : "<STR_LIT:'>" , '<STR_LIT>' : '<STR_LIT:">' , '<STR_LIT>' : '<STR_LIT:">' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:U+0020>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:*>' , <EOL> '<STR_LIT>' : '<STR_LIT:->' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:a>' , '<STR_LIT>' : '<STR_LIT:a>' , '<STR_LIT>' : '<STR_LIT:a>' , '<STR_LIT>' : '<STR_LIT:a>' , '<STR_LIT>' : '<STR_LIT:a>' , '<STR_LIT>' : '<STR_LIT:a>' , <EOL> '<STR_LIT>' : '<STR_LIT:e>' , '<STR_LIT>' : '<STR_LIT:e>' , '<STR_LIT>' : '<STR_LIT:e>' , '<STR_LIT>' : '<STR_LIT:e>' , <EOL> '<STR_LIT>' : '<STR_LIT:i>' , '<STR_LIT>' : '<STR_LIT:i>' , '<STR_LIT>' : '<STR_LIT:i>' , '<STR_LIT>' : '<STR_LIT:i>' , <EOL> '<STR_LIT>' : '<STR_LIT:o>' , '<STR_LIT>' : '<STR_LIT:o>' , '<STR_LIT>' : '<STR_LIT:o>' , '<STR_LIT>' : '<STR_LIT:o>' , '<STR_LIT>' : '<STR_LIT:o>' , <EOL> '<STR_LIT>' : '<STR_LIT:u>' , '<STR_LIT>' : '<STR_LIT:u>' , '<STR_LIT>' : '<STR_LIT:u>' , '<STR_LIT>' : '<STR_LIT:u>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> unifiable_n = { } <EOL> for k in list ( unifiable . keys ( ) ) : <EOL> unifiable_n [ name2cp ( k ) ] = unifiable [ k ] <EOL> def charref ( name ) : <EOL> if name [ <NUM_LIT:0> ] in [ '<STR_LIT:x>' , '<STR_LIT:X>' ] : <EOL> c = int ( name [ <NUM_LIT:1> : ] , <NUM_LIT:16> ) <EOL> else : <EOL> c = int ( name ) <EOL> if not UNICODE_SNOB and c in list ( unifiable_n . keys ( ) ) : <EOL> return unifiable_n [ c ] <EOL> else : <EOL> try : <EOL> return chr ( c ) <EOL> except NameError : <EOL> return chr ( c ) <EOL> def entityref ( c ) : <EOL> if not UNICODE_SNOB and c in list ( unifiable . keys ( ) ) : <EOL> return unifiable [ c ] <EOL> else : <EOL> try : <EOL> name2cp ( c ) <EOL> except KeyError : <EOL> return "<STR_LIT:&>" + c + '<STR_LIT:;>' <EOL> else : <EOL> try : <EOL> return chr ( name2cp ( c ) ) <EOL> except NameError : <EOL> return chr ( name2cp ( c ) ) <EOL> def replaceEntities ( s ) : <EOL> s = s . group ( <NUM_LIT:1> ) <EOL> if s [ <NUM_LIT:0> ] == "<STR_LIT:#>" : <EOL> return charref ( s [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> return entityref ( s ) <EOL> r_unescape = re . compile ( r"<STR_LIT>" ) <EOL> def unescape ( s ) : <EOL> return r_unescape . sub ( replaceEntities , s ) <EOL> def onlywhite ( line ) : <EOL> """<STR_LIT>""" <EOL> for c in line : <EOL> if c is not '<STR_LIT:U+0020>' and c is not '<STR_LIT:U+0020>' : <EOL> return c is '<STR_LIT:U+0020>' <EOL> return line <EOL> def optwrap ( text ) : <EOL> """<STR_LIT>""" <EOL> if not BODY_WIDTH : <EOL> return text <EOL> assert wrap , "<STR_LIT>" <EOL> result = '<STR_LIT>' <EOL> newlines = <NUM_LIT:0> <EOL> for para in text . split ( "<STR_LIT:\n>" ) : <EOL> if len ( para ) > <NUM_LIT:0> : <EOL> if para [ <NUM_LIT:0> ] != '<STR_LIT:U+0020>' and para [ <NUM_LIT:0> ] != '<STR_LIT:->' and para [ <NUM_LIT:0> ] != '<STR_LIT:*>' : <EOL> for line in wrap ( para , BODY_WIDTH ) : <EOL> result += line + "<STR_LIT:\n>" <EOL> result += "<STR_LIT:\n>" <EOL> newlines = <NUM_LIT:2> <EOL> else : <EOL> if not onlywhite ( para ) : <EOL> result += para + "<STR_LIT:\n>" <EOL> newlines = <NUM_LIT:1> <EOL> else : <EOL> if newlines < <NUM_LIT:2> : <EOL> result += "<STR_LIT:\n>" <EOL> newlines += <NUM_LIT:1> <EOL> return result <EOL> def hn ( tag ) : <EOL> if tag [ <NUM_LIT:0> ] == '<STR_LIT:h>' and len ( tag ) == <NUM_LIT:2> : <EOL> try : <EOL> n = int ( tag [ <NUM_LIT:1> ] ) <EOL> if n in range ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> return n <EOL> except ValueError : <EOL> return <NUM_LIT:0> <EOL> def dumb_property_dict ( style ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( x . strip ( ) , y . strip ( ) ) for x , y in [ z . split ( '<STR_LIT::>' , <NUM_LIT:1> ) for z in style . split ( '<STR_LIT:;>' ) if '<STR_LIT::>' in z ] ] ) <EOL> def dumb_css_parser ( data ) : <EOL> """<STR_LIT>""" <EOL> importIndex = data . find ( '<STR_LIT>' ) <EOL> while importIndex != - <NUM_LIT:1> : <EOL> data = data [ <NUM_LIT:0> : importIndex ] + data [ data . find ( '<STR_LIT:;>' , importIndex ) + <NUM_LIT:1> : ] <EOL> importIndex = data . find ( '<STR_LIT>' ) <EOL> elements = [ x . split ( '<STR_LIT:{>' ) for x in data . split ( '<STR_LIT:}>' ) if '<STR_LIT:{>' in x . strip ( ) ] <EOL> elements = dict ( [ ( a . strip ( ) , dumb_property_dict ( b ) ) for a , b in elements ] ) <EOL> return elements <EOL> def element_style ( attrs , style_def , parent_style ) : <EOL> """<STR_LIT>""" <EOL> style = parent_style . copy ( ) <EOL> if '<STR_LIT:class>' in attrs : <EOL> for css_class in attrs [ '<STR_LIT:class>' ] . split ( ) : <EOL> css_style = style_def [ '<STR_LIT:.>' + css_class ] <EOL> style . update ( css_style ) <EOL> if '<STR_LIT>' in attrs : <EOL> immediate_style = dumb_property_dict ( attrs [ '<STR_LIT>' ] ) <EOL> style . update ( immediate_style ) <EOL> return style <EOL> def google_list_style ( style ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in style : <EOL> list_style = style [ '<STR_LIT>' ] <EOL> if list_style in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:none>' ] : <EOL> return '<STR_LIT>' <EOL> return '<STR_LIT>' <EOL> def google_nest_count ( style ) : <EOL> """<STR_LIT>""" <EOL> nest_count = <NUM_LIT:0> <EOL> if '<STR_LIT>' in style : <EOL> nest_count = old_div ( int ( style [ '<STR_LIT>' ] [ : - <NUM_LIT:2> ] ) , GOOGLE_LIST_INDENT ) <EOL> return nest_count <EOL> def google_has_height ( style ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in style : <EOL> return True <EOL> return False <EOL> def google_text_emphasis ( style ) : <EOL> """<STR_LIT>""" <EOL> emphasis = [ ] <EOL> if '<STR_LIT>' in style : <EOL> emphasis . append ( style [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in style : <EOL> emphasis . append ( style [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in style : <EOL> emphasis . append ( style [ '<STR_LIT>' ] ) <EOL> return emphasis <EOL> def google_fixed_width_font ( style ) : <EOL> """<STR_LIT>""" <EOL> font_family = '<STR_LIT>' <EOL> if '<STR_LIT>' in style : <EOL> font_family = style [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' == font_family or '<STR_LIT>' == font_family : <EOL> return True <EOL> return False <EOL> def list_numbering_start ( attrs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:start>' in attrs : <EOL> return int ( attrs [ '<STR_LIT:start>' ] ) - <NUM_LIT:1> <EOL> else : <EOL> return <NUM_LIT:0> <EOL> class _html2text ( HTMLParser ) : <EOL> def __init__ ( self , out = None , baseurl = '<STR_LIT>' ) : <EOL> HTMLParser . __init__ ( self ) <EOL> if out is None : <EOL> self . out = self . outtextf <EOL> else : <EOL> self . out = out <EOL> self . outtextlist = [ ] <EOL> try : <EOL> self . outtext = str ( ) <EOL> except NameError : <EOL> self . outtext = str ( ) <EOL> self . quiet = <NUM_LIT:0> <EOL> self . p_p = <NUM_LIT:0> <EOL> self . outcount = <NUM_LIT:0> <EOL> self . start = <NUM_LIT:1> <EOL> self . space = <NUM_LIT:0> <EOL> self . a = [ ] <EOL> self . astack = [ ] <EOL> self . acount = <NUM_LIT:0> <EOL> self . list = [ ] <EOL> self . blockquote = <NUM_LIT:0> <EOL> self . pre = <NUM_LIT:0> <EOL> self . startpre = <NUM_LIT:0> <EOL> self . code = False <EOL> self . br_toggle = '<STR_LIT>' <EOL> self . lastWasNL = <NUM_LIT:0> <EOL> self . lastWasList = False <EOL> self . style = <NUM_LIT:0> <EOL> self . style_def = { } <EOL> self . tag_stack = [ ] <EOL> self . emphasis = <NUM_LIT:0> <EOL> self . drop_white_space = <NUM_LIT:0> <EOL> self . inheader = False <EOL> self . abbr_title = None <EOL> self . abbr_data = None <EOL> self . abbr_list = { } <EOL> self . baseurl = baseurl <EOL> if options . google_doc : <EOL> del unifiable_n [ name2cp ( '<STR_LIT>' ) ] <EOL> unifiable [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> def feed ( self , data ) : <EOL> data = data . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> HTMLParser . feed ( self , data ) <EOL> def outtextf ( self , s ) : <EOL> self . outtextlist . append ( s ) <EOL> if s : <EOL> self . lastWasNL = s [ - <NUM_LIT:1> ] == '<STR_LIT:\n>' <EOL> def close ( self ) : <EOL> HTMLParser . close ( self ) <EOL> self . pbr ( ) <EOL> self . o ( '<STR_LIT>' , <NUM_LIT:0> , '<STR_LIT:end>' ) <EOL> self . outtext = self . outtext . join ( self . outtextlist ) <EOL> if options . google_doc : <EOL> self . outtext = self . outtext . replace ( '<STR_LIT>' , '<STR_LIT:U+0020>' ) <EOL> return self . outtext <EOL> def handle_charref ( self , c ) : <EOL> self . o ( charref ( c ) , <NUM_LIT:1> ) <EOL> def handle_entityref ( self , c ) : <EOL> self . o ( entityref ( c ) , <NUM_LIT:1> ) <EOL> def handle_starttag ( self , tag , attrs ) : <EOL> self . handle_tag ( tag , attrs , <NUM_LIT:1> ) <EOL> def handle_endtag ( self , tag ) : <EOL> self . handle_tag ( tag , None , <NUM_LIT:0> ) <EOL> def previousIndex ( self , attrs ) : <EOL> """<STR_LIT>""" <EOL> if not has_key ( attrs , '<STR_LIT>' ) : <EOL> return None <EOL> i = - <NUM_LIT:1> <EOL> for a in self . a : <EOL> i += <NUM_LIT:1> <EOL> match = <NUM_LIT:0> <EOL> if has_key ( a , '<STR_LIT>' ) and a [ '<STR_LIT>' ] == attrs [ '<STR_LIT>' ] : <EOL> if has_key ( a , '<STR_LIT:title>' ) or has_key ( attrs , '<STR_LIT:title>' ) : <EOL> if ( has_key ( a , '<STR_LIT:title>' ) and has_key ( attrs , '<STR_LIT:title>' ) and <EOL> a [ '<STR_LIT:title>' ] == attrs [ '<STR_LIT:title>' ] ) : <EOL> match = True <EOL> else : <EOL> match = True <EOL> if match : <EOL> return i <EOL> def drop_last ( self , nLetters ) : <EOL> if not self . quiet : <EOL> self . outtext = self . outtext [ : - nLetters ] <EOL> def handle_emphasis ( self , start , tag_style , parent_style ) : <EOL> """<STR_LIT>""" <EOL> tag_emphasis = google_text_emphasis ( tag_style ) <EOL> parent_emphasis = google_text_emphasis ( parent_style ) <EOL> strikethrough = '<STR_LIT>' in tag_emphasis and options . hide_strikethrough <EOL> bold = '<STR_LIT>' in tag_emphasis and not '<STR_LIT>' in parent_emphasis <EOL> italic = '<STR_LIT>' in tag_emphasis and not '<STR_LIT>' in parent_emphasis <EOL> fixed = google_fixed_width_font ( tag_style ) and not google_fixed_width_font ( parent_style ) and not self . pre <EOL> if start : <EOL> if bold or italic or fixed : <EOL> self . emphasis += <NUM_LIT:1> <EOL> if strikethrough : <EOL> self . quiet += <NUM_LIT:1> <EOL> if italic : <EOL> self . o ( "<STR_LIT:_>" ) <EOL> self . drop_white_space += <NUM_LIT:1> <EOL> if bold : <EOL> self . o ( "<STR_LIT>" ) <EOL> self . drop_white_space += <NUM_LIT:1> <EOL> if fixed : <EOL> self . o ( '<STR_LIT>' ) <EOL> self . drop_white_space += <NUM_LIT:1> <EOL> self . code = True <EOL> else : <EOL> if bold or italic or fixed : <EOL> self . emphasis -= <NUM_LIT:1> <EOL> self . space = <NUM_LIT:0> <EOL> self . outtext = self . outtext . rstrip ( ) <EOL> if fixed : <EOL> if self . drop_white_space : <EOL> self . drop_last ( <NUM_LIT:1> ) <EOL> self . drop_white_space -= <NUM_LIT:1> <EOL> else : <EOL> self . o ( '<STR_LIT>' ) <EOL> self . code = False <EOL> if bold : <EOL> if self . drop_white_space : <EOL> self . drop_last ( <NUM_LIT:2> ) <EOL> self . drop_white_space -= <NUM_LIT:1> <EOL> else : <EOL> self . o ( "<STR_LIT>" ) <EOL> if italic : <EOL> if self . drop_white_space : <EOL> self . drop_last ( <NUM_LIT:1> ) <EOL> self . drop_white_space -= <NUM_LIT:1> <EOL> else : <EOL> self . o ( "<STR_LIT:_>" ) <EOL> if ( bold or italic ) and not self . emphasis : <EOL> self . o ( "<STR_LIT:U+0020>" ) <EOL> if strikethrough : <EOL> self . quiet -= <NUM_LIT:1> <EOL> def handle_tag ( self , tag , attrs , start ) : <EOL> if attrs is None : <EOL> attrs = { } <EOL> else : <EOL> attrs = dict ( attrs ) <EOL> if options . google_doc : <EOL> parent_style = { } <EOL> if start : <EOL> if self . tag_stack : <EOL> parent_style = self . tag_stack [ - <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> tag_style = element_style ( attrs , self . style_def , parent_style ) <EOL> self . tag_stack . append ( ( tag , attrs , tag_style ) ) <EOL> else : <EOL> dummy , attrs , tag_style = self . tag_stack . pop ( ) <EOL> if self . tag_stack : <EOL> parent_style = self . tag_stack [ - <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> if hn ( tag ) : <EOL> self . p ( ) <EOL> if start : <EOL> self . inheader = True <EOL> self . o ( hn ( tag ) * "<STR_LIT:#>" + '<STR_LIT:U+0020>' ) <EOL> else : <EOL> self . inheader = False <EOL> return <EOL> if tag in [ '<STR_LIT:p>' , '<STR_LIT>' ] : <EOL> if options . google_doc : <EOL> if start and google_has_height ( tag_style ) : <EOL> self . p ( ) <EOL> else : <EOL> self . soft_br ( ) <EOL> else : <EOL> self . p ( ) <EOL> if tag == "<STR_LIT>" and start : <EOL> self . o ( "<STR_LIT>" ) <EOL> if tag == "<STR_LIT>" and start : <EOL> self . p ( ) <EOL> self . o ( "<STR_LIT>" ) <EOL> self . p ( ) <EOL> if tag in [ "<STR_LIT>" , "<STR_LIT>" , '<STR_LIT>' ] : <EOL> if start : <EOL> self . quiet += <NUM_LIT:1> <EOL> else : <EOL> self . quiet -= <NUM_LIT:1> <EOL> if tag == "<STR_LIT>" : <EOL> if start : <EOL> self . style += <NUM_LIT:1> <EOL> else : <EOL> self . style -= <NUM_LIT:1> <EOL> if tag in [ "<STR_LIT:body>" ] : <EOL> self . quiet = <NUM_LIT:0> <EOL> if tag == "<STR_LIT>" : <EOL> if start : <EOL> self . p ( ) <EOL> self . o ( '<STR_LIT>' , <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> self . start = <NUM_LIT:1> <EOL> self . blockquote += <NUM_LIT:1> <EOL> else : <EOL> self . blockquote -= <NUM_LIT:1> <EOL> self . p ( ) <EOL> if tag in [ '<STR_LIT>' , '<STR_LIT:i>' , '<STR_LIT:u>' ] : <EOL> self . o ( "<STR_LIT:_>" ) <EOL> if tag in [ '<STR_LIT>' , '<STR_LIT:b>' ] : <EOL> self . o ( "<STR_LIT>" ) <EOL> if tag in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if start : <EOL> self . o ( "<STR_LIT:<>" + tag + "<STR_LIT:>>" ) <EOL> else : <EOL> self . o ( "<STR_LIT>" + tag + "<STR_LIT:>>" ) <EOL> if options . google_doc : <EOL> if not self . inheader : <EOL> self . handle_emphasis ( start , tag_style , parent_style ) <EOL> if tag == "<STR_LIT:code>" and not self . pre : <EOL> self . o ( '<STR_LIT>' ) <EOL> if tag == "<STR_LIT>" : <EOL> if start : <EOL> self . abbr_title = None <EOL> self . abbr_data = '<STR_LIT>' <EOL> if has_key ( attrs , '<STR_LIT:title>' ) : <EOL> self . abbr_title = attrs [ '<STR_LIT:title>' ] <EOL> else : <EOL> if self . abbr_title != None : <EOL> self . abbr_list [ self . abbr_data ] = self . abbr_title <EOL> self . abbr_title = None <EOL> self . abbr_data = '<STR_LIT>' <EOL> if tag == "<STR_LIT:a>" and not IGNORE_ANCHORS : <EOL> if start : <EOL> if has_key ( attrs , '<STR_LIT>' ) and not ( SKIP_INTERNAL_LINKS and attrs [ '<STR_LIT>' ] . startswith ( '<STR_LIT:#>' ) ) : <EOL> self . astack . append ( attrs ) <EOL> self . o ( "<STR_LIT:[>" ) <EOL> else : <EOL> self . astack . append ( None ) <EOL> else : <EOL> if self . astack : <EOL> a = self . astack . pop ( ) <EOL> if a : <EOL> if INLINE_LINKS : <EOL> self . o ( "<STR_LIT>" + a [ '<STR_LIT>' ] + "<STR_LIT:)>" ) <EOL> else : <EOL> i = self . previousIndex ( a ) <EOL> if i is not None : <EOL> a = self . a [ i ] <EOL> else : <EOL> self . acount += <NUM_LIT:1> <EOL> a [ '<STR_LIT:count>' ] = self . acount <EOL> a [ '<STR_LIT>' ] = self . outcount <EOL> self . a . append ( a ) <EOL> self . o ( "<STR_LIT>" + str ( a [ '<STR_LIT:count>' ] ) + "<STR_LIT:]>" ) <EOL> if tag == "<STR_LIT>" and start and not IGNORE_IMAGES : <EOL> if has_key ( attrs , '<STR_LIT:src>' ) : <EOL> attrs [ '<STR_LIT>' ] = attrs [ '<STR_LIT:src>' ] <EOL> alt = attrs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if INLINE_LINKS : <EOL> self . o ( "<STR_LIT>" ) <EOL> self . o ( alt ) <EOL> self . o ( "<STR_LIT>" + attrs [ '<STR_LIT>' ] + "<STR_LIT:)>" ) <EOL> else : <EOL> i = self . previousIndex ( attrs ) <EOL> if i is not None : <EOL> attrs = self . a [ i ] <EOL> else : <EOL> self . acount += <NUM_LIT:1> <EOL> attrs [ '<STR_LIT:count>' ] = self . acount <EOL> attrs [ '<STR_LIT>' ] = self . outcount <EOL> self . a . append ( attrs ) <EOL> self . o ( "<STR_LIT>" ) <EOL> self . o ( alt ) <EOL> self . o ( "<STR_LIT>" + str ( attrs [ '<STR_LIT:count>' ] ) + "<STR_LIT:]>" ) <EOL> if tag == '<STR_LIT>' and start : <EOL> self . p ( ) <EOL> if tag == '<STR_LIT>' and not start : <EOL> self . pbr ( ) <EOL> if tag == '<STR_LIT>' and start : <EOL> self . o ( '<STR_LIT:U+0020>' ) <EOL> if tag == '<STR_LIT>' and not start : <EOL> self . pbr ( ) <EOL> if tag in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> if ( not self . list ) and ( not self . lastWasList ) : <EOL> self . p ( ) <EOL> if start : <EOL> if options . google_doc : <EOL> list_style = google_list_style ( tag_style ) <EOL> else : <EOL> list_style = tag <EOL> numbering_start = list_numbering_start ( attrs ) <EOL> self . list . append ( { '<STR_LIT:name>' : list_style , '<STR_LIT>' : numbering_start } ) <EOL> else : <EOL> if self . list : <EOL> self . list . pop ( ) <EOL> self . lastWasList = True <EOL> else : <EOL> self . lastWasList = False <EOL> if tag == '<STR_LIT>' : <EOL> self . pbr ( ) <EOL> if start : <EOL> if self . list : <EOL> li = self . list [ - <NUM_LIT:1> ] <EOL> else : <EOL> li = { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } <EOL> if options . google_doc : <EOL> nest_count = google_nest_count ( tag_style ) <EOL> else : <EOL> nest_count = len ( self . list ) <EOL> self . o ( "<STR_LIT:U+0020>" * nest_count ) <EOL> if li [ '<STR_LIT:name>' ] == "<STR_LIT>" : <EOL> self . o ( options . ul_item_mark + "<STR_LIT:U+0020>" ) <EOL> elif li [ '<STR_LIT:name>' ] == "<STR_LIT>" : <EOL> li [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> self . o ( str ( li [ '<STR_LIT>' ] ) + "<STR_LIT>" ) <EOL> self . start = <NUM_LIT:1> <EOL> if tag in [ "<STR_LIT>" , "<STR_LIT>" ] and start : <EOL> self . p ( ) <EOL> if tag == '<STR_LIT>' : <EOL> self . pbr ( ) <EOL> if tag == "<STR_LIT>" : <EOL> if start : <EOL> self . startpre = <NUM_LIT:1> <EOL> self . pre = <NUM_LIT:1> <EOL> else : <EOL> self . pre = <NUM_LIT:0> <EOL> self . p ( ) <EOL> def pbr ( self ) : <EOL> if self . p_p == <NUM_LIT:0> : <EOL> self . p_p = <NUM_LIT:1> <EOL> def p ( self ) : <EOL> self . p_p = <NUM_LIT:2> <EOL> def soft_br ( self ) : <EOL> self . pbr ( ) <EOL> self . br_toggle = '<STR_LIT:U+0020>' <EOL> def o ( self , data , puredata = <NUM_LIT:0> , force = <NUM_LIT:0> ) : <EOL> if self . abbr_data is not None : <EOL> self . abbr_data += data <EOL> if not self . quiet : <EOL> if options . google_doc : <EOL> lstripped_data = data . lstrip ( ) <EOL> if self . drop_white_space and not ( self . pre or self . code ) : <EOL> data = lstripped_data <EOL> if lstripped_data != '<STR_LIT>' : <EOL> self . drop_white_space = <NUM_LIT:0> <EOL> if puredata and not self . pre : <EOL> data = re . sub ( '<STR_LIT>' , '<STR_LIT:U+0020>' , data ) <EOL> if data and data [ <NUM_LIT:0> ] == '<STR_LIT:U+0020>' : <EOL> self . space = <NUM_LIT:1> <EOL> data = data [ <NUM_LIT:1> : ] <EOL> if not data and not force : <EOL> return <EOL> if self . startpre : <EOL> self . startpre = <NUM_LIT:0> <EOL> bq = ( "<STR_LIT:>>" * self . blockquote ) <EOL> if not ( force and data and data [ <NUM_LIT:0> ] == "<STR_LIT:>>" ) and self . blockquote : <EOL> bq += "<STR_LIT:U+0020>" <EOL> if self . pre : <EOL> bq += "<STR_LIT:U+0020>" <EOL> data = data . replace ( "<STR_LIT:\n>" , "<STR_LIT:\n>" + bq ) <EOL> if self . start : <EOL> self . space = <NUM_LIT:0> <EOL> self . p_p = <NUM_LIT:0> <EOL> self . start = <NUM_LIT:0> <EOL> if force == '<STR_LIT:end>' : <EOL> self . p_p = <NUM_LIT:0> <EOL> self . out ( "<STR_LIT:\n>" ) <EOL> self . space = <NUM_LIT:0> <EOL> if self . p_p : <EOL> self . out ( ( self . br_toggle + '<STR_LIT:\n>' + bq ) * self . p_p ) <EOL> self . space = <NUM_LIT:0> <EOL> self . br_toggle = '<STR_LIT>' <EOL> if self . space : <EOL> if not self . lastWasNL : <EOL> self . out ( '<STR_LIT:U+0020>' ) <EOL> self . space = <NUM_LIT:0> <EOL> if self . a and ( ( self . p_p == <NUM_LIT:2> and LINKS_EACH_PARAGRAPH ) or force == "<STR_LIT:end>" ) : <EOL> if force == "<STR_LIT:end>" : <EOL> self . out ( "<STR_LIT:\n>" ) <EOL> newa = [ ] <EOL> for link in self . a : <EOL> if self . outcount > link [ '<STR_LIT>' ] : <EOL> self . out ( "<STR_LIT>" + str ( link [ '<STR_LIT:count>' ] ) + "<STR_LIT>" + <EOL> urllib . parse . urljoin ( self . baseurl , link [ '<STR_LIT>' ] ) ) <EOL> if has_key ( link , '<STR_LIT:title>' ) : <EOL> self . out ( "<STR_LIT>" + link [ '<STR_LIT:title>' ] + "<STR_LIT:)>" ) <EOL> self . out ( "<STR_LIT:\n>" ) <EOL> else : <EOL> newa . append ( link ) <EOL> if self . a != newa : <EOL> self . out ( "<STR_LIT:\n>" ) <EOL> self . a = newa <EOL> if self . abbr_list and force == "<STR_LIT:end>" : <EOL> for abbr , definition in list ( self . abbr_list . items ( ) ) : <EOL> self . out ( "<STR_LIT>" + abbr + "<STR_LIT>" + definition + "<STR_LIT:\n>" ) <EOL> self . p_p = <NUM_LIT:0> <EOL> self . out ( data ) <EOL> self . outcount += <NUM_LIT:1> <EOL> def handle_data ( self , data ) : <EOL> if r'<STR_LIT>' in data : <EOL> self . quiet -= <NUM_LIT:1> <EOL> if self . style : <EOL> self . style_def . update ( dumb_css_parser ( data ) ) <EOL> self . o ( data , <NUM_LIT:1> ) <EOL> def unknown_decl ( self , data ) : <EOL> pass <EOL> def wrapwrite ( text ) : <EOL> text = text . encode ( '<STR_LIT:utf-8>' ) <EOL> try : <EOL> sys . stdout . buffer . write ( text ) <EOL> except AttributeError : <EOL> sys . stdout . write ( text ) <EOL> def html2text_file ( html , out = wrapwrite , baseurl = '<STR_LIT>' ) : <EOL> h = _html2text ( out , baseurl ) <EOL> h . feed ( html ) <EOL> h . feed ( "<STR_LIT>" ) <EOL> return h . close ( ) <EOL> def html2text ( html , baseurl = '<STR_LIT>' ) : <EOL> return optwrap ( html2text_file ( html , None , baseurl ) ) <EOL> class Storage ( object ) : <EOL> pass <EOL> options = Storage ( ) <EOL> options . google_doc = False <EOL> options . ul_item_mark = '<STR_LIT:*>' <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> baseurl = '<STR_LIT>' <EOL> p = optparse . OptionParser ( '<STR_LIT>' , <EOL> version = '<STR_LIT>' + __version__ ) <EOL> p . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> default = False , help = "<STR_LIT>" ) <EOL> p . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> default = False , help = "<STR_LIT>" ) <EOL> p . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:int>" , <EOL> default = <NUM_LIT> , help = "<STR_LIT>" ) <EOL> p . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store>" , type = "<STR_LIT:int>" , <EOL> default = GOOGLE_LIST_INDENT , help = "<STR_LIT>" ) <EOL> p . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , <EOL> default = False , help = "<STR_LIT>" ) <EOL> ( options , args ) = p . parse_args ( ) <EOL> if options . ul_style_dash : <EOL> options . ul_item_mark = '<STR_LIT:->' <EOL> else : <EOL> options . ul_item_mark = '<STR_LIT:*>' <EOL> BODY_WIDTH = options . body_width <EOL> GOOGLE_LIST_INDENT = options . list_indent <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> file_ = args [ <NUM_LIT:0> ] <EOL> encoding = None <EOL> if len ( args ) == <NUM_LIT:2> : <EOL> encoding = args [ <NUM_LIT:1> ] <EOL> if len ( args ) > <NUM_LIT:2> : <EOL> p . error ( '<STR_LIT>' ) <EOL> if file_ . startswith ( '<STR_LIT>' ) or file_ . startswith ( '<STR_LIT>' ) : <EOL> baseurl = file_ <EOL> j = urllib . request . urlopen ( baseurl ) <EOL> text = j . read ( ) <EOL> if encoding is None : <EOL> try : <EOL> from feedparser import _getCharacterEncoding as enc <EOL> except ImportError : <EOL> enc = lambda x , y : ( '<STR_LIT:utf-8>' , <NUM_LIT:1> ) <EOL> encoding = enc ( j . headers , text ) [ <NUM_LIT:0> ] <EOL> if encoding == '<STR_LIT>' : <EOL> encoding = '<STR_LIT:utf-8>' <EOL> data = text . decode ( encoding ) <EOL> else : <EOL> data = open ( file_ , '<STR_LIT:rb>' ) . read ( ) <EOL> if encoding is None : <EOL> try : <EOL> from chardet import detect <EOL> except ImportError : <EOL> detect = lambda x : { '<STR_LIT>' : '<STR_LIT:utf-8>' } <EOL> encoding = detect ( data ) [ '<STR_LIT>' ] <EOL> data = data . decode ( encoding ) <EOL> else : <EOL> data = sys . stdin . read ( ) <EOL> wrapwrite ( html2text ( data , baseurl ) ) </s>
