imip-agent

Annotated markup.py

1072:519e1d452e0d
2016-03-06 Paul Boddie Added parsing and serialisation functions involving strings. freebusy-collections
paul@69 1
# This code is in the public domain, it comes
paul@69 2
# with absolutely no warranty and you can do
paul@69 3
# absolutely whatever you want with it.
paul@69 4
paul@69 5
__date__ = '1 October 2012'
paul@69 6
__version__ = '1.9'
paul@69 7
__doc__= """
paul@69 8
This is markup.py - a Python module that attempts to
paul@69 9
make it easier to generate HTML/XML from a Python program
paul@69 10
in an intuitive, lightweight, customizable and pythonic way.
paul@69 11
paul@69 12
The code is in the public domain.
paul@69 13
paul@69 14
Version: %s as of %s.
paul@69 15
paul@69 16
Documentation and further info is at http://markup.sourceforge.net/
paul@69 17
paul@69 18
Please send bug reports, feature requests, enhancement
paul@69 19
ideas or questions to nogradi at gmail dot com.
paul@69 20
paul@69 21
Installation: drop markup.py somewhere into your Python path.
paul@69 22
""" % ( __version__, __date__ )
paul@69 23
paul@69 24
try:
paul@69 25
    basestring
paul@69 26
    import string
paul@69 27
except:
paul@69 28
    # python 3
paul@69 29
    basestring = str
paul@69 30
    string = str
paul@69 31
paul@69 32
# tags which are reserved python keywords will be referred 
paul@69 33
# to by a leading underscore otherwise we end up with a syntax error
paul@69 34
import keyword
paul@69 35
paul@69 36
class element:
paul@69 37
    """This class handles the addition of a new element."""
paul@69 38
paul@69 39
    def __init__( self, tag, case='lower', parent=None ):
paul@69 40
        self.parent = parent
paul@69 41
paul@69 42
        if case == 'upper':
paul@69 43
            self.tag = tag.upper( )
paul@69 44
        elif case == 'lower':
paul@69 45
            self.tag = tag.lower( )
paul@69 46
        elif case =='given':
paul@69 47
            self.tag = tag
paul@69 48
        else:
paul@69 49
            self.tag = tag
paul@69 50
    
paul@69 51
    def __call__( self, *args, **kwargs ):
paul@69 52
        if len( args ) > 1:
paul@69 53
            raise ArgumentError( self.tag )
paul@69 54
paul@69 55
        # if class_ was defined in parent it should be added to every element
paul@69 56
        if self.parent is not None and self.parent.class_ is not None:
paul@69 57
            if 'class_' not in kwargs:
paul@69 58
                kwargs['class_'] = self.parent.class_
paul@69 59
            
paul@69 60
        if self.parent is None and len( args ) == 1:
paul@69 61
            x = [ self.render( self.tag, False, myarg, mydict ) for myarg, mydict in _argsdicts( args, kwargs ) ]
paul@69 62
            return '\n'.join( x )
paul@69 63
        elif self.parent is None and len( args ) == 0:
paul@69 64
            x = [ self.render( self.tag, True, myarg, mydict ) for myarg, mydict in _argsdicts( args, kwargs ) ]
paul@69 65
            return '\n'.join( x )
paul@69 66
            
paul@69 67
        if self.tag in self.parent.twotags:
paul@69 68
            for myarg, mydict in _argsdicts( args, kwargs ):
paul@69 69
                self.render( self.tag, False, myarg, mydict )
paul@69 70
        elif self.tag in self.parent.onetags:
paul@69 71
            if len( args ) == 0:
paul@69 72
                for myarg, mydict in _argsdicts( args, kwargs ):
paul@69 73
                    self.render( self.tag, True, myarg, mydict )    # here myarg is always None, because len( args ) = 0
paul@69 74
            else:
paul@69 75
                raise ClosingError( self.tag )
paul@69 76
        elif self.parent.mode == 'strict_html' and self.tag in self.parent.deptags:
paul@69 77
            raise DeprecationError( self.tag )
paul@69 78
        else:
paul@69 79
            raise InvalidElementError( self.tag, self.parent.mode )
paul@69 80
    
paul@69 81
    def render( self, tag, single, between, kwargs ):
paul@69 82
        """Append the actual tags to content."""
paul@69 83
paul@69 84
        out = "<%s" % tag
paul@69 85
        for key, value in list( kwargs.items( ) ):
paul@69 86
            if value is not None:               # when value is None that means stuff like <... checked>
paul@69 87
                key = key.strip('_')            # strip this so class_ will mean class, etc.
paul@69 88
                if key == 'http_equiv':         # special cases, maybe change _ to - overall?
paul@69 89
                    key = 'http-equiv'
paul@69 90
                elif key == 'accept_charset':
paul@69 91
                    key = 'accept-charset'
paul@69 92
                out = "%s %s=\"%s\"" % ( out, key, escape( value ) )
paul@69 93
            else:
paul@69 94
                out = "%s %s" % ( out, key )
paul@69 95
        if between is not None:
paul@789 96
            out = "%s>%s</%s>" % ( out, escape( between ), tag )
paul@69 97
        else:
paul@69 98
            if single:
paul@69 99
                out = "%s />" % out
paul@69 100
            else:
paul@69 101
                out = "%s>" % out
paul@69 102
        if self.parent is not None:
paul@69 103
            self.parent.content.append( out )
paul@69 104
        else:
paul@69 105
            return out
paul@69 106
    
paul@69 107
    def close( self ):
paul@69 108
        """Append a closing tag unless element has only opening tag."""
paul@69 109
paul@69 110
        if self.tag in self.parent.twotags:
paul@69 111
            self.parent.content.append( "</%s>" % self.tag )
paul@69 112
        elif self.tag in self.parent.onetags:
paul@69 113
            raise ClosingError( self.tag )
paul@69 114
        elif self.parent.mode == 'strict_html' and self.tag in self.parent.deptags:
paul@69 115
            raise DeprecationError( self.tag )
paul@69 116
paul@69 117
    def open( self, **kwargs ):
paul@69 118
        """Append an opening tag."""
paul@69 119
paul@69 120
        if self.tag in self.parent.twotags or self.tag in self.parent.onetags:
paul@69 121
            self.render( self.tag, False, None, kwargs )
paul@69 122
        elif self.mode == 'strict_html' and self.tag in self.parent.deptags:
paul@69 123
            raise DeprecationError( self.tag )
paul@69 124
paul@69 125
class page:
paul@69 126
    """This is our main class representing a document. Elements are added
paul@69 127
    as attributes of an instance of this class."""
paul@69 128
paul@69 129
    def __init__( self, mode='strict_html', case='lower', onetags=None, twotags=None, separator='\n', class_=None ):
paul@69 130
        """Stuff that effects the whole document.
paul@69 131
paul@69 132
        mode -- 'strict_html'   for HTML 4.01 (default)
paul@69 133
                'html'          alias for 'strict_html'
paul@69 134
                'loose_html'    to allow some deprecated elements
paul@69 135
                'xml'           to allow arbitrary elements
paul@69 136
paul@69 137
        case -- 'lower'         element names will be printed in lower case (default)
paul@69 138
                'upper'         they will be printed in upper case
paul@69 139
                'given'         element names will be printed as they are given
paul@69 140
paul@69 141
        onetags --              list or tuple of valid elements with opening tags only
paul@69 142
        twotags --              list or tuple of valid elements with both opening and closing tags
paul@69 143
                                these two keyword arguments may be used to select
paul@69 144
                                the set of valid elements in 'xml' mode
paul@69 145
                                invalid elements will raise appropriate exceptions
paul@69 146
        
paul@69 147
        separator --            string to place between added elements, defaults to newline
paul@69 148
        
paul@69 149
        class_ --               a class that will be added to every element if defined"""
