
    w:iz                        d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZ g dZdZd d
Zd!dZd"dZ G d de          Z G d de          Z  G d de           Z! G d de!          Z" G d de          Z# G d de          Z$e$j%        Z% G d de          Z& G d de          Z' G d de          Z(dS )#zUThis module provides different kinds of serialization methods for XML event
streams.
    )chainN)string_types	text_type)escapeAttrsMarkupQNameStreamEventKind)STARTENDTEXTXML_DECLDOCTYPESTART_NSEND_NSSTART_CDATA	END_CDATAPICOMMENTXML_NAMESPACE)encodeget_serializerDocTypeXMLSerializerXHTMLSerializerHTMLSerializerTextSerializerzrestructuredtext enxmlc                     &d|dk    rt          |t                    sdfd}nd }|+ |d                    t          |                               S | D ] }|                     ||                     !dS )a  Encode serializer output into a string.
    
    :param iterator: the iterator returned from serializing a stream (basically
                     any iterator that yields unicode objects)
    :param method: the serialization method; determines how characters not
                   representable in the specified encoding are treated
    :param encoding: how the output string should be encoded; if set to `None`,
                     this method returns a `unicode` object
    :param out: a file-like object that the output should be written to
                instead of being returned as one big string; note that if
                this is a file or socket (or similar), the `encoding` must
                not be `None` (that is, the output must be encoded)
    :return: a `str` or `unicode` object (depending on the `encoding`
             parameter), or `None` if the `out` parameter is provided
    
    :since: version 0.4.1
    :note: Changed in 0.5: added the `out` parameter
    Nreplacetextxmlcharrefreplacec                 0    |                                S N)r   )stringencodingerrorss    E/var/www/html/trac/venv/lib/python3.11/site-packages/genshi/output.py<lambda>zencode.<locals>.<lambda>6   s    x!@!@     c                     | S r$    )r%   s    r(   r)   zencode.<locals>.<lambda>8   s     r*    )
