Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Private Attributes
MoinMoin.support.htmlmarkup.Element Class Reference
Inheritance diagram for MoinMoin.support.htmlmarkup.Element:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.support.htmlmarkup.Element:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def __call__
def append
def serialize
def __call__
def __unicode__
def __str__
def __add__

Public Attributes

 tagname
 attr
 children

Static Private Attributes

list __slots__ = ['tagname', 'attr']

Detailed Description

Simple XHTML output generator based on the builder pattern.

Construct XHTML elements by passing the tag name to the constructor:

>>> print Element('strong')
<strong></strong>

Attributes can be specified using keyword arguments. The values of the
arguments will be converted to strings and any special XML characters
escaped:

>>> print Element('textarea', rows=10, cols=60)
<textarea rows="10" cols="60"></textarea>
>>> print Element('span', title='1 < 2')
<span title="1 &lt; 2"></span>
>>> print Element('span', title='"baz"')
<span title="&#34;baz&#34;"></span>

The " character is escaped using a numerical entity.
The order in which attributes are rendered is undefined.

If an attribute value evaluates to `None`, that attribute is not included
in the output:

>>> print Element('a', name=None)
<a></a>

Attribute names that conflict with Python keywords can be specified by
appending an underscore:

>>> print Element('div', class_='warning')
<div class="warning"></div>

While the tag names and attributes are not restricted to the XHTML language,
some HTML characteristics such as boolean (minimized) attributes and empty
elements get special treatment.

For compatibility with HTML user agents, some XHTML elements need to be
closed using a separate closing tag even if they are empty. For this, the
close tag is only ommitted for a small set of elements which are known be
be safe for use as empty elements:

>>> print Element('br')
<br />

Trying to add nested elements to such an element will cause an
`AssertionError`:

>>> Element('br')('Oops')
Traceback (most recent call last):
    ...
AssertionError: 'br' elements must not have content

Furthermore, boolean attributes such as "selected" or "checked" are omitted
if the value evaluates to `False`. Otherwise, the name of the attribute is
used for the value:

>>> print Element('option', value=0, selected=False)
<option value="0"></option>
>>> print Element('option', selected='yeah')
<option selected="selected"></option>


Nested elements can be added to an element by calling the instance using
positional arguments. The same technique can also be used for adding
attributes using keyword arguments, as one would do in the constructor:

>>> print Element('ul')(Element('li'), Element('li'))
<ul><li></li><li></li></ul>
>>> print Element('a')('Label')
<a>Label</a>
>>> print Element('a')('Label', href="target")
<a href="target">Label</a>

Text nodes can be nested in an element by adding strings instead of
elements. Any special characters in the strings are escaped automatically:

>>> print Element('em')('Hello world')
<em>Hello world</em>
>>> print Element('em')(42)
<em>42</em>
>>> print Element('em')('1 < 2')
<em>1 &lt; 2</em>

This technique also allows mixed content:

>>> print Element('p')('Hello ', Element('b')('world'))
<p>Hello <b>world</b></p>

Elements can also be combined with other elements or strings using the
addition operator, which results in a `Fragment` object that contains the
operands:

>>> print Element('br') + 'some text' + Element('br')
<br />some text<br />

Definition at line 331 of file htmlmarkup.py.


Constructor & Destructor Documentation

def MoinMoin.support.htmlmarkup.Element.__init__ (   self,
  tagname_ = None,
  attr 
)

Definition at line 430 of file htmlmarkup.py.

00430 
00431     def __init__(self, tagname_=None, **attr):
00432         Fragment.__init__(self)
00433         if tagname_:
00434             self.tagname = tagname_
00435         self.attr = {}
00436         self(**attr)


Member Function Documentation

def MoinMoin.support.htmlmarkup.Fragment.__add__ (   self,
  other 
) [inherited]

Definition at line 327 of file htmlmarkup.py.

00327 
00328     def __add__(self, other):
00329         return Fragment()(self, other)
00330 

def MoinMoin.support.htmlmarkup.Fragment.__call__ (   self,
  args 
) [inherited]

Definition at line 308 of file htmlmarkup.py.

00308 
00309     def __call__(self, *args):
00310         for arg in args:
00311             self.append(arg)
00312         return self

Here is the call graph for this function:

def MoinMoin.support.htmlmarkup.Element.__call__ (   self,
  args,
  attr 
)

Definition at line 437 of file htmlmarkup.py.

00437 
00438     def __call__(self, *args, **attr):
00439         self.attr.update(attr)
00440         return Fragment.__call__(self, *args)

Definition at line 324 of file htmlmarkup.py.

00324 
00325     def __str__(self):
00326         return ''.join(self.serialize())

Here is the call graph for this function:

Definition at line 321 of file htmlmarkup.py.

00321 
00322     def __unicode__(self):
00323         return u''.join(self.serialize())

Here is the call graph for this function:

def MoinMoin.support.htmlmarkup.Element.append (   self,
  node 
)
Append an element or string as child node.

Reimplemented from MoinMoin.support.htmlmarkup.Fragment.

Definition at line 441 of file htmlmarkup.py.

00441 
00442     def append(self, node):
00443         """Append an element or string as child node."""
00444         assert self.tagname not in _EMPTY_TAGS, \
00445             "'%s' elements must not have content" % self.tagname
00446         Fragment.append(self, node)

Here is the caller graph for this function:

Generator that yield tags and text nodes as strings.

Reimplemented from MoinMoin.support.htmlmarkup.Fragment.

Definition at line 447 of file htmlmarkup.py.

00447 
00448     def serialize(self):
00449         """Generator that yield tags and text nodes as strings."""
00450         starttag = ['<', self.tagname]
00451         for name, value in self.attr.items():
00452             if value is None:
00453                 continue
00454             if name in _BOOLEAN_ATTRS:
00455                 if not value:
00456                     continue
00457                 value = name
00458             else:
00459                 name = name.rstrip('_').replace('_', '-')
00460             starttag.append(' %s="%s"' % (name.lower(), escape(value)))
00461 
00462         if self.children or self.tagname not in _EMPTY_TAGS:
00463             starttag.append('>')
00464             yield Markup(''.join(starttag))
00465             for part in Fragment.serialize(self):
00466                 yield part
00467             yield Markup('</%s>', self.tagname)
00468 
00469         else:
00470             starttag.append(' />')
00471             yield Markup(''.join(starttag))
00472 

Here is the caller graph for this function:


Member Data Documentation

Reimplemented from MoinMoin.support.htmlmarkup.Fragment.

Definition at line 428 of file htmlmarkup.py.

Definition at line 434 of file htmlmarkup.py.

Definition at line 291 of file htmlmarkup.py.

Definition at line 433 of file htmlmarkup.py.


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