paul@69 150
        
paul@69 151
        valid_onetags = [ "AREA", "BASE", "BR", "COL", "FRAME", "HR", "IMG", "INPUT", "LINK", "META", "PARAM" ]
paul@69 152
        valid_twotags = [ "A", "ABBR", "ACRONYM", "ADDRESS", "B", "BDO", "BIG", "BLOCKQUOTE", "BODY", "BUTTON",
paul@69 153
                "CAPTION", "CITE", "CODE", "COLGROUP", "DD", "DEL", "DFN", "DIV", "DL", "DT", "EM", "FIELDSET",
paul@69 154
                "FORM", "FRAMESET", "H1", "H2", "H3", "H4", "H5", "H6", "HEAD", "HTML", "I", "IFRAME", "INS",
paul@69 155
                "KBD", "LABEL", "LEGEND", "LI", "MAP", "NOFRAMES", "NOSCRIPT", "OBJECT", "OL", "OPTGROUP",
paul@69 156
                "OPTION", "P", "PRE", "Q", "SAMP", "SCRIPT", "SELECT", "SMALL", "SPAN", "STRONG", "STYLE",
paul@69 157
                "SUB", "SUP", "TABLE", "TBODY", "TD", "TEXTAREA", "TFOOT", "TH", "THEAD", "TITLE", "TR",
paul@69 158
                "TT", "UL", "VAR" ]
paul@69 159
        deprecated_onetags = [ "BASEFONT", "ISINDEX" ]
paul@69 160
        deprecated_twotags = [ "APPLET", "CENTER", "DIR", "FONT", "MENU", "S", "STRIKE", "U" ]
paul@69 161
paul@69 162
        self.header = [ ]
paul@69 163
        self.content = [ ]
paul@69 164
        self.footer = [ ]
paul@69 165
        self.case = case
paul@69 166
        self.separator = separator
paul@69 167
paul@69 168
        # init( ) sets it to True so we know that </body></html> has to be printed at the end
paul@69 169
        self._full = False
paul@69 170
        self.class_= class_
paul@69 171
paul@69 172
        if mode == 'strict_html' or mode == 'html':
paul@69 173
            self.onetags = valid_onetags
paul@69 174
            self.onetags += list( map( string.lower, self.onetags ) )
paul@69 175
            self.twotags = valid_twotags
paul@69 176
            self.twotags += list( map( string.lower, self.twotags ) )
paul@69 177
            self.deptags = deprecated_onetags + deprecated_twotags
paul@69 178
            self.deptags += list( map( string.lower, self.deptags ) )
paul@69 179
            self.mode = 'strict_html'
paul@69 180
        elif mode == 'loose_html':
paul@69 181
            self.onetags = valid_onetags + deprecated_onetags 
paul@69 182
            self.onetags += list( map( string.lower, self.onetags ) )
paul@69 183
            self.twotags = valid_twotags + deprecated_twotags
paul@69 184
            self.twotags += list( map( string.lower, self.twotags ) )
paul@69 185
            self.mode = mode
paul@69 186
        elif mode == 'xml':
paul@69 187
            if onetags and twotags:
paul@69 188
                self.onetags = onetags
paul@69 189
                self.twotags = twotags
paul@69 190
            elif ( onetags and not twotags ) or ( twotags and not onetags ):
paul@69 191
                raise CustomizationError( )
paul@69 192
            else:
paul@69 193
                self.onetags = russell( )
paul@69 194
                self.twotags = russell( )
paul@69 195
            self.mode = mode
paul@69 196
        else:
paul@69 197
            raise ModeError( mode )
paul@69 198
paul@69 199
    def __getattr__( self, attr ):
paul@69 200
paul@69 201
        # tags should start with double underscore
paul@69 202
        if attr.startswith("__") and attr.endswith("__"):
paul@69 203
            raise AttributeError( attr )
paul@69 204
        # tag with single underscore should be a reserved keyword
