Ńň
űătHc @ sŢ d Z y e Wn# e j
o d d k l Z n Xd d k l Z l Z l Z l Z l
Z
l Z l Z l
Z
d d d d g Z d Z d e f d YZ d
Z d e f d YZ d e f d YZ e Z d
S( s Support for programmatically generating markup streams from Python code using
a very simple syntax. The main entry point to this module is the `tag` object
(which is actually an instance of the ``ElementFactory`` class). You should
rarely (if ever) need to directly import and use any of the other classes in
this module.
Elements can be created using the `tag` object using attribute access. For
example:
>>> doc = tag.p('Some text and ', tag.a('a link', href='http://example.org/'), '.')
>>> doc
This produces an `Element` instance which can be further modified to add child
nodes and attributes. This is done by "calling" the element: positional
arguments are added as child nodes (alternatively, the `Element.append` method
can be used for that purpose), whereas keywords arguments are added as
attributes:
>>> doc(tag.br)
>>> print doc
Some text and a link.
If an attribute name collides with a Python keyword, simply append an underscore
to the name:
>>> doc(class_='intro')
>>> print doc
Some text and a link.
As shown above, an `Element` can easily be directly rendered to XML text by
printing it or using the Python ``str()`` function. This is basically a
shortcut for converting the `Element` to a stream and serializing that
stream:
>>> stream = doc.generate()
>>> stream #doctest: +ELLIPSIS
>>> print stream
Some text and a link.
The `tag` object also allows creating "fragments", which are basically lists
of nodes (elements or text) that don't have a parent element. This can be useful
for creating snippets of markup that are attached to a parent element later (for
example in a template). Fragments are created by calling the `tag` object, which
returns an object of type `Fragment`:
>>> fragment = tag('Hello, ', tag.em('world'), '!')
>>> fragment
>>> print fragment
Hello, world!
i˙˙˙˙( t Set( t Attrst Markupt Namespacet QNamet Streamt STARTt ENDt TEXTt Fragmentt Elementt ElementFactoryt tags restructuredtext enc B sz e Z d Z d g Z d Z d Z d Z d Z d Z d Z d Z
d Z d
Z d Z
d Z RS(
s_ Represents a markup fragment, which is basically just a list of element
or text nodes.
t childrenc C s
g | _ d S( s Create a new fragment.N( R
( t self( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt __init__Y s c C s t | | S( N( R ( R t other( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt __add__] s c G s t | i | | S( sX Append any positional arguments as child nodes.
:see: `append`
( t mapt append( R t args( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt __call__` s c C s
| i S( N( t _generate( R ( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt __iter__h s c C s d | i i S( Ns <%s>( t __class__t __name__( R ( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt __repr__k s c C s t | i S( N( t strt generate( R ( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt __str__n s c C s t | i S( N( t unicodeR ( R ( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt __unicode__q s c C s t | i S( N( R R ( R ( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt __html__t s c C s˛ t | t t t t t t f o | i i | ny t | t o | i i
| i nR | d j oD y t | i t
| WqŽ t j
o | i i | qŽ Xn d S( sÇ Append an element or string as child node.
:param node: the node to append; can be an `Element`, `Fragment`, or a
`Stream`, or a Python string or number
N( t
isinstanceR R
t
basestringt intt floatt longR
R R t extendt NoneR t itert TypeError( R t node( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyR w s "
c c s x | i D] } t | t o xu | i D] } | Vq- Wq
t | t o xE | D] } | VqW Wq
t | t p t | } n t | d f Vq
Wd S( Ni˙˙˙˙( Ni˙˙˙˙i˙˙˙˙( R
R! R R R R" R R R' ( R t childt event( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyR s
c C s t | i S( sY Return a markup event stream for the fragment.
:rtype: `Stream`
( R R ( R ( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyR s ( R t
__module__t __doc__t __slots__R R R R R R R R R R R ( ( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyR S s
c C s g } t } x | i D]q \ } } | i d i d d } | d j o= | | j o0 | i t | t | f | i | q q Wt | S( Nt _t -(
t sett itemst rstript replaceR' R R R t addR ( t kwargst attrst namest namet value( ( s4 /usr/lib64/python2.6/site-packages/genshi/builder.pyt _kwargs_to_attrs s
c B sG e Z d Z d d g Z d Z d Z d Z d Z d Z RS( s(
Simple XML output generator based on the builder pattern.
Construct XML elements by passing the tag name to the constructor:
>>> print Element('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)
>>> print Element('span', title='1 < 2')
>>> print Element('span', title='"baz"')
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)
Attribute names that conflict with Python keywords can be specified by
appending an underscore:
>>> print Element('div', class_='warning')
Nested elements can be added to an element using item access notation.
The call notation can also be used for this and for adding attributes
using keyword arguments, as one would do in the constructor.
>>> print Element('ul')(Element('li'), Element('li'))
>>> print Element('a')('Label')
Label
>>> print Element('a')('Label', href="target")
Label
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')
Hello world
>>> print Element('em')(42)
42
>>> print Element('em')('1 < 2')
1 < 2
This technique also allows mixed content:
>>> print Element('p')('Hello ', Element('b')('world'))
Hello world
Quotes are not escaped inside text nodes:
>>> print Element('p')('"Hello"')
"Hello"
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')
some text
Elements with a namespace can be generated using the `Namespace` and/or
`QName` classes:
>>> from genshi.core import Namespace
>>> xhtml = Namespace('http://www.w3.org/1999/xhtml')
>>> print Element(xhtml.html, lang='en')
R t attribc K s/ t i | t | | _ t | | _ d S( N( R R R R R<