o !bz@s8dZddlmZddlZddlZddlmZmZmZm Z m Z ddlm Z m Z m Z mZmZmZmZmZmZmZmZmZgdZdZd"d d Zd#d d Zd$ddZGdddeZGdddeZGdddeZGdddeZ GdddeZ!GdddeZ"e"j#Z#GdddeZ$GdddeZ%Gd d!d!eZ&dS)%zUThis module provides different kinds of serialization methods for XML event streams. )chainN)escapeAttrsMarkupQNameStreamEventKind) STARTENDTEXTXML_DECLDOCTYPESTART_NSEND_NS START_CDATA END_CDATAPICOMMENT XML_NAMESPACE)encodeget_serializerDocType XMLSerializerXHTMLSerializerHTMLSerializerTextSerializerzrestructuredtext enxmlcspdurd|dkrt|tsdfdd}ndd}|dur*|dt|S|D] }|||q,dS) aEncode 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 Nreplacetextxmlcharrefreplacecs |SN)rstringencodingerrors//usr/lib/python3/dist-packages/genshi/output.py7s zencode..cSs|Srr%r r%r%r&r'9s) isinstancerjoinlistwrite)iteratormethodr#out_encodechunkr%r"r&r srcKs0t|tjrttttd|}|di|S)aReturn 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 )rxhtmlhtmlrNr%)r)six string_typesrrrrlower)r.kwargsr%r%r&r@s rTcs6i|rfdd}j}ndd}dd}||fS)aNPrepare 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. cs|||f<|Srr%kindinputoutputcacher%r&_emit`s z_prepare_cache.._emitcSs|Srr%r8r%r%r&r>ecSsdSrr%)keyr%r%r&_getgr?z_prepare_cache.._get)get) use_cacher>rAr%r<r&_prepare_cacheTs   rDc@sTeZdZdZdZdZdZeZdZdZ dZ dZ e Z d Z d Zd Zd ZeZed dZdS)rzDDefines a number of commonly used DOCTYPE declarations as constants.)r3z-//W3C//DTD HTML 4.01//ENz%http://www.w3.org/TR/html4/strict.dtd)r3z&-//W3C//DTD HTML 4.01 Transitional//ENz$http://www.w3.org/TR/html4/loose.dtd)r3z"-//W3C//DTD HTML 4.01 Frameset//ENz'http://www.w3.org/TR/html4/frameset.dtd)r3NN)r3z -//W3C//DTD XHTML 1.0 Strict//ENz1http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd)r3z&-//W3C//DTD XHTML 1.0 Transitional//ENz7http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd)r3z"-//W3C//DTD XHTML 1.0 Frameset//ENz3http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd)r3z-//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)rEz-//W3C//DTD SVG Basic 1.1//ENz6http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd)rEz-//W3C//DTD SVG Tiny 1.1//ENz5http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtdcCsH|j|jtjtj|j|j|j|j|j |j |j |j |j |jd|S)aReturn 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 )r3z html-strictzhtml-transitionalz html-framesethtml5r2z xhtml-strictzxhtml-transitionalzxhtml-framesetxhtml11rEzsvg-fullz svg-basiczsvg-tiny)HTML HTML_STRICTrHTML_TRANSITIONAL HTML_FRAMESETHTML5XHTML XHTML_STRICTXHTML_TRANSITIONALXHTML_FRAMESETXHTML11SVGSVG_FULL SVG_BASICSVG_TINYrBr6)clsnamer%r%r&rBs z DocType.getN)__name__ __module__ __qualname____doc__rIrJrKrHrLrNrOrPrMrQrSrTrUrR classmethodrBr%r%r%r&rls$rc@s4eZdZdZeZ  d ddZddZdd ZdS) raProduces 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())))


NTcCsRtg|_|r|jt|j|jt||d|r$|jt|||_dS)aInitialize 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 prefixesr=N)EmptyTagFilterfiltersappendWhitespaceFilter_PRESERVE_SPACENamespaceFlattenerDocTypeInserterr=)selfdoctypestrip_whitespacenamespace_prefixesr=r%r%r&__init__s   zXMLSerializer.__init__cCst|jddS)N)rDr=)rfr%r%r&rDszXMLSerializer._prepare_cacheccsd}}d}|\}}|jD]}||}q|D]:\}} } |tur,t| tr,| Vq||| f} | dur:| Vq|tusB|turt| \} } d| g}| D]\}}|d|dt|dg7}qL||turddped||| td |Vq|t ur||| td | Vq|tur|r||| | Vq||| t| dd Vq|t ur||| td | Vq|t ur|s| \}}}d |g}|r|d ||dkr|rdpd}|d||dtd |Vd}q|t ur*|s*| \}}}dg}|r|dn|r |d|r|d|dtd |tdd| DVd}q|tur7tdVd}q|turDtdVd}q|turT||| td| VqdS)NF< =""z/>>r(quotes  T cSg|]}|r|qSr%r%.0pr%r%r& $z*XMLSerializer.__call__.. )rDr`r r)rrEMPTYrrar*r rr r tuplerrr)rfstream have_decl have_doctypein_cdatar>rAfilter_r9dataposcachedtagattribbufattrvalueversionr# standalonerWpubidsysidr%r%r&__call__sz               "     zXMLSerializer.__call__)NTNT) rXrYrZr[ frozensetrcrjrDrr%r%r%r&rs  rcsfeZdZdZegdZegdZeededededgZ dfd d Z d d Z Z S)raProduces 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())))


) areabasebasefontbrcolframehrimgr:isindexlinkmetaparam)selectedcheckedcompactdeclaredeferdisabledismapmultiplenohrefnoresizenoshadenowrap autofocusreadonlyrequiredformnovalidateprez http://www.w3.org/1999/xhtml}pretextareaz%http://www.w3.org/1999/xhtml}textareaNTcsztt||dtg|_|r|jt|j|pi}d|d<|jt||d|r5|jt |||_ ||_ dS)NFr(http://www.w3.org/1999/xhtmlr]) superrrjr_r`rarbrcrdre drop_xml_declr=)rfrgrhrirr= __class__r%r&rjIs   zXHTMLSerializer.__init__ccs,|j}|j}|j}d}}d}|\}} |jD]} | |}q|D]p\} } } | tur5t| tr5| Vq"| | | f}|durC|Vq"| tusK| t ur| \}}d|g}|D]-\}}||vr`|}n|dkrrd|vrr|dt |dg7}n|dkrwqU|d|d t |dg7}qU| t ur||vr| d n | d |n| d || | td |Vq"| t ur|| | td| Vq"| tur|r|| | | Vq"|| | t | ddVq"| tur|| | td| Vq"| tur&|s&| \}}}dg}|r| dn|r| d|r | d| dtd |tdd| DVd}q"| turi|si|si| \}}}d|g}|rE| d||dkrY|rPdpQd}| d|| dtd |Vd}q"| turvtd Vd}q"| turtd!Vd}q"| tur|| | td"| Vq"dS)#NFrlxml:langlang lang="roz xml:spacermrnz />z>rpr(rqrrrtr|r}r~rrcSrr%r%rr%r%r&rrz,XHTMLSerializer.__call__..Trurvrwrxryrzr{rrr)_BOOLEAN_ATTRS _EMPTY_ELEMSrrDr`r r)rrrrrar*r rr rr rrr)rfr boolean_attrs empty_elemsrrrrr>rArr9rrrrrrrrrWrrrr#rr%r%r&rXs            "         zXHTMLSerializer.__call__)NTNTT) rXrYrZr[rrrrrcrjr __classcell__r%r%rr&r3s    rcsJeZdZdZeededededgZd fdd Zd d ZZ S) ra 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())))