isinstancer   joinlistwrite)iteratormethodr&   out_encodechunkr'   s     `   @r(   r   r      s    & VJv~$F$F(F@@@@@''
{wrwwtH~~../// " "		''%..!!!!" "r*   c                     t          | t                    r3t          t          t          t
          d|                                          }  | di |S )a  Return a serializer object for the given method.
    
    :param method: the serialization method; can be either "xml", "xhtml",
                   "html", "text", or a custom serializer class

    Any additional keyword arguments are passed to the serializer, and thus
    depend on the `method` parameter value.
    
    :see: `XMLSerializer`, `XHTMLSerializer`, `HTMLSerializer`, `TextSerializer`
    :since: version 0.4.1
    )r   xhtmlhtmlr!   r,   )r.   r   r   r   r   r   lower)r3   kwargss     r(   r   r   ?   sX     &,'' ;(*))+ + ,2<<>>; 6Fr*   Tc                 <    i | rfd}j         }nd }d }||fS )aN  Prepare a private token serialization cache.

    :param use_cache: boolean indicating whether a real cache should
                      be used or not. If not, the returned functions
                      are no-ops.

    :return: emit and get functions, for storing and retrieving
             serialized values from the cache.
    c                     || |f<   |S r$   r,   )kindinputoutputcaches      r(   _emitz_prepare_cache.<locals>._emit_   s    !'E$+Mr*   c                     |S r$   r,   )r>   r?   r@   s      r(   rB   z_prepare_cache.<locals>._emitd   s    Mr*   c                     d S r$   r,   )keys    r(   _getz_prepare_cache.<locals>._getf   s    Dr*   )get)	use_cacherB   rF   rA   s      @r(   _prepare_cacherI   S   sa     E 		 	 	 	 	 y	 	 		 	 	$r*   c                   `    e Zd ZdZdZdZdZeZdZdZ	dZ
dZe	Zd	Zd
ZdZdZeZed             ZdS )r   zDDefines a number of commonly used DOCTYPE declarations as constants.)r9   z-//W3C//DTD HTML 4.01//ENz%http://www.w3.org/TR/html4/strict.dtd)r9   z&-//W3C//DTD HTML 4.01 Transitional//ENz$http://www.w3.org/TR/html4/loose.dtd)r9   z"-//W3C//DTD HTML 4.01 Frameset//ENz'http://www.w3.org/TR/html4/frameset.dtd)r9   NN)r9   z -//W3C//DTD XHTML 1.0 Strict//ENz1http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd)r9   z&-//W3C//DTD XHTML 1.0 Transitional//ENz7http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)r9   z"-//W3C//DTD XHTML 1.0 Frameset//ENz3http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd)r9   z-//W3C//DTD XHTML 1.1//ENz,http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd)svgz-//W3C//DTD SVG 1.1//ENz0http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd)rK   z-//W3C//DTD SVG Basic 1.1//ENz6http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd)rK   z-//W3C//DTD SVG Tiny 1.1//ENz5http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtdc                    | j         | j        t          j        t          j        | j        | j        | j        | j        | j	        | j
        | j        | j        | j        | j        d                    |                                          S )a  Return the ``(name, pubid, sysid)`` tuple of the ``DOCTYPE``
        declaration for the specified name.
        
        The following names are recognized in this version:
         * "html" or "html-strict" for the HTML 4.01 strict DTD
         * "html-transitional" for the HTML 4.01 transitional DTD
         * "html-frameset" for the HTML 4.01 frameset DTD
         * "html5" for the ``DOCTYPE`` proposed for HTML5
         * "xhtml" or "xhtml-strict" for the XHTML 1.0 strict DTD
         * "xhtml-transitional" for the XHTML 1.0 transitional DTD
         * "xhtml-frameset" for the XHTML 1.0 frameset DTD
         * "xhtml11" for the XHTML 1.1 DTD
         * "svg" or "svg-full" for the SVG 1.1 DTD
         * "svg-basic" for the SVG Basic 1.1 DTD
         * "svg-tiny" for the SVG Tiny 1.1 DTD
        
        :param name: the name of the ``DOCTYPE``
        :return: the ``(name, pubid, sysid)`` tuple for the requested
                 ``DOCTYPE``, or ``None`` if the name is not recognized
        :since: version 0.4.1
        )r9   zhtml-strictzhtml-transitionalzhtml-framesethtml5r8   zxhtml-strictzxhtml-transitionalzxhtml-framesetxhtml11rK   zsvg-fullz	svg-basiczsvg-tiny)HTMLHTML_STRICTr   HTML_TRANSITIONALHTML_FRAMESETHTML5XHTMLXHTML_STRICTXHTML_TRANSITIONALXHTML_FRAMESETXHTML11SVGSVG_FULL	SVG_BASICSVG_TINYrG   r:   )clsnames     r(   rG   zDocType.get   sr    0 HS_!(!:$2YY0@"%"8!0{7
 
 #djjll

	r*   N)__name__
__module____qualname____doc__rP   rQ   rR   rO   rS   rU   rV   rW   rT   rX   rZ   r[   r\   rY   classmethodrG   r,   r*   r(   r   r   k   s        NNKM D ELN EG
HIH C" " [" " "r*   r   c                   >    e Zd ZdZ e            Z	 	 ddZd Zd ZdS )r   a  Produces XML text from an event stream.
    
    >>> from genshi.builder import tag
    >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
    >>> print(''.join(XMLSerializer()(elem.generate())))
    <div><a href="foo"/><br/><hr noshade="True"/></div>
    NTc                 <   t                      g| _        |r,| j                            t          | j                             | j                            t          ||                     |r'| j                            t          |                     || _        dS )a  Initialize the XML serializer.
        
        :param doctype: a ``(name, pubid, sysid)`` tuple that represents the
                        DOCTYPE declaration that should be included at the top
                        of the generated output, or the name of a DOCTYPE as
                        defined in `DocType.get`
        :param strip_whitespace: whether extraneous whitespace should be
                                 stripped from the output
        :param cache: whether to cache the text output per event, which
                      improves performance for repetitive markup
        :note: Changed in 0.4.2: The  `doctype` parameter can now be a string.
        :note: Changed in 0.6: The `cache` parameter was added
        prefixesrA   N)EmptyTagFilterfiltersappendWhitespaceFilter_PRESERVE_SPACENamespaceFlattenerDocTypeInserterrA   )selfdoctypestrip_whitespacenamespace_prefixesrA   s        r(   __init__zXMLSerializer.__init__   s     '(() 	HL 01E F FGGG.8J5:< < < 	= 	= 	= 	:L 8 8999


r*   c                 :    t          | j                  d d         S )N   )rI   rA   )ro   s    r(   rI   zXMLSerializer._prepare_cache   s    dj))"1"--r*   c           
   #   r  K   dx}}d}|                                  \  }}| j        D ]} ||          }|D ]\  }}	}
|t          u rt          |	t                    r|	V  * |||	f          }||V  >|t
          u s	|t          u rx|	\  }}d|g}|D ]\  }}|d|dt          |          dgz  }|                    |t          u rdpd            |||	t	          d	                    |                              V  |t          u r  |||	t	          d	|	z                      V  |t          u r3|r |||	|	          V   |||	t          |	d
                    V  -|t          u r! |||	t	          d|	z                      V  W|t          u r|s|	\  }}}d|z  g}|r|                    d|z             |dk    r|rdpd}|                    d|z             |                    d           t	          d	                    |                    V  d}|t          u r|s|	\  }}}dg}|r|                    d           n|r|                    d           |r|                    d           |                    d           t	          d	                    |                    t          d |	D                       z  V  d}|t          u rt	          d          V  d}|t           u rt	          d          V  d}|t"          u r |||	t	          d|	z                      V   d S )NF< =""z/>>r-   </%s>quotes	<!--%s--><?xml version="%s" encoding="%s"yesno standalone="%s"?>
T<!DOCTYPE %s PUBLIC "%s" SYSTEM "%s">
c                     g | ]}||S r,   r,   .0ps     r(   
<listcomp>z*XMLSerializer.__call__.<locals>.<listcomp>#      3I3I3I!q3IA3I3I3Ir*   	<![CDATA[]]>	<?%s %s?>)rI   ri   r   r.   r   r   EMPTYr   rj   r/   r   r   r   r   tupler   r   r   )ro   stream	have_declhave_doctypein_cdatarB   rF   filter_r>   dataposcachedtagattribbufattrvalueversionr&   
standaloner^   pubidsysids                          r(   __call__zXMLSerializer.__call__   s	     #((	L))++t| 	% 	%GWV__FF% =	D =	DOD$t||
4 8 8|


T4,''F!$%--"VCj#) A AKD%CtVE]]C@@CC

45=1T8S999eD$rwws||(<(<======eD$w~(>(>?????? H%dD111111%dF4,F,F,FGGGGGGeD${T/A(B(BCCCCCC!!)!04-:+g56 <JJ/(:;;;##!+!5!=JJJ1J>???

6"""RWWS\\***** 		%)"eU%& *JJ~.... *JJy))) (JJw'''

5!!!RWWS\\**U3I3It3I3I3I-J-JJJJJ#$$[)))))""Umm### eD${T/A(B(BCCCCC{=	D =	Dr*   )NTNT)	r_   r`   ra   rb   	frozensetrl   rs   rI   r   r,   r*   r(   r   r      sm           ikkO6:04   0. . .DD DD DD DD DDr*   r   c                        e Zd ZdZ eg d          Z eg d          Z e ed           ed           ed           ed          g          Z	 	 d fd
	Z	d Z
 xZS )r   a  Produces XHTML text from an event stream.
    
    >>> from genshi.builder import tag
    >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
    >>> print(''.join(XHTMLSerializer()(elem.generate())))
    <div><a href="foo"></a><br /><hr noshade="noshade" /></div>
    )areabasebasefontbrcolframehrimgr?   isindexlinkmetaparam)selectedcheckedcompactdeclaredeferdisabledismapmultiplenohrefnoresizenoshadenowrap	autofocusreadonlyrequiredformnovalidateprez http://www.w3.org/1999/xhtml}pretextareaz%http://www.w3.org/1999/xhtml}textareaNTc                    t          t          |                               |d           t                      g| _        |r,| j                            t          | j                             |pi }d|d<   | j                            t          ||                     |r'| j                            t          |                     || _
        || _        d S )NFr-   http://www.w3.org/1999/xhtmlrf   )superr   rs   rh   ri   rj   rk   rl   rm   rn   drop_xml_declrA   )ro   rp   rq   rr   r   rA   	__class__s         r(   rs   zXHTMLSerializer.__init__H  s    ot$$--gu===&(() 	HL 01E F FGGG/52=?9:.8J5:< < < 	= 	= 	= 	:L 8 8999*


r*   c           
   #   b  K   | j         }| j        }| j        }dx}}d}|                                 \  }}	| j        D ]}
 |
|          }|D ]a\  }}}|t
          u rt          |t                    r|V  * |	||f          }||V  >|t          u s	|t          u r|\  }}d|g}|D ]J\  }}||v r|}n'|dk    rd|vr|dt          |          dgz  }n|dk    r3|d|d	t          |          dgz  }K|t          u r3||v r|                    d
           n.|                    d|z             n|                    d            |||t          d                    |                              V  (|t          u r! |||t          d|z                      V  R|t
          u r3|r ||||          V  n |||t          |d                    V  |t          u r! |||t          d|z                      V  |t          u r|s|\  }}}dg}|r|                    d           n|r|                    d           |r|                    d           |                    d           t          d                    |                    t!          d |D                       z  V  d}i|t"          u r|s|s|\  }}}d|z  g}|r|                    d|z             |dk    r|rdpd}|                    d|z             |                    d           t          d                    |                    V  d}|t$          u rt          d          V  d}|t&          u rt          d           V  d}9|t(          u r |||t          d!|z                      V  cd S )"NFrw   xml:langlang lang="rz   z	xml:spacerx   ry   z />z></%s>r{   r-   r|   r}   r   r   r   r   r   r   c                     g | ]}||S r,   r,   r   s     r(   r   z,XHTMLSerializer.__call__.<locals>.<listcomp>  r   r*   Tr   r   r   r   r   r   r   r   r   r   )_BOOLEAN_ATTRS_EMPTY_ELEMSr   rI   ri   r   r.   r   r   r   r   rj   r/   r   r   r   r   r   r   r   r   )ro   r   boolean_attrsempty_elemsr   r   r   r   rB   rF   r   r>   r   r   r   r   r   r   r   r   r^   r   r   r   r&   r   s                             r(   r   zXHTMLSerializer.__call__W  s     +'*#((	L))++t| 	% 	%GWV__FF% J	D J	DOD$t||
4 8 8|


T4,''F!$%--"VCj#) A AKD%},, $++f0D0D	6%==#>>,, CtVE]]C@@CC5==k))

5))))

8c>2222JJsOOOeD$rwws||(<(<======eD$w~(>(>?????? H%dD111111%dF4,F,F,FGGGGGGeD${T/A(B(BCCCCCC%)"eU%& *JJ~.... *JJy))) (JJw'''

5!!!RWWS\\**U3I3It3I3I3I-J-JJJJJ#!!)!M!04-:+g56 <JJ/(:;;;##!+!5!=JJJ1J>???

6"""RWWS\\***** 		$$[)))))""Umm### eD${T/A(B(BCCCCCUJ	D J	Dr*   )NTNTT)r_   r`   ra   rb   r   r   r   r	   rl   rs   r   __classcell__r   s   @r(   r   r   2  s          9 ' ' ' ( (L Y  2  2  2 3 3N
  ieee>??j55!HII!  O
 7;DH     TD TD TD TD TD TD TDr*   r   c                        e Zd ZdZ e ed           ed           ed           ed          g          Zd
 fd	Zd	 Z xZ	S )r   a  Produces HTML text from an event stream.
    
    >>> from genshi.builder import tag
    >>> elem = tag.div(tag.a(href='foo'), tag.br, tag.hr(noshade=True))
    >>> print(''.join(HTMLSerializer()(elem.generate())))
    <div><a href="foo"></a><br><hr noshade></div>
    scriptz#http://www.w3.org/1999/xhtml}scriptstylez"http://www.w3.org/1999/xhtml}styleNTc                    t          t          |                               |d           t                      g| _        |r2| j                            t          | j        | j                             | j                            t          ddi|                     |r'| j                            t          |                     d| _        dS )aH  Initialize the HTML serializer.
        
        :param doctype: a ``(name, pubid, sysid)`` tuple that represents the
                        DOCTYPE declaration that should be included at the top
                        of the generated output
        :param strip_whitespace: whether extraneous whitespace should be
                                 stripped from the output
        :param cache: whether to cache the text output per event, which
                      improves performance for repetitive markup
        :note: Changed in 0.6: The `cache` parameter was added
        Fr   r-   rf   TN)r   r   rs   rh   ri   rj   rk   rl   _NOESCAPE_ELEMSrm   rn   rA   )ro   rp   rq   rA   r   s       r(   rs   zHTMLSerializer.__init__  s     	nd##,,We<<<&(() 	HL 01E151E!G !G H H H.*B9
   	 	 	  	:L 8 8999


r*   c           
   #     K   | j         }| j        }| j        }d}d}|                                 \  }}| j        D ]}	 |	|          }|D ]\  }
}}|
t
          u rt          |t                    r|V  * ||
|f          }|/|V  |
t          u s	|
t          u r|d         |v rd}\|
t          u rd}h|
t          u s	|
t          u r|\  }}d|g}|D ]T\  }}||v r
|r|d|gz  }d|v r |dk    rd|vr|d	t          |          d
gz  }7|dk    r|d|dt          |          d
gz  }U|                    d           |
t          u r||vr|                    d|z              ||
|t          d                    |                              V  ||v rd}K|
t          u r# ||
|t          d|z                      V  d}w|
t
          u r3|r ||
||          V   ||
|t          |d                    V  |
t          u r! ||
|t          d|z                      V  |
t          u r|s|\  }}}dg}|r|                    d           n|r|                    d           |r|                    d           |                    d           t          d                    |                    t!          d |D                       z  V  d}|
t"          u r ||
|t          d|z                      V  d S )NFr   Trw   rx   :r   r   r   rz   xmlnsry   r{   r|   r-   r}   r   r   r   r   r   r   c                     g | ]}||S r,   r,   r   s     r(   r   z+HTMLSerializer.__call__.<locals>.<listcomp>  r   r*   r   )r   r   r   rI   ri   r   r.   r   r   r   r   r   rj   r/   r   r   r   r   )ro   r   r   r   noescape_elemsr   noescaperB   rF   r   r>   r   _r@   r   r   r   r   r   r^   r   r   s                         r(   r   zHTMLSerializer.__call__  s     +'-))++t| 	% 	%GWV__FF# <	D <	DMD$t||
4 8 8|


T4,''F!EMMTU]] G~55#HHS[[$H$%--"VCj#) E EKD%},,  /C;.C:--&2F2FIve}}c#BBCT4DD

35==+--

7S=111eD$rwws||(<(<=====.((#HeD$w~(>(>?????  H%dD111111%dF4,F,F,FGGGGGGeD${T/A(B(BCCCCCC%)"eU%& *JJ~.... *JJy))) (JJw'''

5!!!RWWS\\**U3I3It3I3I3I-J-JJJJJ#eD${T/A(B(BCCCCCy<	D <	Dr*   )NTT)
r_   r`   ra   rb   r   r	   r   rs   r   r   r   s   @r(   r   r     s           ihDEEgBCC!  O
     0FD FD FD FD FD FD FDr*   r   c                        e Zd ZdZddZd ZdS )r   aD  Produces plain text from an event stream.
    
    Only text events are included in the output. Unlike the other serializer,
    special XML characters are not escaped:
    
    >>> from genshi.builder import tag
    >>> elem = tag.div(tag.a('<Hello!>', href='foo'), tag.br)
    >>> print(elem)
    <div><a href="foo">&lt;Hello!&gt;</a><br/></div>
    >>> print(''.join(TextSerializer()(elem.generate())))
    <Hello!>

    If text events contain literal markup (instances of the `Markup` class),
    that markup is by default passed through unchanged:
    
    >>> elem = tag.div(Markup('<a href="foo">Hello &amp; Bye!</a><br/>'))
    >>> print(elem.generate().render(TextSerializer, encoding=None))
    <a href="foo">Hello &amp; Bye!</a><br/>
    
    You can use the ``strip_markup`` to change this behavior, so that tags and
    entities are stripped from the output (or in the case of entities,
    replaced with the equivalent character):

    >>> print(elem.generate().render(TextSerializer, strip_markup=True,
    ...                              encoding=None))
    Hello & Bye!
    Fc                     || _         dS )zCreate the serializer.
        
        :param strip_markup: whether markup (tags and encoded characters) found
                             in the text should be removed
        N)strip_markup)ro   r   s     r(   rs   zTextSerializer.__init__:  s     )r*   c              #      K   | j         }|D ]h}|d         t          u rW|d         }|r<t          |          t          u r&|                                                                }t          |          V  id S )Nr      )r   r   typer   	striptagsstripentitiesr   )ro   r   r   eventr   s        r(   r   zTextSerializer.__call__B  s      ( 	& 	&EQx4Qx <DJJ&$8$8>>++99;;Doo%%%	& 	&r*   N)Fr_   r`   ra   rb   rs   r   r,   r*   r(   r   r     sA         8) ) ) )& & & & &r*   r   c                   .    e Zd ZdZ ed          Zd ZdS )rh   zcCombines `START` and `STOP` events into `EMPTY` events for elements that
    have no contents.
    r   c              #      K   d}|D ]T}|d         t           u r.|d         t          u rt          |d         |d         f}|V  ;|V  |d         t           ur|V  |}Ud S )NNNNr   r   ru   )r   r   r   )ro   r   prevevs       r(   r   zEmptyTagFilter.__call__S  s      ! 
	 
	BAw%a5C<< $q'472DJJJJJJ!uE!!DD
	 
	r*   N)r_   r`   ra   rb   r
   r   r   r,   r*   r(   rh   rh   L  s>          OG$$E    r*   rh   c                        e Zd ZdZddZd ZdS )rm   af  Output stream filter that removes namespace information from the stream,
    instead adding namespace attributes and prefixes as needed.
    
    :param prefixes: optional mapping of namespace URIs to prefixes
    
    >>> from genshi.input import XML
    >>> xml = XML('''<doc xmlns="NS1" xmlns:two="NS2">
    ...   <two:item/>
    ... </doc>''')
    >>> for kind, data, pos in NamespaceFlattener()(xml):
    ...     print('%s %r' % (kind, data))
    START ('doc', Attrs([('xmlns', 'NS1'), ('xmlns:two', 'NS2')]))
    TEXT '\n  '
    START ('two:item', Attrs())
    END 'two:item'
    TEXT '\n'
    END 'doc'
    NTc                 r    t           j        di| _        || j                            |           || _        d S )Nr   )r   urirg   updaterA   )ro   rg   rA   s      r(   rs   zNamespaceFlattener.__init__y  s8    &*E2M  ***


r*   c           
   #   \  K   t          d | j                                        D                       t          j        dgit          | j                  \  }}fd}fd}g }|j        }d }d }	 |	            fd}	|D ]\  }
}}|
t          u rt          |t                    r|
||fV  - ||
|f          }||
||fV  D|
t          u s	|
t          u r|\  }}|j        }|j        }|r5|v r|         d         }|r|d	|}n |d
|f            |d|           g }|D ]n\  }}|j        }|j        }|rB|vr' |	            } |||            |d|z  |f           n|         d         }|r|d	|}|                    ||f           o ||
||t          ||z             f          }|
||fV  |d d = @|
t           u r;|j        }|j        }|r|         d         }|r|d	|}|
 ||
||          |fV  |
t"          u rI|\  }}|vr2                    ||g          d         } | |||                      |||           |
t&          u r8|v r2 ||          }|r% |||          }||v r|                    |           |
||fV   d S )Nc                     g | ]
\  }}||gfS r,   r,   )r   kvs      r(   r   z/NamespaceFlattener.__call__.<locals>.<listcomp>  s"    DDDda!aSDDDr*   r   c                                          |g                               |                                 | g                               |                                            d S r$   )
setdefaultrj   clear)prefixr   rA   
namespacesrg   s     r(   _push_nsz-NamespaceFlattener.__call__.<locals>._push_ns  s\    !!#r**11&999++223777KKMMMMMr*   c                                          |           }|                                }|s| = ||vs||d         k    r!|         }|                                 |s|=                                  |S )Nr   )rG   popr   )r   urisr   uri_prefixesrA   r   rg   s       r(   _pop_nsz,NamespaceFlattener.__call__.<locals>._pop_ns  s    <<''D((**C %V$$#b//)#  """# ("3KKMMMJr*   c                     d| rd| z  pdz  |fS )Nzxmlns%sz:%sr-   r,   )r   r   s     r(   _make_ns_attrz2NamespaceFlattener.__call__.<locals>._make_ns_attr  s      956> ?R@#EEr*   c               3   &   K   d} 	 | dz  } d| z  V  )Nr   r   zns%dr,   )vals    r(   _gen_prefixz0NamespaceFlattener.__call__.<locals>._gen_prefix  s.      C#qsl"""#r*   c                  "    t                     S r$   )next)_prefix_generators   r(   r)   z-NamespaceFlattener.__call__.<locals>.<lambda>  s    d#455 r*   r   r   r   r-   zxmlns:%s)dictrg   itemsr   r   rI   rA   rj   r   r.   r   r   r   	localname	namespacer   r   r   rG   r   remove)ro   r   rB   rF   r  r  ns_attrs_push_ns_attrr  r  r>   r   r   r@   r   attrstagnametagnsr   	new_attrsr   r   attrnameattrnsr   r  rA   r   rg   s                            @@@@r(   r   zNamespaceFlattener.__call__  s3     DDdm.A.A.C.CDDDEE#'%1
+DJ77tU	 	 	 	 	 	 		 	 	 	 	 	 	  	F 	F 	F	# 	# 	#
 (KMM5555% B	& B	&OD$t||
4 8 8|D#o%%%T4,''F!FC'''''$%--!
U- ,
**!+E!22!6! B17&AG%w&6777 U+++	#( 8 8KD%#~H!^F D!33%0[]]F$HVV444)M:+>*GHHHH%/%7%;F! D28&&(('CH$$h%67777uT4'5I9M3N3N)OPPD#o%%%QQQKK. >'.r2F >-3VVWW"=EE$g66;;;;;!!"j((%\\#x88<F!M--"<"<===%%%%8##!'$--C 2,}T3778++$OOD111 D#o%%%%EB	& B	&r*   )NTr   r,   r*   r(   rm   rm   e  sF         &   d& d& d& d& d&r*   rm   c                       e Zd ZdZddZded          ej        d          j         ej        d          j        fdZ	dS )	rk   zPA filter that removes extraneous ignorable white space from the
    stream.
    Nc                 f    |g }t          |          | _        |g }t          |          | _        dS )a  Initialize the filter.
        
        :param preserve: a set or sequence of tag names for which white-space
                         should be preserved
        :param noescape: a set or sequence of tag names for which text content
                         should not be escaped
        
        The `noescape` set is expected to refer to elements that cannot contain
        further child elements (such as ``<style>`` or ``<script>`` in HTML
        documents).
        N)r   preserver   )ro   r  r   s      r(   rs   zWhitespaceFilter.__init__  s<     H!(++H!(++r*   spacez
[ 	]+(?=
)z
{2,}c              #     K   t          d          j        }| j        }d}| j        }	d}
g }|j        }|j        }t          |dg          D ]\  }}}|t          u r|
rt          |          } ||           -|rpt          |          dk    r ||d          }|d d = nt           |            d          }|s |d |d|                    }t          t          |          |fV  |t          u r2|\  }}|s||v s|                    |          d	k    r|dz  }|
s||	v rd
}
n*|t          u r
d}
|r|dz  }n|t          u rd
}
n|t          u rd}
|r|||fV  d S )Nr-   r   Fr   r   )escape_quotesr}   
r  T)r   r/   r  r   rj   r  r   r   lenr   r   rG   r   r   r   )ro   r   ctxtr  trim_trailing_spacecollapse_linesmjoinpreserve_elemsr  r   r   textbuf	push_textpop_textr>   r   r   r!   r   r  s                       r(   r   zWhitespaceFilter.__call__  s      r

N	;$V.@-ABB %	* %	*OD$t|| (!$<<D	$ 27||a''$uWEBBB#AAAJJ%hhjj???# S-~d4G4GD4Q4QRRtc11115==!%JC &C>$9$9$)IIe$4$4
$B$B A# (~(=(=#'S[[$H & A[((#HHY&&$H *c/)))K%	* %	*r*   )NN)
r_   r`   ra   rb   rs   r   recompilesubr   r,   r*   r(   rk   rk     sv         , , , ,& %)g0F%/RZ%?%?%C *
8 4 4 81* 1* 1* 1* 1* 1*r*   rk   c                       e Zd ZdZd Zd ZdS )rn   zjA filter that inserts the DOCTYPE declaration in the correct location,
    after the XML declaration.
    c                     t          |t                    rt                              |          }t          |df| _        dS )z_Initialize the filter.

        :param doctype: DOCTYPE as a string or DocType object.
        )Nr   r   N)r.   r   r   rG   r   doctype_event)ro   rp   s     r(   rs   zDocTypeInserter.__init__6  s<    
 g|,, 	+kk'**G%w?r*   c              #      K   d}|D ]4\  }}}|s%d}|t           u r|||fV  | j        V  $| j        V  |||fV  5|s| j        V  d S d S )NFT)r   r1  )ro   r   doctype_insertedr>   r   r   s         r(   r   zDocTypeInserter.__call__?  s       % 		$ 		$OD$# )#' 8##s++++,,,,((((s##### 	%$$$$$$	% 	%r*   Nr   r,   r*   r(   rn   rn   2  s?         @ @ @% % % % %r*   rn   )r   NN)r   )T))rb   	itertoolsr   r,  genshi.compatr   r   genshi.corer   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   __all____docformat__r   r   rI   objectr   r   r   r   r   rh   r   rm   rk   rn   r,   r*   r(   <module>r:     sI          				 1 1 1 1 1 1 1 1 E E E E E E E E E E E E E EK K K K K K K K K K K K K K K K K K K K K K K K K K K KB B B%" " " "@   (   0W W W W Wf W W WtjD jD jD jD jDF jD jD jDZyD yD yD yD yDm yD yD yDxlD lD lD lD lD_ lD lD lD^,& ,& ,& ,& ,&V ,& ,& ,&^    V   , 	~& ~& ~& ~& ~& ~& ~& ~&BI* I* I* I* I*v I* I* I*X% % % % %f % % % % %r*   