<s> __date__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> __doc__ = """<STR_LIT>""" % ( __version__ , __date__ ) <EOL> import string <EOL> class element : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tag , case = '<STR_LIT>' , parent = None ) : <EOL> self . parent = parent <EOL> if case == '<STR_LIT>' : <EOL> self . tag = tag . lower ( ) <EOL> else : <EOL> self . tag = tag . upper ( ) <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> raise ArgumentError ( self . tag ) <EOL> if self . parent is not None and self . parent . class_ is not None : <EOL> if '<STR_LIT>' not in kwargs : <EOL> kwargs [ '<STR_LIT>' ] = self . parent . class_ <EOL> if self . parent is None and len ( args ) == <NUM_LIT:1> : <EOL> x = [ self . render ( self . tag , False , myarg , mydict ) for myarg , mydict in _argsdicts ( args , kwargs ) ] <EOL> return '<STR_LIT:\n>' . join ( x ) <EOL> elif self . parent is None and len ( args ) == <NUM_LIT:0> : <EOL> x = [ self . render ( self . tag , True , myarg , mydict ) for myarg , mydict in _argsdicts ( args , kwargs ) ] <EOL> return '<STR_LIT:\n>' . join ( x ) <EOL> if self . tag in self . parent . twotags : <EOL> for myarg , mydict in _argsdicts ( args , kwargs ) : <EOL> self . render ( self . tag , False , myarg , mydict ) <EOL> elif self . tag in self . parent . onetags : <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> for myarg , mydict in _argsdicts ( args , kwargs ) : <EOL> self . render ( self . tag , True , myarg , mydict ) <EOL> else : <EOL> raise ClosingError ( self . tag ) <EOL> elif self . parent . mode == '<STR_LIT>' and self . tag in self . parent . deptags : <EOL> raise DeprecationError ( self . tag ) <EOL> else : <EOL> raise InvalidElementError ( self . tag , self . parent . mode ) <EOL> def render ( self , tag , single , between , kwargs ) : <EOL> """<STR_LIT>""" <EOL> out = "<STR_LIT>" % tag <EOL> for key , value in kwargs . items ( ) : <EOL> if value is not None : <EOL> key = key . strip ( '<STR_LIT:_>' ) <EOL> if key == '<STR_LIT>' : <EOL> key = '<STR_LIT>' <EOL> elif key == '<STR_LIT>' : <EOL> key = '<STR_LIT>' <EOL> out = "<STR_LIT>" % ( out , key , escape ( value ) ) <EOL> else : <EOL> out = "<STR_LIT>" % ( out , key ) <EOL> if between is not None : <EOL> out = "<STR_LIT>" % ( out , between , tag ) <EOL> else : <EOL> if single : <EOL> out = "<STR_LIT>" % out <EOL> else : <EOL> out = "<STR_LIT>" % out <EOL> if self . parent is not None : <EOL> self . parent . content . append ( out ) <EOL> else : <EOL> return out <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . tag in self . parent . twotags : <EOL> self . parent . content . append ( "<STR_LIT>" % self . tag ) <EOL> elif self . tag in self . parent . onetags : <EOL> raise ClosingError ( self . tag ) <EOL> elif self . parent . mode == '<STR_LIT>' and self . tag in self . parent . deptags : <EOL> raise DeprecationError ( self . tag ) <EOL> def open ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . tag in self . parent . twotags or self . tag in self . parent . onetags : <EOL> self . render ( self . tag , False , None , kwargs ) <EOL> elif self . mode == '<STR_LIT>' and self . tag in self . parent . deptags : <EOL> raise DeprecationError ( self . tag ) <EOL> class page : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mode = '<STR_LIT>' , case = '<STR_LIT>' , onetags = None , twotags = None , separator = '<STR_LIT:\n>' , class_ = None ) : <EOL> """<STR_LIT>""" <EOL> valid_onetags = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> valid_twotags = [ "<STR_LIT:A>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:B>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<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>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:I>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT:P>" , "<STR_LIT>" , "<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>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> deprecated_onetags = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> deprecated_twotags = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:S>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . header = [ ] <EOL> self . content = [ ] <EOL> self . footer = [ ] <EOL> self . case = case <EOL> self . separator = separator <EOL> self . _full = False <EOL> self . class_ = class_ <EOL> if mode == '<STR_LIT>' or mode == '<STR_LIT:html>' : <EOL> self . onetags = valid_onetags <EOL> self . onetags += list ( map ( str . lower , self . onetags ) ) <EOL> self . twotags = valid_twotags <EOL> self . twotags += list ( map ( str . lower , self . twotags ) ) <EOL> self . deptags = deprecated_onetags + deprecated_twotags <EOL> self . deptags += list ( map ( str . lower , self . deptags ) ) <EOL> self . mode = '<STR_LIT>' <EOL> elif mode == '<STR_LIT>' : <EOL> self . onetags = valid_onetags + deprecated_onetags <EOL> self . onetags += list ( map ( str . lower , self . onetags ) ) <EOL> self . twotags = valid_twotags + deprecated_twotags <EOL> self . twotags += list ( map ( str . lower , self . twotags ) ) <EOL> self . mode = mode <EOL> elif mode == '<STR_LIT>' : <EOL> if onetags and twotags : <EOL> self . onetags = onetags <EOL> self . twotags = twotags <EOL> elif ( onetags and not twotags ) or ( twotags and not onetags ) : <EOL> raise CustomizationError ( ) <EOL> else : <EOL> self . onetags = russell ( ) <EOL> self . twotags = russell ( ) <EOL> self . mode = mode <EOL> else : <EOL> raise ModeError ( mode ) <EOL> def __getattr__ ( self , attr ) : <EOL> if attr . startswith ( "<STR_LIT>" ) and attr . endswith ( "<STR_LIT>" ) : <EOL> raise AttributeError ( attr ) <EOL> return element ( attr , case = self . case , parent = self ) <EOL> def __str__ ( self ) : <EOL> if self . _full and ( self . mode == '<STR_LIT>' or self . mode == '<STR_LIT>' ) : <EOL> end = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> else : <EOL> end = [ ] <EOL> return self . separator . join ( self . header + self . content + self . footer + end ) <EOL> def __call__ ( self , escape = False ) : <EOL> """<STR_LIT>""" <EOL> if escape : <EOL> return _escape ( self . __str__ ( ) ) <EOL> else : <EOL> return self . __str__ ( ) <EOL> def add ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . addcontent ( text ) <EOL> def addfooter ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . footer . append ( text ) <EOL> def addheader ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . header . append ( text ) <EOL> def addcontent ( self , text ) : <EOL> """<STR_LIT>""" <EOL> self . content . append ( text ) <EOL> def init ( self , lang = '<STR_LIT>' , css = None , metainfo = None , title = None , header = None , <EOL> footer = None , charset = None , encoding = None , doctype = None , bodyattrs = None , script = None ) : <EOL> """<STR_LIT>""" <EOL> self . _full = True <EOL> if self . mode == '<STR_LIT>' or self . mode == '<STR_LIT>' : <EOL> if doctype is None : <EOL> doctype = "<STR_LIT>" <EOL> self . header . append ( doctype ) <EOL> self . html ( lang = lang ) <EOL> self . head ( ) <EOL> if charset is not None : <EOL> self . meta ( http_equiv = '<STR_LIT:Content-Type>' , content = "<STR_LIT>" % charset ) <EOL> if metainfo is not None : <EOL> self . metainfo ( metainfo ) <EOL> if css is not None : <EOL> self . css ( css ) <EOL> if title is not None : <EOL> self . title ( title ) <EOL> if script is not None : <EOL> self . scripts ( script ) <EOL> self . head . close ( ) <EOL> if bodyattrs is not None : <EOL> self . body ( ** bodyattrs ) <EOL> else : <EOL> self . body ( ) <EOL> if header is not None : <EOL> self . content . append ( header ) <EOL> if footer is not None : <EOL> self . footer . append ( footer ) <EOL> elif self . mode == '<STR_LIT>' : <EOL> if doctype is None : <EOL> if encoding is not None : <EOL> doctype = "<STR_LIT>" % encoding <EOL> else : <EOL> doctype = "<STR_LIT>" <EOL> self . header . append ( doctype ) <EOL> def css ( self , filelist ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( filelist , str ) : <EOL> self . link ( href = filelist , rel = '<STR_LIT>' , type = '<STR_LIT>' , media = '<STR_LIT:all>' ) <EOL> else : <EOL> for file in filelist : <EOL> self . link ( href = file , rel = '<STR_LIT>' , type = '<STR_LIT>' , media = '<STR_LIT:all>' ) <EOL> def metainfo ( self , mydict ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( mydict , dict ) : <EOL> for name , content in mydict . items ( ) : <EOL> self . meta ( name = name , content = content ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def scripts ( self , mydict ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( mydict , dict ) : <EOL> for src , type in mydict . items ( ) : <EOL> self . script ( '<STR_LIT>' , src = src , type = '<STR_LIT>' % type ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> class _oneliner : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , case = '<STR_LIT>' ) : <EOL> self . case = case <EOL> def __getattr__ ( self , attr ) : <EOL> if attr . startswith ( "<STR_LIT>" ) and attr . endswith ( "<STR_LIT>" ) : <EOL> raise AttributeError ( attr ) <EOL> return element ( attr , case = self . case , parent = None ) <EOL> oneliner = _oneliner ( case = '<STR_LIT>' ) <EOL> upper_oneliner = _oneliner ( case = '<STR_LIT>' ) <EOL> def _argsdicts ( args , mydict ) : <EOL> """<STR_LIT>""" <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> args = None , <EOL> elif len ( args ) == <NUM_LIT:1> : <EOL> args = _totuple ( args [ <NUM_LIT:0> ] ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> mykeys = list ( mydict . keys ( ) ) <EOL> myvalues = list ( map ( _totuple , list ( mydict . values ( ) ) ) ) <EOL> maxlength = max ( list ( map ( len , [ args ] + myvalues ) ) ) <EOL> for i in range ( maxlength ) : <EOL> thisdict = { } <EOL> for key , value in zip ( mykeys , myvalues ) : <EOL> try : <EOL> thisdict [ key ] = value [ i ] <EOL> except IndexError : <EOL> thisdict [ key ] = value [ - <NUM_LIT:1> ] <EOL> try : <EOL> thisarg = args [ i ] <EOL> except IndexError : <EOL> thisarg = args [ - <NUM_LIT:1> ] <EOL> yield thisarg , thisdict <EOL> def _totuple ( x ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( x , str ) : <EOL> out = x , <EOL> elif isinstance ( x , ( int , float ) ) : <EOL> out = str ( x ) , <EOL> elif x is None : <EOL> out = None , <EOL> else : <EOL> out = tuple ( x ) <EOL> return out <EOL> def escape ( text , newline = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( text , str ) : <EOL> if '<STR_LIT:&>' in text : <EOL> text = text . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) <EOL> if '<STR_LIT:>>' in text : <EOL> text = text . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) <EOL> if '<STR_LIT:<>' in text : <EOL> text = text . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) <EOL> if '<STR_LIT>' in text : <EOL> text = text . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if '<STR_LIT>' in text : <EOL> text = text . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if newline : <EOL> if '<STR_LIT:\n>' in text : <EOL> text = text . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> return text <EOL> _escape = escape <EOL> def unescape ( text ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( text , str ) : <EOL> if '<STR_LIT>' in text : <EOL> text = text . replace ( '<STR_LIT>' , '<STR_LIT:&>' ) <EOL> if '<STR_LIT>' in text : <EOL> text = text . replace ( '<STR_LIT>' , '<STR_LIT:>>' ) <EOL> if '<STR_LIT>' in text : <EOL> text = text . replace ( '<STR_LIT>' , '<STR_LIT:<>' ) <EOL> if '<STR_LIT>' in text : <EOL> text = text . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return text <EOL> class dummy : <EOL> """<STR_LIT>""" <EOL> pass <EOL> doctype = dummy ( ) <EOL> doctype . frameset = "<STR_LIT>" <EOL> doctype . strict = "<STR_LIT>" <EOL> doctype . loose = "<STR_LIT>" <EOL> class russell : <EOL> """<STR_LIT>""" <EOL> def __contains__ ( self , item ) : <EOL> return True <EOL> class MarkupError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return self . message <EOL> class ClosingError ( MarkupError ) : <EOL> def __init__ ( self , tag ) : <EOL> self . message = "<STR_LIT>" % tag <EOL> class OpeningError ( MarkupError ) : <EOL> def __init__ ( self , tag ) : <EOL> self . message = "<STR_LIT>" % tag <EOL> class ArgumentError ( MarkupError ) : <EOL> def __init__ ( self , tag ) : <EOL> self . message = "<STR_LIT>" % tag <EOL> class InvalidElementError ( MarkupError ) : <EOL> def __init__ ( self , tag , mode ) : <EOL> self . message = "<STR_LIT>" % ( tag , mode ) <EOL> class DeprecationError ( MarkupError ) : <EOL> def __init__ ( self , tag ) : <EOL> self . message = "<STR_LIT>" % tag <EOL> class ModeError ( MarkupError ) : <EOL> def __init__ ( self , mode ) : <EOL> self . message = "<STR_LIT>" % mode <EOL> class CustomizationError ( MarkupError ) : <EOL> def __init__ ( self ) : <EOL> self . message = "<STR_LIT>" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> print ( __doc__ ) </s>
