Back to index

moin  1.9.0~rc2
Public Member Functions | Private Attributes | Static Private Attributes Class Reference

List of all members.

Public Member Functions

def __init__
def __call__
def __getattr__
def __repr__

Private Attributes


Static Private Attributes

tuple _entity_re = re.compile(r'&([^;]+);')
tuple _entities = name2codepoint.copy()
tuple _empty_elements
tuple _boolean_attributes
tuple _plaintext_elements = set(['textarea'])
tuple _c_like_cdata = set(['script', 'style'])

Detailed Description

Helper object for HTML generation.

Per default there are two instances of that class.  The `html` one, and
the `xhtml` one for those two dialects.  The class uses keyword parameters
and positional parameters to generate small snippets of HTML.

Keyword parameters are converted to XML/SGML attributes, positional
arguments are used as children.  Because Python accepts positional
arguments before keyword arguments it's a good idea to use a list with the
star-syntax for some children:

>>> html.p(class_='foo', *[html.a('foo', href='foo.html'), ' ',
...                        html.a('bar', href='bar.html')])
u'<p class="foo"><a href="foo.html">foo</a> <a href="bar.html">bar</a></p>'

This class works around some browser limitations and can not be used for
arbitrary SGML/XML generation.  For that purpose lxml and similar
libraries exist.

Calling the builder escapes the string passed:

>>> html.p(html("<foo>"))

Definition at line 725 of file

Constructor & Destructor Documentation

Definition at line 767 of file

00768     def __init__(self, dialect):
00769         self._dialect = dialect

Member Function Documentation

Definition at line 770 of file

00771     def __call__(self, s):
00772         return escape(s)

Here is the call graph for this function:

Definition at line 773 of file

00774     def __getattr__(self, tag):
00775         if tag[:2] == '__':
00776             raise AttributeError(tag)
00777         def proxy(*children, **arguments):
00778             buffer = ['<' + tag]
00779             write = buffer.append
00780             for key, value in arguments.iteritems():
00781                 if value is None:
00782                     continue
00783                 if key.endswith('_'):
00784                     key = key[:-1]
00785                 if key in self._boolean_attributes:
00786                     if not value:
00787                         continue
00788                     value = self._dialect == 'xhtml' and '="%s"' % key or ''
00789                 else:
00790                     value = '="%s"' % escape(value, True)
00791                 write(' ' + key + value)
00792             if not children and tag in self._empty_elements:
00793                 write(self._dialect == 'xhtml' and ' />' or '>')
00794                 return ''.join(buffer)
00795             write('>')
00796             children_as_string = ''.join(unicode(x) for x in children
00797                                          if x is not None)
00798             if children_as_string:
00799                 if tag in self._plaintext_elements:
00800                     children_as_string = escape(children_as_string)
00801                 elif tag in self._c_like_cdata and self._dialect == 'xhtml':
00802                     children_as_string = '/*<![CDATA[*/%s/*]]>*/' % \
00803                                          children_as_string
00804             buffer.extend((children_as_string, '</%s>' % tag))
00805             return ''.join(buffer)
00806         return proxy

Here is the call graph for this function:

Definition at line 807 of file

00808     def __repr__(self):
00809         return '<%s for %r>' % (
00810             self.__class__.__name__,
00811             self._dialect
00812         )

Member Data Documentation

Initial value:
        'selected', 'checked', 'compact', 'declare', 'defer', 'disabled',
        'ismap', 'multiple', 'nohref', 'noresize', 'noshade', 'nowrap'

Definition at line 759 of file

tuple = set(['script', 'style']) [static, private]

Definition at line 764 of file

Definition at line 768 of file

Initial value:
        'area', 'base', 'basefont', 'br', 'col', 'frame', 'hr', 'img',
        'input', 'isindex', 'link', 'meta', 'param'

Definition at line 755 of file

tuple = name2codepoint.copy() [static, private]

Definition at line 753 of file

tuple = re.compile(r'&([^;]+);') [static, private]

Definition at line 752 of file

tuple = set(['textarea']) [static, private]

Definition at line 763 of file

The documentation for this class was generated from the following file: