ó
Ð²Ebc           @` s  d  d l  m Z m Z m 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 d d l
 m Z m Z m Z d d l m Z m Z d  d	 l m Z d
 j e ƒ d Z e j d e d ƒ Z e j d e d ƒ Z i  Z e d ƒ d k Z x» e e j ƒ  ƒ D]§ \ Z  Z! e o9e e! ƒ d k pRe oRe e! ƒ d k r[qn  e! d k r¿e e! ƒ d k r‹e j" e! ƒ Z! n e# e! ƒ Z! e! e k p¬e  j$ ƒ  r¼e  e e! <n  n  qWd „  Z% e d e% ƒ d e& d „ Z' d e( f d „  ƒ  YZ) d e* f d „  ƒ  YZ+ d S(   i    (   t   absolute_importt   divisiont   unicode_literals(   t	   text_typeN(   t   register_errort   xmlcharrefreplace_errorsi   (   t   voidElementst   booleanAttributest   spaceCharacters(   t   rcdataElementst   entitiest   xmlEntities(   t   treewalkerst   _utils(   t   escapeu    u   "'=<>`u   [u   ]u_    	
 /`Â áš€á Žá â€€â€â€‚â€ƒâ€„â€…â€†â€‡â€ˆâ€‰â€Šâ€¨â€©â€¯âŸã€€]u   ô¿¿i   u   &c   
      C` s  t  |  t t f ƒ rqg  } g  } t } x´ t |  j |  j |  j !ƒ D]– \ } } | rb t } qD n  | |  j } t j	 |  j | t
 |  j | d g ƒ !ƒ rÁ t j |  j | | d !ƒ } t } n t | ƒ } | j | ƒ qD Wxz | D]r } t j | ƒ }	 |	 r<| j d ƒ | j |	 ƒ |	 j d ƒ sW| j d ƒ qWqå | j d t | ƒ d ƒ qå Wd j | ƒ |  j f St |  ƒ Sd  S(   Ni   u   &u   ;u   &#x%s;u    (   t
   isinstancet   UnicodeEncodeErrort   UnicodeTranslateErrort   Falset	   enumeratet   objectt   startt   endR   t   isSurrogatePairt   mint   surrogatePairToCodepointt   Truet   ordt   appendt   _encode_entity_mapt   gett   endswitht   hext   joinR   (
   t   exct   rest
   codepointst   skipt   it   ct   indext	   codepointt   cpt   e(    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyt   htmlentityreplace_errors*   s0    ),	u   htmlentityreplaceu   etreec         K` s1   t  j | ƒ } t |   } | j | |  ƒ | ƒ S(   u˜  Serializes the input token stream using the specified treewalker

    :arg input: the token stream to serialize

    :arg tree: the treewalker to use

    :arg encoding: the encoding to use

    :arg serializer_opts: any options to pass to the
        :py:class:`html5lib.serializer.HTMLSerializer` that gets created

    :returns: the tree serialized as a string

    Example:

    >>> from html5lib.html5parser import parse
    >>> from html5lib.serializer import serialize
    >>> token_stream = parse('<html><body><p>Hi!</p></body></html>')
    >>> serialize(token_stream, omit_optional_tags=False)
    '<html><head></head><body><p>Hi!</p></body></html>'

    (   R   t   getTreeWalkert   HTMLSerializert   render(   t   inputt   treet   encodingt   serializer_optst   walkert   s(    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyt	   serializeK   s    R.   c           B` s¡   e  Z d  Z d Z e Z e Z e Z e Z	 e Z
 e Z e Z e Z e Z e Z e Z e Z d Z d „  Z d „  Z d „  Z d d „ Z d d „ Z d d „ Z RS(   u   legacyu   "u   quote_attr_valuesu
   quote_charu   use_best_quote_charu   omit_optional_tagsu   minimize_boolean_attributesu   use_trailing_solidusu   space_before_trailing_solidusu   escape_lt_in_attrsu   escape_rcdatau   resolve_entitiesu   alphabetical_attributesu   inject_meta_charsetu   strip_whitespaceu   sanitizec      	   K` s±   t  | ƒ t  |  j ƒ } t | ƒ d k rJ t d t t | ƒ ƒ ƒ ‚ n  d | k rb t |  _ n  x6 |  j D]+ } t |  | | j	 | t
 |  | ƒ ƒ ƒ ql Wg  |  _ t |  _ d S(   uB
  Initialize HTMLSerializer

        :arg inject_meta_charset: Whether or not to inject the meta charset.

            Defaults to ``True``.

        :arg quote_attr_values: Whether to quote attribute values that don't
            require quoting per legacy browser behavior (``"legacy"``), when
            required by the standard (``"spec"``), or always (``"always"``).

            Defaults to ``"legacy"``.

        :arg quote_char: Use given quote character for attribute quoting.

            Defaults to ``"`` which will use double quotes unless attribute
            value contains a double quote, in which case single quotes are
            used.

        :arg escape_lt_in_attrs: Whether or not to escape ``<`` in attribute
            values.

            Defaults to ``False``.

        :arg escape_rcdata: Whether to escape characters that need to be
            escaped within normal elements within rcdata elements such as
            style.

            Defaults to ``False``.

        :arg resolve_entities: Whether to resolve named character entities that
            appear in the source tree. The XML predefined entities &lt; &gt;
            &amp; &quot; &apos; are unaffected by this setting.

            Defaults to ``True``.

        :arg strip_whitespace: Whether to remove semantically meaningless
            whitespace. (This compresses all whitespace to a single space
            except within ``pre``.)

            Defaults to ``False``.

        :arg minimize_boolean_attributes: Shortens boolean attributes to give
            just the attribute value, for example::

              <input disabled="disabled">

            becomes::

              <input disabled>

            Defaults to ``True``.

        :arg use_trailing_solidus: Includes a close-tag slash at the end of the
            start tag of void elements (empty elements whose end tag is
            forbidden). E.g. ``<hr/>``.

            Defaults to ``False``.

        :arg space_before_trailing_solidus: Places a space immediately before
            the closing slash in a tag using a trailing solidus. E.g.
            ``<hr />``. Requires ``use_trailing_solidus=True``.

            Defaults to ``True``.

        :arg sanitize: Strip all unsafe or unknown constructs from output.
            See :py:class:`html5lib.filters.sanitizer.Filter`.

            Defaults to ``False``.

        :arg omit_optional_tags: Omit start/end tags that are optional.

            Defaults to ``True``.

        :arg alphabetical_attributes: Reorder attributes to be in alphabetical order.

            Defaults to ``False``.

        i    u2   __init__() got an unexpected keyword argument '%s'u
   quote_charN(   t	   frozensett   optionst   lent	   TypeErrort   nextt   iterR   t   use_best_quote_chart   setattrR   t   getattrt   errorst   strict(   t   selft   kwargst   unexpected_argst   attr(    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyt   __init__‡   s    O)	c         C` s9   t  | t ƒ s t ‚ |  j r1 | j |  j d ƒ S| Sd  S(   Nu   htmlentityreplace(   R   R   t   AssertionErrorR2   t   encode(   RB   t   string(    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyRH   à   s    	c         C` s9   t  | t ƒ s t ‚ |  j r1 | j |  j d ƒ S| Sd  S(   Nu   strict(   R   R   RG   R2   RH   (   RB   RI   (    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyt   encodeStrictç   s    	c         c` sn  | |  _  t } g  |  _ | rI |  j rI d d l m } | | | ƒ } n  |  j rq d d l m } | | ƒ } n  |  j r™ d d l	 m } | | ƒ } n  |  j
 rÁ d d l m } | | ƒ } n  |  j ré d d l m } | | ƒ } n  x~| D]v} | d } | d k räd | d } | d r9| d | d 7} n | d	 rP| d
 7} n  | d	 rÉ| d	 j d ƒ d k r¥| d	 j d ƒ d k rœ|  j d ƒ n  d } n d } | d | | d	 | f 7} n  | d 7} |  j | ƒ Vqð | d3 k ra| d k s| rF| r1| d j d ƒ d k r1|  j d ƒ n  |  j | d ƒ Vqf|  j t | d ƒ ƒ Vqð | d4 k r.| d }	 |  j d |	 ƒ V|	 t k r¨|  j r¨t } n | r¾|  j d ƒ n  x| d j ƒ  D]