paul@69 205
        if attr.startswith( '_' ):
paul@69 206
            attr = attr.lstrip( '_' ) 
paul@69 207
            if attr not in keyword.kwlist:
paul@69 208
                raise AttributeError( attr )
paul@69 209
paul@69 210
        return element( attr, case=self.case, parent=self )
paul@69 211
paul@69 212
    def __str__( self ):
paul@69 213
        
paul@69 214
        if self._full and ( self.mode == 'strict_html' or self.mode == 'loose_html' ):
paul@69 215
            end = [ '</body>', '</html>' ]
paul@69 216
        else:
paul@69 217
            end = [ ]
paul@69 218
paul@69 219
        return self.separator.join( self.header + self.content + self.footer + end )
paul@69 220
paul@69 221
    def __call__( self, escape=False ):
paul@69 222
        """Return the document as a string.
paul@69 223
paul@69 224
        escape --   False   print normally
paul@69 225
                    True    replace < and > by &lt; and &gt;
paul@69 226
                            the default escape sequences in most browsers"""
paul@69 227
paul@69 228
        if escape:
paul@69 229
            return _escape( self.__str__( ) )
paul@69 230
        else:
paul@69 231
            return self.__str__( )
paul@69 232
paul@69 233
    def add( self, text ):
paul@69 234
        """This is an alias to addcontent."""
paul@69 235
        self.addcontent( text )
paul@69 236
paul@69 237
    def addfooter( self, text ):
paul@69 238
        """Add some text to the bottom of the document"""
paul@793 239
        self.footer.append( escape( text ) )
paul@69 240
paul@69 241
    def addheader( self, text ):
paul@69 242
        """Add some text to the top of the document"""
paul@793 243
        self.header.append( escape( text ) )
paul@69 244
paul@69 245
    def addcontent( self, text ):
paul@69 246
        """Add some text to the main part of the document"""
paul@793 247
        self.content.append( escape( text ) )
paul@69 248
paul@69 249
paul@69 250
    def init( self, lang='en', css=None, metainfo=None, title=None, header=None,
paul@69 251
              footer=None, charset=None, encoding=None, doctype=None, bodyattrs=None, script=None, base=None ):
paul@69 252
        """This method is used for complete documents with appropriate
paul@69 253
        doctype, encoding, title, etc information. For an HTML/XML snippet
paul@69 254
        omit this method.
paul@69 255
paul@69 256
        lang --     language, usually a two character string, will appear
paul@69 257
                    as <html lang='en'> in html mode (ignored in xml mode)
paul@69 258
        
paul@69 259
        css --      Cascading Style Sheet filename as a string or a list of
paul@69 260
                    strings for multiple css files (ignored in xml mode)
paul@69 261
paul@69 262
        metainfo -- a dictionary in the form { 'name':'content' } to be inserted
paul@69 263
                    into meta element(s) as <meta name='name' content='content'>
paul@69 264
                    (ignored in xml mode)
paul@69 265
        
paul@69 266
        base     -- set the <base href="..."> tag in <head>
paul@69 267
        
paul@69 268
        bodyattrs --a dictionary in the form { 'key':'value', ... } which will be added
paul@69 269
                    as attributes of the <body> element as <body key='value' ... >
paul@69 270
                    (ignored in xml mode)
paul@69 271
paul@69 272
        script --   dictionary containing src:type pairs, <script type='text/type' src=src></script>
paul@69 273
                    or a list of [ 'src1', 'src2', ... ] in which case 'javascript' is assumed for all
paul@69 274
paul@69 275
        title --    the title of the document as a string to be inserted into
paul@69 276
                    a title element as <title>my title</title> (ignored in xml mode)
paul@69 277
paul@69 278
        header --   some text to be inserted right after the <body> element
paul@69 279
                    (ignored in xml mode)
paul@69 280
paul@69 281
        footer --   some text to be inserted right before the </body> element
paul@69 282
                    (ignored in xml mode)
paul@69 283
paul@69 284
        charset --  a string defining the character set, will be inserted into a
paul@69 285
                    <meta http-equiv='Content-Type' content='text/html; charset=myset'>
paul@69 286
                    element (ignored in xml mode)
paul@69 287
paul@69 288
        encoding -- a string defining the encoding, will be put into to first line of
paul@69 289
                    the document as <?xml version='1.0' encoding='myencoding' ?> in
paul@69 290
                    xml mode (ignored in html mode)
paul@69 291
paul@69 292
        doctype --  the document type string, defaults to
paul@69 293
                    <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
paul@69 294
                    in html mode (ignored in xml mode)"""
