o !b5.@sdZddlZddlmZddlmZmZmZmZm Z m Z m Z m Z gdZ dZGdddeZd d ZGd d d eZGd ddeZeZdS)aSupport 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! N) numeric_types)AttrsMarkup NamespaceQNameStreamSTARTENDTEXT)FragmentElementElementFactorytagzrestructuredtext enc@sneZdZdZdgZddZddZddZd d Zd d Z d dZ ddZ ddZ ddZ ddZddZdS)r z_Represents a markup fragment, which is basically just a list of element or text nodes. childrencCs g|_dS)zCreate a new fragment.N)rselfr0/usr/lib/python3/dist-packages/genshi/builder.py__init__Ws zFragment.__init__cCs t||SNr )rotherrrr__add__[ zFragment.__add__cGs|D]}||q|S)zXAppend any positional arguments as child nodes. :see: `append` )append)rargsargrrr__call__^s zFragment.__call__cCs|Sr) _generaterrrr__iter__gszFragment.__iter__cCsdt|jS)Nz<%s>)type__name__rrrr__repr__jzFragment.__repr__cC t|Sr)strgeneraterrrr__str__mrzFragment.__str__cCst|Sr)six text_typer&rrrr __unicode__pr#zFragment.__unicode__cCr$r)rr&rrrr__html__srzFragment.__html__cCsttftjt}t||r|j|dSt|tr$|j |jdS|durFz |D]}||q+WdSt yE|j|YdSwdS)zAppend 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) rr r( string_typesr isinstancerrr extend TypeError)rnode simple_typeschildrrrrvs    zFragment.appendccsp|jD]1}t|tr|D]}|Vqqt|tr$|D]}|Vqqt|tjs/t|}t|dfVqdSN)Nr4) rr-r rrr(r,r)r )rr2eventrrrrs      zFragment._generatecCr$zYReturn a markup event stream for the fragment. :rtype: `Stream` rrrrrrr& zFragment.generateN)r! __module__ __qualname____doc__ __slots__rrrrr"r'r*r+rrr&rrrrr Qs  r cCshg}t}|D]&\}}|ddd}|dur/||vr/|t|t|f||q t |S)N_-) setitemsrstripreplacerrr(r)addr)kwargsattrsnamesnamevaluerrr_kwargs_to_attrss rIc@s@eZdZdZddgZddZddZdd Zd d Zd d Z dS)r a% 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))