\ \ }
 } } | } | } |  j d ƒ V|  j | ƒ V|  j sI| t j |	 t ƒ  ƒ k rÏ| t j d t ƒ  ƒ k rÏ|  j d ƒ V|  j d k sxt | ƒ d k rt } nZ |  j d k r¨t j | ƒ d  k	 } n3 |  j d k rÏt  j | ƒ d  k	 } n t! d  ƒ ‚ | j" d! d" ƒ } |  j# r| j" d# d$ ƒ } n  | rÈ|  j$ } |  j% rhd | k rDd | k rDd } qhd | k rhd | k rhd } qhn  | d k r‰| j" d d% ƒ } n | j" d d& ƒ } |  j | ƒ V|  j | ƒ V|  j | ƒ VqÙ|  j | ƒ VqÏqÏW|	 t& k r|  j' r|  j( r|  j d' ƒ Vq|  j d( ƒ Vn  |  j d ƒ Vqð | d) k r„| d }	 |	 t k rYt } n | ro|  j d ƒ n  |  j d* |	 ƒ Vqð | d+ k rØ| d } | j d, ƒ d k r¿|  j d- ƒ n  |  j d. | d ƒ Vqð | d/ k rU| d }	 |	 d0 } | t) k r|  j d1 |	 ƒ n  |  j* r:| t+ k r:t) | } n
 d2 |	 } |  j | ƒ Vqð |  j | d ƒ qð Wd  S(5   Ni   (   t   Filteru   typeu   Doctypeu   <!DOCTYPE %su   nameu   publicIdu    PUBLIC "%s"u   systemIdu    SYSTEMu   "i    u   'uB   System identifier contains both single and double quote charactersu    %s%s%su   >u
   Charactersu   SpaceCharactersu   datau   </u   Unexpected </ in CDATAu   StartTagu   EmptyTagu   <%su+   Unexpected child element of a CDATA elementu    u    u   =u   alwaysu   specu   legacyu?   quote_attr_values must be one of: 'always', 'spec', or 'legacy'u   &u   &amp;u   <u   &lt;u   &#39;u   &quot;u    /u   /u   EndTagu   </%s>u   Commentu   --u   Comment contains --u	   <!--%s-->u   Entityu   ;u   Entity %s not recognizedu   &%s;(   u
   Charactersu   SpaceCharacters(   u   StartTagu   EmptyTag(,   R2   R   R@   t   inject_meta_charsett   filters.inject_meta_charsetRK   t   alphabetical_attributest   filters.alphabeticalattributest   strip_whitespacet   filters.whitespacet   sanitizet   filters.sanitizert   omit_optional_tagst   filters.optionaltagst   findt   serializeErrorRJ   RH   R   R	   t   escape_rcdataR   t   itemst   minimize_boolean_attributesR   R   t   tuplet   quote_attr_valuesR9   t   _quoteAttributeSpect   searcht   Nonet   _quoteAttributeLegacyt
   ValueErrort   replacet   escape_lt_in_attrst
   quote_charR=   R   t   use_trailing_solidust   space_before_trailing_solidusR
   t   resolve_entitiesR   (   RB   t
   treewalkerR2   t   in_cdataRK   t   tokent   typet   doctypeRd   t   namet   _t	   attr_namet
   attr_valuet   kt   vt
   quote_attrt   datat   key(    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyR6   î   sØ    						



	

	#
!						
	



c         C` sE   | r% d j  t |  j | | ƒ ƒ ƒ Sd j  t |  j | ƒ ƒ ƒ Sd S(   un  Serializes the stream from the treewalker into a string

        :arg treewalker: the treewalker to serialize

        :arg encoding: the string encoding to use

        :returns: the serialized tree

        Example:

        >>> from html5lib import parse, getTreeWalker
        >>> from html5lib.serializer import HTMLSerializer
        >>> token_stream = parse('<html><body>Hi!</body></html>')
        >>> walker = getTreeWalker('etree')
        >>> serializer = HTMLSerializer(omit_optional_tags=False)
        >>> serializer.render(walker(token_stream))
        '<html><head></head><body>Hi!</body></html>'

        t    u    N(   R!   t   listR6   (   RB   Rh   R2   (    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyR/   w  s    u   XXX ERROR MESSAGE NEEDEDc         C` s&   |  j  j | ƒ |  j r" t ‚ n  d  S(   N(   R@   R   RA   t   SerializeError(   RB   Rt   (    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyRW     s    	(   u   quote_attr_valuesu
   quote_charu   use_best_quote_charu   omit_optional_tagsu   minimize_boolean_attributesu   use_trailing_solidusu   space_before_trailing_solidusu   escape_lt_in_attrsu   escape_rcdatau   resolve_entitiesu   alphabetical_attributesu   inject_meta_charsetu   strip_whitespaceu   sanitizeN(   t   __name__t
   __module__R\   Rd   R   R=   RT   RZ   R   Re   Rf   Rc   RX   Rg   RN   RL   RP   RR   R8   RF   RH   RJ   R_   R6   R/   RW   (    (    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyR.   h   s4        	Y		‰Rx   c           B` s   e  Z d  Z RS(   u   Error in serialized tree(   Ry   Rz   t   __doc__(    (    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyRx   —  s   (,   t
   __future__R    R   R   t   pip._vendor.sixR   t   ret   codecsR   R   t	   constantsR   R   R   R	   R
   R   Rv   R   R   t   xml.sax.saxutilsR   R!   t   _quoteAttributeSpecCharst   compileR]   R`   R   R9   t   _is_ucs4Rw   RY   Rq   Rr   R   R   t   islowerR,   R_   R6   R   R.   t	   ExceptionRx   (    (    (    s<   /tmp/pip-build-UPPWic/pip/pip/_vendor/html5lib/serializer.pyt   <module>   s:   
		ÿ 0