2009-03-05 02:55:18 +05:30
|
|
|
#
|
2009-03-03 23:23:35 +05:30
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2009 Gerald Britton <gerald.britton@gmail.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
# $Id: html.py 10874 2009-03-03 18:00:00Z gbritton $
|
|
|
|
|
|
|
|
"""
|
|
|
|
HTML operations.
|
|
|
|
|
2009-03-05 02:55:18 +05:30
|
|
|
This module exports one class and two functions.
|
2009-03-03 23:23:35 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# constants
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
_XHTML1_STRICT = 'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n' \
|
|
|
|
'\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'
|
|
|
|
|
2009-03-05 02:55:18 +05:30
|
|
|
class Html(list):
|
2009-03-03 23:23:35 +05:30
|
|
|
"""
|
|
|
|
HTML class.
|
|
|
|
"""
|
|
|
|
__slots__ = ['items','indent','inline','end']
|
|
|
|
#
|
|
|
|
def __init__(self, tag='html', *args, **keywargs):
|
2009-03-05 02:55:18 +05:30
|
|
|
super(Html, self).__init__([])
|
2009-03-03 23:23:35 +05:30
|
|
|
attr, indent, close, inline = '', False, True, False
|
|
|
|
for keyw, arg in keywargs.iteritems():
|
2009-03-05 02:55:18 +05:30
|
|
|
if keyw in ['indent', 'close', 'inline'] and arg in [True, False, None]:
|
2009-03-03 23:23:35 +05:30
|
|
|
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
|
|
|
|
self.inline = inline
|
2009-03-05 02:55:18 +05:30
|
|
|
self.end = close
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
2009-03-05 02:55:18 +05:30
|
|
|
if tag[0] == '<':
|
|
|
|
self += [tag]
|
|
|
|
self.end = None
|
2009-03-09 20:48:51 +05:30
|
|
|
else:
|
2009-03-09 22:57:06 +05:30
|
|
|
if tag in ['area', 'base', 'br', 'frame', 'hr',
|
|
|
|
'img', 'input', 'link', 'meta', 'param']:
|
|
|
|
self.end = close = False
|
2009-03-05 02:55:18 +05:30
|
|
|
begin = '<%s%s%s>' % (
|
|
|
|
tag,
|
|
|
|
attr,
|
|
|
|
('' if close or close is None else ' /')
|
|
|
|
)
|
|
|
|
self += [begin] + list(args)
|
|
|
|
super(Html, self).extend(['</%s>' % tag] if close else [])
|
|
|
|
#
|
|
|
|
def __add__(self, value):
|
|
|
|
if isinstance(value, Html) or not hasattr(value, '__iter__'):
|
|
|
|
value = [value]
|
2009-03-09 22:57:06 +05:30
|
|
|
index = len(self) - (1 if self.end else 0)
|
2009-03-05 02:55:18 +05:30
|
|
|
self[index:index] = value
|
2009-03-03 23:23:35 +05:30
|
|
|
return self
|
|
|
|
#
|
2009-03-05 02:55:18 +05:30
|
|
|
__iadd__ = append = extend = __add__
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
2009-03-05 02:55:18 +05:30
|
|
|
def replace(self, cur_value, value):
|
|
|
|
self[self.index(cur_value)] = value
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
2009-03-05 02:55:18 +05:30
|
|
|
def __sub__(self, value):
|
|
|
|
del self[self.index(value)]
|
2009-03-03 23:23:35 +05:30
|
|
|
return self
|
2009-03-05 02:55:18 +05:30
|
|
|
#
|
|
|
|
__isub__ = remove = __sub__
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
|
|
|
def _print(data):
|
|
|
|
print data
|
2009-03-05 02:55:18 +05:30
|
|
|
#
|
|
|
|
def __str__(self):
|
|
|
|
return '%s'*len(self) % tuple(self[:])
|
|
|
|
#
|
|
|
|
def __iter__(self):
|
|
|
|
for item in self[:]:
|
|
|
|
if isinstance(item, self.Html):
|
|
|
|
for sub_item in item:
|
|
|
|
yield sub_item
|
|
|
|
else:
|
|
|
|
yield item
|
|
|
|
#
|
|
|
|
iterkeys = itervalues = iteritems = __iter__
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
|
|
|
def write(self, method=_print, tabs=''):
|
|
|
|
tabs += '\t' if self.indent else ''
|
|
|
|
if self.inline:
|
|
|
|
method('%s%s' % (tabs,self))
|
|
|
|
#
|
|
|
|
else:
|
2009-03-05 02:55:18 +05:30
|
|
|
for item in self[:]:
|
|
|
|
if isinstance(item, Html):
|
2009-03-09 19:08:50 +05:30
|
|
|
item.write(method=method, tabs=tabs)
|
2009-03-03 23:23:35 +05:30
|
|
|
else:
|
|
|
|
method('%s%s' % (tabs, item))
|
|
|
|
#
|
2009-03-05 02:55:18 +05:30
|
|
|
def addXML(self, version=1.0, encoding="UTF-8", standalone="no"):
|
|
|
|
xml = XML(version=version, encoding=encoding, standalone=standalone)
|
|
|
|
self.insert(0, xml)
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
2009-03-05 02:55:18 +05:30
|
|
|
def addDOCTYPE(self, name='html', external_id=_XHTML1_STRICT, *args):
|
|
|
|
doctype = DOCTYPE(name='html', external_id=_XHTML1_STRICT, *args)
|
|
|
|
if len(self) and self[0][:6] == '<?xml ':
|
|
|
|
self.insert(1, doctype)
|
2009-03-03 23:23:35 +05:30
|
|
|
else:
|
2009-03-05 02:55:18 +05:30
|
|
|
self.insert(0, doctype)
|
|
|
|
|
|
|
|
def XML(version=1.0, encoding="UTF-8", standalone="no"):
|
|
|
|
return '<?xml %s %s %s?>' % (
|
|
|
|
'version="%s"' % version,
|
|
|
|
'encoding="%s"' % encoding,
|
|
|
|
'standalone="%s"' % standalone
|
|
|
|
)
|
2009-03-03 23:23:35 +05:30
|
|
|
|
2009-03-05 02:55:18 +05:30
|
|
|
def DOCTYPE(name='html', external_id=_XHTML1_STRICT, *args):
|
|
|
|
return '<!DOCTYPE %s %s%s>' % (
|
|
|
|
name,
|
|
|
|
external_id,
|
|
|
|
' %s'*len(args) % args
|
|
|
|
)
|
2009-03-03 23:23:35 +05:30
|
|
|
|