paul@69 295
paul@69 296
        self._full = True
paul@69 297
paul@69 298
        if self.mode == 'strict_html' or self.mode == 'loose_html':
paul@69 299
            if doctype is None:
paul@69 300
                doctype = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>"
paul@69 301
            self.header.append( doctype )
paul@69 302
            self.html( lang=lang )
paul@69 303
            self.head( )
paul@69 304
            if charset is not None:
paul@69 305
                self.meta( http_equiv='Content-Type', content="text/html; charset=%s" % charset )
paul@69 306
            if metainfo is not None:
paul@69 307
                self.metainfo( metainfo )
paul@69 308
            if css is not None:
paul@69 309
                self.css( css )
paul@69 310
            if title is not None:
paul@69 311
                self.title( title )
paul@69 312
            if script is not None:
paul@69 313
                self.scripts( script )
paul@69 314
            if base is not None:
paul@69 315
                self.base( href='%s' % base )
paul@69 316
            self.head.close()
paul@69 317
            if bodyattrs is not None:
paul@69 318
                self.body( **bodyattrs )
paul@69 319
            else:
paul@69 320
                self.body( )
paul@69 321
            if header is not None:
paul@69 322
                self.content.append( header )
paul@69 323
            if footer is not None:
paul@69 324
                self.footer.append( footer )
paul@69 325
paul@69 326
        elif self.mode == 'xml':
paul@69 327
            if doctype is None:
paul@69 328
                if encoding is not None:
paul@69 329
                    doctype = "<?xml version='1.0' encoding='%s' ?>" % encoding
paul@69 330
                else:
paul@69 331
                    doctype = "<?xml version='1.0' ?>"
paul@69 332
            self.header.append( doctype )
paul@69 333
paul@69 334
    def css( self, filelist ):
paul@69 335
        """This convenience function is only useful for html.
paul@69 336
        It adds css stylesheet(s) to the document via the <link> element."""
paul@69 337
      
paul@69 338
        if isinstance( filelist, basestring ):
paul@69 339
            self.link( href=filelist, rel='stylesheet', type='text/css', media='all' )
paul@69 340
        else:
paul@69 341
            for file in filelist:
paul@69 342
                self.link( href=file, rel='stylesheet', type='text/css', media='all' )
paul@69 343
paul@69 344
    def metainfo( self, mydict ):
paul@69 345
        """This convenience function is only useful for html.
paul@69 346
        It adds meta information via the <meta> element, the argument is
paul@69 347
        a dictionary of the form { 'name':'content' }."""
paul@69 348
paul@69 349
        if isinstance( mydict, dict ):
paul@69 350
            for name, content in list( mydict.items( ) ):
paul@69 351
                self.meta( name=name, content=content )
paul@69 352
        else:
paul@69 353
            raise TypeError( "Metainfo should be called with a dictionary argument of name:content pairs." )
paul@69 354
paul@69 355
    def scripts( self, mydict ):
paul@69 356
        """Only useful in html, mydict is dictionary of src:type pairs or a list
paul@69 357
        of script sources [ 'src1', 'src2', ... ] in which case 'javascript' is assumed for type.
paul@69 358
        Will be rendered as <script type='text/type' src=src></script>"""
paul@69 359
paul@69 360
        if isinstance( mydict, dict ):