scriptz#http://www.w3.org/1999/xhtml}scriptstylez"http://www.w3.org/1999/xhtml}styleNTcsltt||dtg|_|r|jt|j|j|jt ddi|d|r1|jt |d|_ dS)aHInitialize 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 Frr(r]TN) rrrjr_r`rarbrc_NOESCAPE_ELEMSrdrer=)rfrgrhr=rr%r&rjs   zHTMLSerializer.__init__ccs|j}|j}|j}d}d}|\}}|jD]} | |}q|D]6\} } } | tur3t| tr3| Vq || | f} | durX| V| tusH| t urQ| d|vrQd}q | t urWd}q | tus`| t ur| \}}d|g}|D]6\}}||vr{|rz|d|g7}qjd|vr|dkrd|vr|d t |d g7}qj|d kr|d|d t |d g7}qj| d | t ur||vr| d||| | td |V||vrd}q | t ur|| | td| Vd}q | tur|r|| | | Vq || | t | ddVq | tur|| | td| Vq | turG|sG| \}}}dg}|r| dn|r&| d|r.| d| dtd |tdd| DVd}q | turW|| | td| Vq dS)NFrTrlrm:rrrroxmlnsrnrprqr(rrrtr|r}r~rrcSrr%r%rr%r%r&rrz+HTMLSerializer.__call__..r)rrrrDr`r r)rrrr rrar*rr rr)rfrrrnoescape_elemsrnoescaper>rArr9r_r;rrrrrrWrrr%r%r&rs              " zHTMLSerializer.__call__)NTT) rXrYrZr[rrrrjrrr%r%rr&rs  rc@s"eZdZdZdddZddZdS) raDProduces 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('', href='foo'), tag.br) >>> print(elem) >>> print(''.join(TextSerializer()(elem.generate()))) If text events contain literal markup (instances of the `Markup` class), that markup is by default passed through unchanged: >>> elem = tag.div(Markup('Hello & Bye!
')) >>> print(elem.generate().render(TextSerializer, encoding=None)) Hello & Bye!
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! FcCs ||_dS)zCreate the serializer. :param strip_markup: whether markup (tags and encoded characters) found in the text should be removed N) strip_markup)rfrr%r%r&rj;s zTextSerializer.__init__ccsR|j}|D] }|dtur&|d}|r t|tur |}t|VqdS)Nr)rr typer striptags stripentitiesr4 text_type)rfrreventrr%r%r&rCs   zTextSerializer.__call__N)FrXrYrZr[rjrr%r%r%r&rs  rc@s eZdZdZedZddZdS)r_zcCombines `START` and `STOP` events into `EMPTY` events for elements that have no contents. rccsbd}|D])}|dtur#|dtur t|d|df}|Vq|V|dtur,|V|}qdS)NNNNrrrk)rr r)rfrprevevr%r%r&rTs   zEmptyTagFilter.__call__N)rXrYrZr[rrrr%r%r%r&r_Ms r_c@s"eZdZdZdddZddZdS) rdafOutput 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(''' ... ... ''') >>> 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' NTcCs*tjdi|_|dur|j|||_dS)Nr)rurir^updater=)rfr^r=r%r%r&rjzs   zNamespaceFlattener.__init__c#stdd|jDtjdgit|j\}}fdd}fdd}g}|j}dd }d d } | fd d } |D]\} } } | turYt | t rY| | | fVqC|| | f} | durj| | | fVqC| t usr| t ur| \}}|j }|j}|r|vr|d}|rd||f}n |d|f|d|g}|D]6\}}|j }|j}|r|vr| }||||d||fn|d}|rd||f}|||fq|| | |t||f} | | | fV|dd=qC| tur| j }| j}|r |d}|r d||f}| || | || fVqC| tur;| \}}|vr5||gd}|||||||qC| tur\| vr[|| }|r[|| |}||vr[||qC| | | fVqCdS)NcSsg|] \}}||gfqSr%r%)rkvr%r%r&rsz/NamespaceFlattener.__call__..rcs0|g||g|dSr) setdefaultraclearprefixrr= namespacesr^r%r&_push_nss z-NamespaceFlattener.__call__.._push_nscsV|}|}|s|=||vs||dkr%|}||s%|=|S)Nrw)rBpopr)rurisr uri_prefixesrr%r&_pop_nss z,NamespaceFlattener.__call__.._pop_nscSsd|rd|pd|fS)Nzxmlns%sz:%sr(r%rr%r%r& _make_ns_attrsz2NamespaceFlattener.__call__.._make_ns_attrcssd} |d7}d|Vq)Nrrzns%dr%)valr%r%r& _gen_prefixs  z0NamespaceFlattener.__call__.._gen_prefixcstSr)nextr%)_prefix_generatorr%r&r'sz-NamespaceFlattener.__call__..rwz%s:%srr(zxmlns:%s)dictr^itemsrrrDr=rar r)rrr localname namespacerr r rBrremove)rfrr>rArrns_attrs _push_ns_attrrrr9rrr;rattrstagnametagnsr new_attrsrrattrnameattrnsrr%)rr=rr^r&rs                          zNamespaceFlattener.__call__)NTrr%r%r%r&rdfs  rdc@s@eZdZdZd ddZdededjedjfdd Z dS) rbzPA filter that removes extraneous ignorable white space from the stream. NcCs0|durg}t||_|durg}t||_dS)aInitialize 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 ``