2009-08-08 21:22:12 +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
|
|
|
|
#
|
|
|
|
|
2009-12-21 19:13:50 +05:30
|
|
|
# $Id$
|
2009-03-03 23:23:35 +05:30
|
|
|
|
2009-03-20 21:52:12 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Html
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
2010-04-23 01:11:48 +05:30
|
|
|
from __future__ import print_function
|
|
|
|
|
2009-03-03 23:23:35 +05:30
|
|
|
"""
|
|
|
|
HTML operations.
|
|
|
|
|
2009-04-03 01:23:30 +05:30
|
|
|
This module exports the Html class
|
2009-03-03 23:23:35 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
|
2009-04-03 01:23:30 +05:30
|
|
|
__all__ = ['Html']
|
2009-03-20 21:52:12 +05:30
|
|
|
|
2009-03-23 20:12:37 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
2009-03-03 23:23:35 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
2009-03-16 21:47:44 +05:30
|
|
|
# Constants
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
2009-03-16 21:47:44 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# XHTML DOCTYPE constants to be used in <!DOCTYPE ... > statements
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2009-04-03 01:23:30 +05:30
|
|
|
|
|
|
|
_XHTML10_STRICT = '"-//W3C//DTD XHTML 1.0 Strict//EN"\n' \
|
2009-03-16 21:47:44 +05:30
|
|
|
'\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"'
|
2009-04-03 01:23:30 +05:30
|
|
|
_XTHML10_TRANS = '"-//W3C//DTD XHTML 1.0 Transitional//EN"\n' \
|
2009-03-16 21:47:44 +05:30
|
|
|
'\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"'
|
2009-04-03 01:23:30 +05:30
|
|
|
_XHTML10_FRAME = '"-//W3C//DTD XHTML 1.0 Frameset//EN"\n' \
|
2009-03-16 21:47:44 +05:30
|
|
|
'\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"'
|
2009-04-03 01:23:30 +05:30
|
|
|
_XHTML11 = '"-//W3C//DTD XHTML 1.1//EN"\n' \
|
2009-03-16 21:47:44 +05:30
|
|
|
'\t"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"'
|
2009-04-03 01:23:30 +05:30
|
|
|
_XHTML10_BASIC = '"-//W3C//DTD XHTML Basic 1.0//EN"\n' \
|
2009-03-16 21:47:44 +05:30
|
|
|
'\t"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd"'
|
2009-04-03 01:23:30 +05:30
|
|
|
_XHTML11_BASIC = '"-//W3C//DTD XHTML Basic 1.1//EN"\n ' \
|
2009-03-16 21:47:44 +05:30
|
|
|
'\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"
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
2009-04-03 01:23:30 +05:30
|
|
|
# Local constants.
|
2009-03-16 21:47:44 +05:30
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
2009-09-22 02:22:36 +05:30
|
|
|
# Set of html tags that do not use a complementary closing tag, but close with
|
|
|
|
# /> instead
|
|
|
|
_START_CLOSE = set([
|
2009-03-16 21:47:44 +05:30
|
|
|
'area',
|
|
|
|
'base',
|
|
|
|
'br',
|
|
|
|
'frame',
|
|
|
|
'hr',
|
|
|
|
'img',
|
|
|
|
'input',
|
|
|
|
'link',
|
|
|
|
'meta',
|
|
|
|
'param'
|
2009-09-22 02:22:36 +05:30
|
|
|
])
|
2009-03-20 21:52:12 +05:30
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Html class.
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
|
2009-03-05 02:55:18 +05:30
|
|
|
class Html(list):
|
2009-03-03 23:23:35 +05:30
|
|
|
"""
|
2009-03-20 21:52:12 +05:30
|
|
|
HTML class: Manages a rooted tree of HTML objects
|
2009-03-03 23:23:35 +05:30
|
|
|
"""
|
2009-09-22 02:22:36 +05:30
|
|
|
__slots__ = ['items', 'indent', 'inline', 'close']
|
2009-10-04 10:26:26 +05:30
|
|
|
#
|
|
|
|
@staticmethod
|
|
|
|
def xmldecl(version=1.0, encoding="UTF-8", standalone="no"):
|
|
|
|
"""
|
|
|
|
Build and return an XML declaration statement
|
|
|
|
|
|
|
|
:type version: decimal number
|
|
|
|
:param version: version of XML to be used. Defaults to 1.0
|
|
|
|
:type encoding: string
|
|
|
|
:param encoding: encoding method to be used. Defaults to "UTF-8"
|
|
|
|
:type standalone: string
|
|
|
|
:param standalone: "yes" or "no". Defaults to "no"
|
|
|
|
"""
|
|
|
|
return '<?xml %s %s %s?>' % (
|
|
|
|
'version="%s"' % version,
|
|
|
|
'encoding="%s"' % encoding,
|
|
|
|
'standalone="%s"' % standalone
|
|
|
|
)
|
2009-04-03 01:23:30 +05:30
|
|
|
#
|
|
|
|
@staticmethod
|
|
|
|
def doctype(name='html', public='PUBLIC', external_id=_XHTML10_STRICT):
|
|
|
|
"""
|
|
|
|
Build and return a DOCTYPE statement
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type name: string
|
|
|
|
:param name: name of this DOCTYPE. Defaults to "html"
|
|
|
|
:type public: string
|
|
|
|
:param public: class of this DOCTYPE. Defaults to 'PUBLIC
|
|
|
|
:type external_id: string
|
|
|
|
:param external_id: external identifier of this DOCTYPE.
|
2009-04-03 01:23:30 +05:30
|
|
|
Defaults to XHTML 1.0 STRICT
|
2009-06-25 03:26:07 +05:30
|
|
|
:type args: object
|
|
|
|
:param args: 0 or more positional parameters to be added to this
|
2009-04-03 01:23:30 +05:30
|
|
|
DOCTYPE.
|
|
|
|
"""
|
|
|
|
return '<!DOCTYPE %s %s %s>' % (
|
|
|
|
name,
|
|
|
|
public,
|
|
|
|
external_id,
|
|
|
|
)
|
|
|
|
#
|
|
|
|
@staticmethod
|
|
|
|
def html(xmlns=_XMLNS, lang='en', *args, **keywargs):
|
|
|
|
"""
|
|
|
|
Build and return a properly-formated <html> object
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type xmlns: string
|
|
|
|
:param xmlns: XML namespace string. Default = 'http://www.w3.org/1999/xhtml'
|
|
|
|
:type lang: string
|
|
|
|
:param lang: language to be used. Defaul = 'en'
|
|
|
|
:rtype: reference to new Html instance
|
|
|
|
:returns: reference to the newly-created Html instances for <html> object
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
return Html('html',
|
|
|
|
indent=False,
|
|
|
|
xmlns=xmlns,
|
|
|
|
attr='xml:lang="%s" lang="%s"' % ((lang,)*2),
|
|
|
|
*args, **keywargs
|
|
|
|
)
|
|
|
|
#
|
|
|
|
@staticmethod
|
2009-06-12 22:06:31 +05:30
|
|
|
def head(title=None, encoding='utf-8', *args, **keywargs):
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
Build and return a properly-formated <head> object
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type title: string or None
|
|
|
|
:param title: title for HTML page. Default=None. If None no
|
2009-06-11 18:31:27 +05:30
|
|
|
title tag is written
|
2009-06-25 03:26:07 +05:30
|
|
|
:type encoding: string
|
|
|
|
:param encoding: encoding to be used. Default = 'utf-8'
|
|
|
|
:rtype: reference to new Html instance
|
|
|
|
:returns: reference to the newly-created Html instances for <head> object
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
meta1 = 'http-equiv="content-type" content="text/html;charset=%s"'
|
|
|
|
meta2 = 'http-equiv="Content-Style-Type" content="text/css"'
|
2009-06-11 18:31:27 +05:30
|
|
|
head = Html('head', *args, **keywargs)
|
2009-06-12 22:06:31 +05:30
|
|
|
if title is not None:
|
2009-06-11 18:31:27 +05:30
|
|
|
head += (Html('title', title, inline=True, indent=True))
|
2009-06-12 22:06:31 +05:30
|
|
|
head += Html('meta', attr=meta1 % encoding, indent=True)
|
|
|
|
head += Html('meta', attr=meta2, indent=True)
|
2009-04-03 01:23:30 +05:30
|
|
|
return head
|
|
|
|
#
|
|
|
|
@staticmethod
|
2009-06-12 22:06:31 +05:30
|
|
|
def page(title=None, encoding='utf-8', lang='en', *args, **keywargs):
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
This function prepares a new Html class based page and returns
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type title: string
|
|
|
|
:param title: title for HTML page. Default=None
|
|
|
|
:type encoding: string
|
|
|
|
:param encoding: encoding to be used. Default = 'utf-8'
|
|
|
|
:type lang: string
|
|
|
|
:param lang: language to be used. Defaul = 'en'
|
|
|
|
:rtype: three object references
|
|
|
|
:returns: references to the newly-created Html instances for
|
2009-04-03 01:23:30 +05:30
|
|
|
page, head and body
|
|
|
|
"""
|
|
|
|
page = Html.html(lang=lang, *args, **keywargs)
|
2009-10-04 10:26:26 +05:30
|
|
|
page.addXML(encoding=encoding)
|
2009-04-03 01:23:30 +05:30
|
|
|
page.addDOCTYPE()
|
|
|
|
#
|
|
|
|
head = Html.head(title=title,
|
|
|
|
encoding=encoding,
|
|
|
|
lang=lang,
|
|
|
|
indent=False,
|
|
|
|
*args, **keywargs
|
|
|
|
)
|
|
|
|
#
|
|
|
|
body = Html('body', indent=False, *args, **keywargs)
|
|
|
|
page += (head, body)
|
|
|
|
return page, head, body
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
|
|
|
def __init__(self, tag='html', *args, **keywargs):
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
|
|
|
Class Constructor: Returns a new instance of the Html class
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type tag: string
|
|
|
|
:param tag: The HTML tag. Default is 'html'
|
|
|
|
:type args: optional positional parameters
|
|
|
|
:param args: 0 more positional arguments to be inserted between
|
2009-03-20 21:52:12 +05:30
|
|
|
opening and closing HTML tags.
|
2009-06-25 03:26:07 +05:30
|
|
|
:type indent: boolean or None
|
|
|
|
:param indent: True ==> indent this object with respect to its parent
|
2009-03-20 21:52:12 +05:30
|
|
|
False ==> do not indent this object
|
2009-06-11 18:31:27 +05:30
|
|
|
None ==> no indent for this object (use eg for pre tag)
|
2009-06-12 22:06:31 +05:30
|
|
|
Defaults to True
|
2009-06-25 03:26:07 +05:30
|
|
|
:type inline: boolean
|
|
|
|
:param inline: True ==> instructs the write() method to output this
|
2009-03-20 21:52:12 +05:30
|
|
|
object and any child objects as a single string
|
|
|
|
False ==> output this object and its contents one string
|
|
|
|
at a time
|
|
|
|
Defaults to False
|
2009-06-25 03:26:07 +05:30
|
|
|
:type close: boolean or None
|
|
|
|
:param close: True ==> this tag should be closed normally
|
2009-03-20 21:52:12 +05:30
|
|
|
e.g. <tag>...</tag>
|
|
|
|
False ==> this tag should be automatically closed
|
|
|
|
e.g. <tag />
|
|
|
|
None ==> do not provide any closing for this tag
|
2009-06-25 03:26:07 +05:30
|
|
|
:type keywargs: optional keyword parameters
|
|
|
|
:param keywargs: 0 or more keyword=argument pairs that should be
|
2009-03-20 21:52:12 +05:30
|
|
|
copied into the opening tag as keyword="argument"
|
|
|
|
attributes
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: object reference
|
|
|
|
:returns: reference to the newly-created Html instance
|
2009-06-10 19:44:11 +05:30
|
|
|
|
|
|
|
For full usage of the Html class with examples, please see the wiki
|
|
|
|
page at: http://www.gramps-project.org/wiki/index.php?title=Libhtml
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
2009-09-26 06:38:14 +05:30
|
|
|
# Replace super(Html, self) with list
|
|
|
|
# Issue with Python 2.6 and reload of plugin
|
|
|
|
list.__init__(self, []) # instantiate object
|
2009-09-22 02:22:36 +05:30
|
|
|
attr = ''
|
|
|
|
self.indent, self.close, self.inline = True, True, False
|
2009-03-16 21:47:44 +05:30
|
|
|
#
|
|
|
|
# Handle keyword arguments passed to this constructor.
|
|
|
|
# Keywords that we process directly are handled.
|
2009-04-03 01:23:30 +05:30
|
|
|
# Keywords we don't recognize are saved for later
|
|
|
|
# addition to the opening tag as attributes.
|
2009-03-16 21:47:44 +05:30
|
|
|
#
|
2009-03-03 23:23:35 +05:30
|
|
|
for keyw, arg in keywargs.iteritems():
|
2009-06-12 22:06:31 +05:30
|
|
|
if (keyw in ['indent', 'close', 'inline'] and
|
|
|
|
arg in [True, False, None]):
|
2009-09-22 02:22:36 +05:30
|
|
|
setattr(self,keyw, arg)
|
2009-03-20 21:52:12 +05:30
|
|
|
elif keyw == 'attr': # pass attributes along
|
2009-03-03 23:23:35 +05:30
|
|
|
attr += ' ' + arg
|
2009-03-20 21:52:12 +05:30
|
|
|
elif keyw[-1] == '_': # avoid Python conflicts
|
|
|
|
attr += ' %s="%s"' % (keyw[:-1], arg) # pass keyword arg along
|
2009-03-03 23:23:35 +05:30
|
|
|
else:
|
2009-03-20 21:52:12 +05:30
|
|
|
attr += ' %s="%s"' % (keyw, arg) # pass keyword arg along
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
2009-04-03 01:23:30 +05:30
|
|
|
if tag[0] == '<': # if caller provided preformatted tag?
|
|
|
|
self[0:] = [tag] # add it in
|
2009-09-22 02:22:36 +05:30
|
|
|
self.close = None # caller must close the tag
|
2009-03-09 20:48:51 +05:30
|
|
|
else:
|
2009-09-22 02:22:36 +05:30
|
|
|
if tag in _START_CLOSE: # if tag in special list
|
|
|
|
self.close = False # it needs no closing tag
|
2009-04-03 01:23:30 +05:30
|
|
|
begin = '<%s%s%s>' % ( # build opening tag with attributes
|
2009-03-05 02:55:18 +05:30
|
|
|
tag,
|
|
|
|
attr,
|
2009-09-22 02:22:36 +05:30
|
|
|
('' if self.close is not False else ' /')
|
2009-03-05 02:55:18 +05:30
|
|
|
)
|
|
|
|
#
|
2009-09-22 02:22:36 +05:30
|
|
|
# Use slice syntax since we don't override slicing
|
2009-04-03 01:23:30 +05:30
|
|
|
self[0:] = [begin] + list(args) # add beginning tag
|
2009-09-22 02:22:36 +05:30
|
|
|
if self.close: # if need closing tab
|
2009-04-03 01:23:30 +05:30
|
|
|
self[len(self):] = ['</%s>' % tag] # add it on the end
|
|
|
|
#
|
|
|
|
def __add(self, value):
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
2009-04-03 01:23:30 +05:30
|
|
|
Helper function for +, +=, operators and append() and extend()
|
|
|
|
methods
|
2009-03-20 21:52:12 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type value: object
|
|
|
|
:param value: object to be added
|
2009-03-20 21:52:12 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: object reference
|
|
|
|
:returns: reference to object with new value added
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
2009-03-05 02:55:18 +05:30
|
|
|
if isinstance(value, Html) or not hasattr(value, '__iter__'):
|
|
|
|
value = [value]
|
2009-09-22 02:22:36 +05:30
|
|
|
index = len(self) - (1 if self.close 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-09-22 02:22:36 +05:30
|
|
|
__iadd__ = __add__ = __add
|
2009-04-03 01:23:30 +05:30
|
|
|
#
|
|
|
|
def append(self, value):
|
|
|
|
"""
|
|
|
|
Append a new value
|
|
|
|
"""
|
|
|
|
self.__add(value)
|
2009-09-22 02:22:36 +05:30
|
|
|
#
|
2009-04-03 01:23:30 +05:30
|
|
|
extend = append
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
2009-03-05 02:55:18 +05:30
|
|
|
def replace(self, cur_value, value):
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
|
|
|
Replace current value with new value
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type cur_value: object
|
|
|
|
:param cur_value: value of object to be replaced
|
|
|
|
:type value: object
|
|
|
|
:param value: replacement value
|
2009-03-20 21:52:12 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: object reference
|
|
|
|
:returns: reference to object with new value added
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
2009-03-05 02:55:18 +05:30
|
|
|
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):
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
|
|
|
Overload function for - and -= operators
|
2009-06-25 03:26:07 +05:30
|
|
|
:type value: object
|
|
|
|
:param value: object to be removed
|
2009-03-20 21:52:12 +05:30
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: object reference
|
|
|
|
:returns: reference to object with value removed
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
2009-03-05 02:55:18 +05:30
|
|
|
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__
|
|
|
|
#
|
|
|
|
def __str__(self):
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
|
|
|
Returns string representation
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: string
|
|
|
|
:returns: string representation of object
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
2009-03-05 02:55:18 +05:30
|
|
|
return '%s'*len(self) % tuple(self[:])
|
|
|
|
#
|
|
|
|
def __iter__(self):
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
|
|
|
Iterator function: returns a generator that performs an
|
|
|
|
insertion-order tree traversal and yields each item found.
|
|
|
|
"""
|
2009-03-16 21:47:44 +05:30
|
|
|
for item in self[:]: # loop through all list elements
|
2009-03-20 21:52:12 +05:30
|
|
|
if isinstance(item, Html): # if nested list found
|
2009-03-16 21:47:44 +05:30
|
|
|
for sub_item in item: # recurse
|
2009-03-05 02:55:18 +05:30
|
|
|
yield sub_item
|
|
|
|
else:
|
|
|
|
yield item
|
|
|
|
#
|
|
|
|
iterkeys = itervalues = iteritems = __iter__
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
2010-04-23 01:11:48 +05:30
|
|
|
def write(self, method=print, indent='\t', tabs=''):
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
|
|
|
Output function: performs an insertion-order tree traversal
|
|
|
|
and calls supplied method for each item found.
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type method: function reference
|
|
|
|
:param method: function to call with each item found
|
|
|
|
:type indent: string
|
|
|
|
:param indenf: string to use for indentation. Default = '\t' (tab)
|
|
|
|
:type tabs: string
|
|
|
|
:param tabs: starting indentation
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
2009-06-10 19:44:11 +05:30
|
|
|
if self.indent is None:
|
|
|
|
tabs = ''
|
|
|
|
elif self.indent:
|
2009-04-03 01:23:30 +05:30
|
|
|
tabs += indent
|
2009-03-16 21:47:44 +05:30
|
|
|
if self.inline: # if inline, write all list and
|
2009-03-20 21:52:12 +05:30
|
|
|
method('%s%s' % (tabs, self)) # nested list elements
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
|
|
|
else:
|
2009-03-20 21:52:12 +05:30
|
|
|
for item in self[:]: # else write one at a time
|
2009-04-03 01:23:30 +05:30
|
|
|
if isinstance(item, Html): # recurse if nested Html class
|
|
|
|
item.write(method=method, indent=indent, tabs=tabs)
|
2009-03-03 23:23:35 +05:30
|
|
|
else:
|
2009-03-20 21:52:12 +05:30
|
|
|
method('%s%s' % (tabs, item)) # else write the line
|
2009-10-04 10:26:26 +05:30
|
|
|
#
|
|
|
|
def addXML(self, version=1.0, encoding="UTF-8", standalone="no"):
|
|
|
|
"""
|
|
|
|
Add an XML statement to the start of the list for this object
|
|
|
|
|
|
|
|
:type version: decimal number
|
|
|
|
:param version: version of XML to be used. Defaults to 1.0
|
|
|
|
:type encoding: string
|
|
|
|
:param encoding: encoding method to be used. Defaults to "UTF-8"
|
|
|
|
:type standalone: string
|
|
|
|
:param standalone: "yes" or "no". Defaults to "no"
|
|
|
|
"""
|
|
|
|
xmldecl = Html.xmldecl(
|
|
|
|
version=version,
|
|
|
|
encoding=encoding,
|
|
|
|
standalone=standalone
|
|
|
|
)
|
|
|
|
self[0:0] = [xmldecl]
|
2009-03-03 23:23:35 +05:30
|
|
|
#
|
2009-03-16 21:47:44 +05:30
|
|
|
def addDOCTYPE(self, name='html', external_id=_XHTML10_STRICT, *args):
|
2009-03-20 21:52:12 +05:30
|
|
|
"""
|
|
|
|
Add a DOCTYPE statement to the start of the list
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type name: string
|
|
|
|
:param name: name of this DOCTYPE. Defaults to "html"
|
|
|
|
:type external_id: string
|
|
|
|
:param external_id: external identifier of this DOCTYPE.
|
2009-03-20 21:52:12 +05:30
|
|
|
Defaults to XHTML 1.0 STRICT
|
2009-06-25 03:26:07 +05:30
|
|
|
:type args: object
|
|
|
|
:param args: 0 or more positional parameters to be added to this
|
2009-03-20 21:52:12 +05:30
|
|
|
DOCTYPE.
|
|
|
|
"""
|
|
|
|
doctype = '<!DOCTYPE %s %s%s>' % (
|
|
|
|
name,
|
|
|
|
external_id,
|
|
|
|
' %s'*len(args) % args
|
|
|
|
)
|
2009-04-03 01:23:30 +05:30
|
|
|
# Note: DOCTYPE declaration must follow XML declaration
|
|
|
|
|
2009-03-05 02:55:18 +05:30
|
|
|
if len(self) and self[0][:6] == '<?xml ':
|
2009-04-03 01:23:30 +05:30
|
|
|
self[1:1] = [doctype]
|
2009-03-03 23:23:35 +05:30
|
|
|
else:
|
2009-04-03 01:23:30 +05:30
|
|
|
self[0:0] = [doctype]
|
|
|
|
#
|
|
|
|
def __gettag(self):
|
|
|
|
"""
|
|
|
|
Returns HTML tag for this object
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: string
|
|
|
|
:returns: HTML tag
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
return self[0].split()[0].strip('< >')
|
|
|
|
#
|
|
|
|
def __settag(self, newtag):
|
|
|
|
"""
|
|
|
|
Sets a new HTML tag for this object
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type name: string
|
|
|
|
:param name: new HTML tag
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
curtag = self.tag
|
|
|
|
if self[-1] == '</%s>' % curtag:
|
|
|
|
self[-1] = '</%s>' % newtag
|
|
|
|
self[0] = self[0].replace('<' + curtag, '<' + newtag)
|
|
|
|
tag = property(__gettag, __settag)
|
|
|
|
#
|
|
|
|
def __getattr(self):
|
|
|
|
"""
|
|
|
|
Returns HTML attributes for this object
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: string
|
|
|
|
:returns: HTML attributes
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
attr = self[0].strip('<!?>').split(None, 1)
|
|
|
|
return attr[1] if len(attr) > 1 else ''
|
|
|
|
#
|
|
|
|
def __setattr(self, value):
|
|
|
|
"""
|
|
|
|
Sets new HTML attributes for this object
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type name: string
|
|
|
|
:param name: new HTML attributes
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
self[0] = self[0][:len(self.tag)+1] + ' ' + value + self[0][-1:]
|
|
|
|
#
|
|
|
|
def __delattr(self):
|
|
|
|
"""
|
|
|
|
Removes HTML attributes for this object
|
|
|
|
"""
|
|
|
|
self[0] = '<' + self.tag + '>'
|
|
|
|
attr = property(__getattr, __setattr, __delattr)
|
|
|
|
#
|
|
|
|
def __getinside(self):
|
|
|
|
"""
|
|
|
|
Returns list of items between opening and closing tags
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:rtype: list
|
|
|
|
:returns: list of items between opening and closing HTML tags
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
return self[1:-1]
|
|
|
|
#
|
|
|
|
def __setinside(self, value):
|
|
|
|
"""
|
|
|
|
Sets new contents between opening and closing tags
|
|
|
|
|
2009-06-25 03:26:07 +05:30
|
|
|
:type name: list
|
|
|
|
:param name: new HTML contents
|
2009-04-03 01:23:30 +05:30
|
|
|
"""
|
|
|
|
if len(self) < 2:
|
|
|
|
raise AttributeError, 'No closing tag. Cannot set inside value'
|
|
|
|
if isinstance(value, Html) or not hasattr(value, '__iter__'):
|
|
|
|
value = [value]
|
|
|
|
self[1:-1] = value
|
|
|
|
#
|
|
|
|
def __delinside(self):
|
|
|
|
"""
|
|
|
|
Removes contents between opening and closing tag
|
|
|
|
"""
|
|
|
|
if len(self) > 2:
|
|
|
|
self[:] = self[:1] + self[-1:]
|
|
|
|
inside = property(__getinside, __setinside, __delinside)
|
2009-06-03 00:53:38 +05:30
|
|
|
#
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
#
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
|
|
return exc_type is None
|
2009-03-03 23:23:35 +05:30
|
|
|
|
2009-09-22 02:22:36 +05:30
|
|
|
#-------------------------------------------
|
|
|
|
#
|
|
|
|
# Unit tests
|
|
|
|
#
|
|
|
|
#-------------------------------------------
|
|
|
|
|
|
|
|
def htmltest():
|
|
|
|
pass
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from libhtmltest import htmltest
|
|
|
|
htmltest()
|