paul@69 361
            for src, type in list( mydict.items( ) ):
paul@69 362
                self.script( '', src=src, type='text/%s' % type )
paul@69 363
        else:
paul@69 364
            try:
paul@69 365
                for src in mydict:
paul@69 366
                    self.script( '', src=src, type='text/javascript' )
paul@69 367
            except:
paul@69 368
                raise TypeError( "Script should be given a dictionary of src:type pairs or a list of javascript src's." )
paul@69 369
paul@69 370
paul@69 371
class _oneliner:
paul@69 372
    """An instance of oneliner returns a string corresponding to one element.
paul@69 373
    This class can be used to write 'oneliners' that return a string
paul@69 374
    immediately so there is no need to instantiate the page class."""
paul@69 375
    
paul@69 376
    def __init__( self, case='lower' ):
paul@69 377
        self.case = case
paul@69 378
    
paul@69 379
    def __getattr__( self, attr ):
paul@69 380
        
paul@69 381
        # tags should start with double underscore
paul@69 382
        if attr.startswith("__") and attr.endswith("__"):
paul@69 383
            raise AttributeError( attr )
paul@69 384
        # tag with single underscore should be a reserved keyword
paul@69 385
        if attr.startswith( '_' ):
paul@69 386
            attr = attr.lstrip( '_' ) 
paul@69 387
            if attr not in keyword.kwlist:
paul@69 388
                raise AttributeError( attr )
paul@69 389
        
paul@69 390
        return element( attr, case=self.case, parent=None )
paul@69 391
paul@69 392
oneliner = _oneliner( case='lower' )
paul@69 393
upper_oneliner = _oneliner( case='upper' )
paul@69 394
given_oneliner = _oneliner( case='given' )
paul@69 395
paul@69 396
def _argsdicts( args, mydict ):
paul@69 397
    """A utility generator that pads argument list and dictionary values, will only be called with len( args ) = 0, 1."""
paul@69 398
    
paul@69 399
    if len( args ) == 0:
paul@69 400
        args = None, 
paul@69 401
    elif len( args ) == 1:
paul@69 402
        args = _totuple( args[0] )
paul@69 403
    else:
paul@69 404
        raise Exception( "We should have never gotten here." )
paul@69 405
paul@69 406
    mykeys = list( mydict.keys( ) )
paul@69 407
    myvalues = list( map( _totuple, list( mydict.values( ) ) ) )
paul@69 408
paul@69 409
    maxlength = max( list( map( len, [ args ] + myvalues ) ) )
paul@69 410
paul@69 411
    for i in range( maxlength ):
paul@69 412
        thisdict = { }
paul@69 413
        for key, value in zip( mykeys, myvalues ):
paul@69 414
            try:
paul@69 415
                thisdict[ key ] = value[i]
paul@69 416
            except IndexError:
paul@69 417
                thisdict[ key ] = value[-1]
paul@69 418
        try:
paul@69 419
            thisarg = args[i]
paul@69 420
        except IndexError:
paul@69 421
            thisarg = args[-1]
paul@69 422
paul@69 423
        yield thisarg, thisdict
paul@69 424
paul@69 425
def _totuple( x ):
paul@69 426
    """Utility stuff to convert string, int, long, float, None or anything to a usable tuple."""
paul@69 427
paul@69 428
    if isinstance( x, basestring ):
paul@69 429
        out = x,
paul@69 430
    elif isinstance( x, ( int, long, float ) ):
paul@69 431
        out = str( x ),
paul@69 432
    elif x is None:
paul@69 433
        out = None,
paul@69 434
    else:
paul@69 435
        out = tuple( x )
paul@69 436
paul@69 437
    return out
paul@69 438
paul@69 439
def escape( text, newline=False ):
paul@69 440
    """Escape special html characters."""
paul@69 441
paul@69 442
    if isinstance( text, basestring ):
paul@69 443
        if '&' in text:
paul@69 444
            text = text.replace( '&', '&amp;' )
