html.py: Add XML constants; change keyword handling to conform to PEP 8
svn: r12352
This commit is contained in:
parent
0214b842cd
commit
7bf8b0056a
@ -29,12 +29,55 @@ This module exports one class and two functions.
|
|||||||
|
|
||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# constants
|
# Constants
|
||||||
#
|
#
|
||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
_XHTML1_STRICT = 'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n' \
|
|
||||||
'\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'
|
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# XHTML DOCTYPE constants to be used in <!DOCTYPE ... > statements
|
||||||
|
#
|
||||||
|
#------------------------------------------------------------------------
|
||||||
|
_XHTML10_STRICT = 'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n' \
|
||||||
|
'\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'
|
||||||
|
_XTHML10_TRANS = 'PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n' \
|
||||||
|
'\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"'
|
||||||
|
_XHTML10_FRAME = 'PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"\n' \
|
||||||
|
'\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"'
|
||||||
|
_XHTML11 = 'PUBLIC "-//W3C//DTD XHTML 1.1//EN"\n' \
|
||||||
|
'\t"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"'
|
||||||
|
_XHTML10_BASIC = 'PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"\n' \
|
||||||
|
'\t"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd"'
|
||||||
|
_XHTML11_BASIC = 'PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN"\n ' \
|
||||||
|
'\t"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd"'
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# XML Namespace constant for use in <html xmlns=...> tags
|
||||||
|
#
|
||||||
|
#------------------------------------------------------------------------
|
||||||
|
|
||||||
|
_XMLNS = "http://www.w3.org/1999/xhtml"
|
||||||
|
|
||||||
|
#------------------------------------------------------------------------
|
||||||
|
#
|
||||||
|
# local constants.
|
||||||
|
#
|
||||||
|
#------------------------------------------------------------------------
|
||||||
|
|
||||||
|
_START_CLOSE = (
|
||||||
|
'area',
|
||||||
|
'base',
|
||||||
|
'br',
|
||||||
|
'frame',
|
||||||
|
'hr',
|
||||||
|
'img',
|
||||||
|
'input',
|
||||||
|
'link',
|
||||||
|
'meta',
|
||||||
|
'param'
|
||||||
|
)
|
||||||
|
#
|
||||||
class Html(list):
|
class Html(list):
|
||||||
"""
|
"""
|
||||||
HTML class.
|
HTML class.
|
||||||
@ -44,34 +87,42 @@ class Html(list):
|
|||||||
def __init__(self, tag='html', *args, **keywargs):
|
def __init__(self, tag='html', *args, **keywargs):
|
||||||
super(Html, self).__init__([])
|
super(Html, self).__init__([])
|
||||||
attr, indent, close, inline = '', False, True, False
|
attr, indent, close, inline = '', False, True, False
|
||||||
for keyw, arg in keywargs.iteritems():
|
|
||||||
if keyw in ['indent', 'close', 'inline'] and arg in [True, False, None]:
|
|
||||||
exec '%s = %s' % (keyw, arg)
|
|
||||||
elif keyw == 'attr':
|
|
||||||
attr += ' ' + arg
|
|
||||||
elif keyw[0] == '_':
|
|
||||||
attr += ' %s="%s"' % (keyw[1:], arg)
|
|
||||||
else:
|
|
||||||
attr += ' %s="%s"' % (keyw, arg)
|
|
||||||
#
|
#
|
||||||
self.indent = indent
|
# Handle keyword arguments passed to this constructor.
|
||||||
|
# Keywords that we process directly are handled.
|
||||||
|
# Keywords we don't recognize are passed into the
|
||||||
|
# opening tag.
|
||||||
|
#
|
||||||
|
for keyw, arg in keywargs.iteritems():
|
||||||
|
if keyw in ['indent', 'close', 'inline'] and \
|
||||||
|
arg in [True, False, None]:
|
||||||
|
exec '%s = %s' % (keyw, arg) # keep these settings
|
||||||
|
elif keyw == 'attr': # pass attributes along
|
||||||
|
attr += ' ' + arg
|
||||||
|
elif keyw[-1] == '_': # avoid Python conflicts
|
||||||
|
attr += ' %s="%s"' % (keyw[:-1], arg) # pass keyword arg along
|
||||||
|
else:
|
||||||
|
attr += ' %s="%s"' % (keyw, arg) # pass keyword arg along
|
||||||
|
#
|
||||||
|
self.indent = indent
|
||||||
self.inline = inline
|
self.inline = inline
|
||||||
self.end = close
|
self.end = close
|
||||||
#
|
#
|
||||||
if tag[0] == '<':
|
if tag[0] == '<': # did caller provide preformatted tag?
|
||||||
self += [tag]
|
self += [tag]
|
||||||
self.end = None
|
self.end = None
|
||||||
else:
|
else:
|
||||||
if tag in ['area', 'base', 'br', 'frame', 'hr',
|
if tag in _START_CLOSE: # is tag in special list that need no closer?
|
||||||
'img', 'input', 'link', 'meta', 'param']:
|
|
||||||
self.end = close = False
|
self.end = close = False
|
||||||
begin = '<%s%s%s>' % (
|
begin = '<%s%s%s>' % ( # build opening tag, passing in attributes
|
||||||
tag,
|
tag,
|
||||||
attr,
|
attr,
|
||||||
('' if close or close is None else ' /')
|
('' if close or close is None else ' /')
|
||||||
)
|
)
|
||||||
self += [begin] + list(args)
|
self += [begin] + list(args) # add beginning tag to list
|
||||||
super(Html, self).extend(['</%s>' % tag] if close else [])
|
super(Html, self).extend( # add closing tag if necessary
|
||||||
|
['</%s>' % tag] if close else []
|
||||||
|
)
|
||||||
#
|
#
|
||||||
def __add__(self, value):
|
def __add__(self, value):
|
||||||
if isinstance(value, Html) or not hasattr(value, '__iter__'):
|
if isinstance(value, Html) or not hasattr(value, '__iter__'):
|
||||||
@ -91,53 +142,53 @@ class Html(list):
|
|||||||
#
|
#
|
||||||
__isub__ = remove = __sub__
|
__isub__ = remove = __sub__
|
||||||
#
|
#
|
||||||
def _print(data):
|
def print_(data):
|
||||||
print data
|
print data
|
||||||
#
|
#
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '%s'*len(self) % tuple(self[:])
|
return '%s'*len(self) % tuple(self[:])
|
||||||
#
|
#
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for item in self[:]:
|
for item in self[:]: # loop through all list elements
|
||||||
if isinstance(item, self.Html):
|
if isinstance(item, self.Html): # if nested list found
|
||||||
for sub_item in item:
|
for sub_item in item: # recurse
|
||||||
yield sub_item
|
yield sub_item
|
||||||
else:
|
else:
|
||||||
yield item
|
yield item
|
||||||
#
|
#
|
||||||
iterkeys = itervalues = iteritems = __iter__
|
iterkeys = itervalues = iteritems = __iter__
|
||||||
#
|
#
|
||||||
def write(self, method=_print, tabs=''):
|
def write(self, method=print_, tabs=''):
|
||||||
tabs += '\t' if self.indent else ''
|
tabs += '\t' if self.indent else ''
|
||||||
if self.inline:
|
if self.inline: # if inline, write all list and
|
||||||
method('%s%s' % (tabs,self))
|
method('%s%s' % (tabs,self)) # nested list elements
|
||||||
#
|
#
|
||||||
else:
|
else:
|
||||||
for item in self[:]:
|
for item in self[:]: # not inline: write one at a time
|
||||||
if isinstance(item, Html):
|
if isinstance(item, Html):
|
||||||
item.write(method=method, tabs=tabs)
|
item.write(method=method, tabs=tabs) # recurse if nested
|
||||||
else:
|
else:
|
||||||
method('%s%s' % (tabs, item))
|
method('%s%s' % (tabs, item)) # write the line if not
|
||||||
#
|
#
|
||||||
def addXML(self, version=1.0, encoding="UTF-8", standalone="no"):
|
def addXML(self, version=1.0, encoding="UTF-8", standalone="no"):
|
||||||
xml = XML(version=version, encoding=encoding, standalone=standalone)
|
xml = XML(version=version, encoding=encoding, standalone=standalone)
|
||||||
self.insert(0, xml)
|
self.insert(0, xml)
|
||||||
#
|
#
|
||||||
def addDOCTYPE(self, name='html', external_id=_XHTML1_STRICT, *args):
|
def addDOCTYPE(self, name='html', external_id=_XHTML10_STRICT, *args):
|
||||||
doctype = DOCTYPE(name='html', external_id=_XHTML1_STRICT, *args)
|
doctype = DOCTYPE(name='html', external_id=external_id, *args)
|
||||||
if len(self) and self[0][:6] == '<?xml ':
|
if len(self) and self[0][:6] == '<?xml ':
|
||||||
self.insert(1, doctype)
|
self.insert(1, doctype)
|
||||||
else:
|
else:
|
||||||
self.insert(0, doctype)
|
self.insert(0, doctype)
|
||||||
|
#
|
||||||
def XML(version=1.0, encoding="UTF-8", standalone="no"):
|
def XML(version=1.0, encoding="UTF-8", standalone="no"):
|
||||||
return '<?xml %s %s %s?>' % (
|
return '<?xml %s %s %s?>' % (
|
||||||
'version="%s"' % version,
|
'version="%s"' % version,
|
||||||
'encoding="%s"' % encoding,
|
'encoding="%s"' % encoding,
|
||||||
'standalone="%s"' % standalone
|
'standalone="%s"' % standalone
|
||||||
)
|
)
|
||||||
|
#
|
||||||
def DOCTYPE(name='html', external_id=_XHTML1_STRICT, *args):
|
def DOCTYPE(name='html', external_id=_XHTML10_STRICT, *args):
|
||||||
return '<!DOCTYPE %s %s%s>' % (
|
return '<!DOCTYPE %s %s%s>' % (
|
||||||
name,
|
name,
|
||||||
external_id,
|
external_id,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user