paul@69 445
        if '>' in text:
paul@69 446
            text = text.replace( '>', '&gt;' )
paul@69 447
        if '<' in text:
paul@69 448
            text = text.replace( '<', '&lt;' )
paul@69 449
        if '\"' in text:
paul@69 450
            text = text.replace( '\"', '&quot;' )
paul@69 451
        if '\'' in text:
paul@270 452
            text = text.replace( '\'', '&apos;' )
paul@69 453
        if newline:
paul@69 454
            if '\n' in text:
paul@69 455
                text = text.replace( '\n', '<br>' )
paul@69 456
paul@69 457
    return text
paul@69 458
paul@69 459
_escape = escape
paul@69 460
paul@69 461
def unescape( text ):
paul@69 462
    """Inverse of escape."""
paul@69 463
    
paul@69 464
    if isinstance( text, basestring ):
paul@69 465
        if '&amp;' in text:
paul@69 466
            text = text.replace( '&amp;', '&' )
paul@69 467
        if '&gt;' in text:
paul@69 468
            text = text.replace( '&gt;', '>' )
paul@69 469
        if '&lt;' in text:
paul@69 470
            text = text.replace( '&lt;', '<' )
paul@69 471
        if '&quot;' in text:
paul@69 472
            text = text.replace( '&quot;', '\"' )
paul@69 473
paul@69 474
    return text
paul@69 475
paul@69 476
class dummy:
paul@69 477
    """A dummy class for attaching attributes."""
paul@69 478
    pass
paul@69 479
paul@69 480
doctype = dummy( )
paul@69 481
doctype.frameset = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">"""
paul@69 482
doctype.strict = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">"""
paul@69 483
doctype.loose = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">"""
paul@69 484
paul@69 485
class russell:
paul@69 486
    """A dummy class that contains anything."""
paul@69 487
paul@69 488
    def __contains__( self, item ):
paul@69 489
        return True
paul@69 490
paul@69 491
paul@69 492
class MarkupError( Exception ):
paul@69 493
    """All our exceptions subclass this."""
paul@69 494
    def __str__( self ):
paul@69 495
        return self.message
paul@69 496
paul@69 497
class ClosingError( MarkupError ):
paul@69 498
    def __init__( self, tag ):
paul@69 499
        self.message = "The element '%s' does not accept non-keyword arguments (has no closing tag)." % tag
paul@69 500
paul@69 501
class OpeningError( MarkupError ):
paul@69 502
    def __init__( self, tag ):
paul@69 503
        self.message = "The element '%s' can not be opened." % tag
paul@69 504
paul@69 505
class ArgumentError( MarkupError ):
paul@69 506
    def __init__( self, tag ):
paul@69 507
        self.message = "The element '%s' was called with more than one non-keyword argument." % tag
paul@69 508
paul@69 509
class InvalidElementError( MarkupError ):
paul@69 510
    def __init__( self, tag, mode ):
paul@69 511
        self.message = "The element '%s' is not valid for your mode '%s'." % ( tag, mode )
paul@69 512
paul@69 513
class DeprecationError( MarkupError ):
paul@69 514
    def __init__( self, tag ):
paul@69 515
        self.message = "The element '%s' is deprecated, instantiate markup.page with mode='loose_html' to allow it." % tag
paul@69 516
paul@69 517
class ModeError( MarkupError ):
paul@69 518
    def __init__( self, mode ):
paul@69 519
        self.message = "Mode '%s' is invalid, possible values: strict_html, html (alias for strict_html), loose_html, xml." % mode
paul@69 520
paul@69 521
class CustomizationError( MarkupError ):
paul@69 522
    def __init__( self ):
paul@69 523
        self.message = "If you customize the allowed elements, you must define both types 'onetags' and 'twotags'."
paul@69 524
paul@69 525
if __name__ == '__main__':
paul@69 526
    import sys
paul@69 527
    sys.stdout.write( __doc__ )