
«_'                 @   s  d  Z  d d l Z d d l Z d d l m Z d d l m Z d d l m Z d d l m Z d d l m Z d d	 l m	 Z	 d d
 l m
 Z
 d Z d d d d g Z e j d  Z e j d  Z Gd d   d e	  Z Gd d   d e  Z e e	 d  rGd d   d e j  Z d d   Z Gd d   d e  Z y  d d l m Z m Z m Z Wn. e k
 rd d l m Z m Z m Z Yn Xe se Z e j d  d S)z
markupsafe
~~~~~~~~~~

Implements an escape function and a Markup string to replace HTML
special characters with safe representations.

:copyright: 2010 Pallets
:license: BSD-3-Clause
    N   )	int_types)	iteritems)Mapping)PY2)string_types)	text_type)unichrz1.1.1Markupsoft_unicodeescapeescape_silentz(<!--.*?-->|<[^>]*>)z&([^& ;]+);c                   s  e  Z d  Z d Z f  Z d d d d d  Z d d   Z   f d	 d
   Z d d   Z d d   Z	 e	 Z
 d d   Z d d   Z d d   Z e j j e _ d d   Z e j j e _ d d   Z e j j e _ d d   Z e j j e _ d d   Z d d   Z e d d     Z d! d"   Z x! d< D] Z e e  e   e <q*Wd3 d4   Z d5 d6   Z d7 d8   Z d9 d:   Z e e d;  re d;  Z [ [   S)=r
   a  A string that is ready to be safely inserted into an HTML or XML
    document, either because it was escaped or because it was marked
    safe.

    Passing an object to the constructor converts it to text and wraps
    it to mark it safe without escaping. To escape the text, use the
    :meth:`escape` class method instead.

    >>> Markup('Hello, <em>World</em>!')
    Markup('Hello, <em>World</em>!')
    >>> Markup(42)
    Markup('42')
    >>> Markup.escape('Hello, <em>World</em>!')
    Markup('Hello &lt;em&gt;World&lt;/em&gt;!')

    This implements the ``__html__()`` interface that some frameworks
    use. Passing an object that implements ``__html__()`` will wrap the
    output of that method, marking it safe.

    >>> class Foo:
    ...     def __html__(self):
    ...         return '<a href="/foo">foo</a>'
    ...
    >>> Markup(Foo())
    Markup('<a href="/foo">foo</a>')

    This is a subclass of the text type (``str`` in Python 3,
    ``unicode`` in Python 2). It has the same methods as that type, but
    all methods escape their arguments and return a ``Markup`` instance.

    >>> Markup('<em>%s</em>') % 'foo & bar'
    Markup('<em>foo &amp; bar</em>')
    >>> Markup('<em>Hello</em> ') + '<foo>'
    Markup('<em>Hello</em> &lt;foo&gt;')
     Nstrictc             C   sM   t  | d  r | j   } | d  k r7 t j |  |  St j |  | | |  S)N__html__)hasattrr   r   __new__)clsbaseencodingerrors r   9/tmp/pip-build-5gj8f0j9/MarkupSafe/markupsafe/__init__.pyr   F   s
    zMarkup.__new__c             C   s   |  S)Nr   )selfr   r   r   r   M   s    zMarkup.__html__c                sJ   t  | t  s t | d  rF |  j t t |   j |  j |    St S)Nr   )	
isinstancer   r   	__class__superr
   __add__r   NotImplemented)r   other)r   r   r   r   P   s    (zMarkup.__add__c             C   s8   t  | d  s t | t  r4 |  j |  j |   St S)Nr   )r   r   r   r   r   r   )r   r   r   r   r   __radd__U   s    zMarkup.__radd__c             C   s,   t  | t  r( |  j t j |  |   St S)N)r   r   r   r   __mul__r   )r   numr   r   r   r!   Z   s    zMarkup.__mul__c                s\   t  | t  r1 t   f d d   | D  } n t |   j  }   j t j   |   S)Nc             3   s!   |  ] } t  |   j  Vq d  S)N)_MarkupEscapeHelperr   ).0x)r   r   r   	<genexpr>c   s    z!Markup.__mod__.<locals>.<genexpr>)r   tupler#   r   r   r   __mod__)r   argr   )r   r   r(   a   s    "zMarkup.__mod__c             C   s   d |  j  j t j |   f S)Nz%s(%s))r   __name__r   __repr__)r   r   r   r   r+   h   s    zMarkup.__repr__c             C   s%   |  j  t j |  t |  j |    S)N)r   r   joinmapr   )r   seqr   r   r   r,   k   s    zMarkup.joinc             O   s%   t  t |  j t j |  | |    S)N)listr-   r   r   split)r   argskwargsr   r   r   r0   p   s    zMarkup.splitc             O   s%   t  t |  j t j |  | |    S)N)r/   r-   r   r   rsplit)r   r1   r2   r   r   r   r3   u   s    zMarkup.rsplitc             O   s%   t  t |  j t j |  | |    S)N)r/   r-   r   r   
splitlines)r   r1   r2   r   r   r   r4   z   s    zMarkup.splitlinesc                s8   d d l  m     f d d   } t j | t |    S)u   Convert escaped markup back into a text string. This replaces
        HTML entities with the characters they represent.

        >>> Markup('Main &raquo; <em>About</em>').unescape()
        'Main » <em>About</em>'
        r   )HTML_ENTITIESc                s   |  j  d  } |   k r) t   |  Sy` | d  d  d k r_ t t | d d   d   S| j d  r t t | d d     SWn t k
 r Yn X|  j    S)Nr      #x#X   #)r7   r8   )groupr	   int
startswith
ValueError)mname)r5   r   r   handle_match   s    z%Markup.unescape.<locals>.handle_match)
_constantsr5   
_entity_resubr   )r   rA   r   )r5   r   unescape   s    zMarkup.unescapec             C   s1   d j  t j d |   j    } t |  j   S)u   :meth:`unescape` the markup, remove tags, and normalize
        whitespace to single spaces.

        >>> Markup('Main &raquo;	<em>About</em>').striptags()
        'Main » About'
         r   )r,   _striptags_rerD   r0   r
   rE   )r   strippedr   r   r   	striptags   s    !zMarkup.striptagsc             C   s)   t  |  } | j |  k	 r% |  |  S| S)ztEscape a string. Calls :func:`escape` and ensures that for
        subclasses the correct type is returned.
        )r   r   )r   srvr   r   r   r      s    
zMarkup.escapec                s=   t  t |       f d d   }   j | _   j | _ | S)Nc                sS   t  t |  t |  |  j  } t  | t |  |  j  |  j   |  | |   S)N)_escape_argspecr/   	enumerater   r   r   )r   r1   r2   )origr   r   func   s    !z1Markup.make_simple_escaping_wrapper.<locals>.func)getattrr   r*   __doc__)r@   rO   r   )rN   r   make_simple_escaping_wrapper   s
    z#Markup.make_simple_escaping_wrapper__getitem__
capitalizetitlelowerupperreplaceljustrjustlstriprstripcenterstrip	translate
expandtabsswapcasezfillc             C   s+   t  t |  j t j |  |  j |     S)N)r'   r-   r   r   	partitionr   )r   sepr   r   r   rc      s    zMarkup.partitionc             C   s+   t  t |  j t j |  |  j |     S)N)r'   r-   r   r   
rpartitionr   )r   rd   r   r   r   re      s    zMarkup.rpartitionc             O   s:   t  |  j  } t | |  } |  j | j |  | |   S)N)EscapeFormatterr   _MagicFormatMappingr   vformat)r   r1   r2   	formatterr   r   r   format   s    zMarkup.formatc             C   s   | r t  d   |  S)Nz,Unsupported format specification for Markup.)r>   )r   format_specr   r   r   __html_format__   s    zMarkup.__html_format____getslice__)rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   )r*   
__module____qualname__rQ   	__slots__r   r   r   r    r!   __rmul__r(   r+   r,   r   r0   r3   r4   rE   rI   classmethodr   rR   methodlocalsrc   re   rj   rl   r   rm   r   r   )r   r   r
      s^   #

               
c               @   sF   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	   Z d
 S)rg   zThis class implements a dummy wrapper to fix a bug in the Python
    standard library for string formatting.

    See http://bugs.python.org/issue13598 for information about why
    this is necessary.
    c             C   s   | |  _  | |  _ d |  _ d  S)Nr   )_args_kwargs_last_index)r   r1   r2   r   r   r   __init__   s    		z_MagicFormatMapping.__init__c             C   s_   | d k rT |  j  } |  j  d 7_  y |  j | SWn t k
 rG Yn Xt |  } |  j | S)Nr   r   )rw   ru   LookupErrorstrrv   )r   keyidxr   r   r   rS      s    	z_MagicFormatMapping.__getitem__c             C   s   t  |  j  S)N)iterrv   )r   r   r   r   __iter__   s    z_MagicFormatMapping.__iter__c             C   s   t  |  j  S)N)lenrv   )r   r   r   r   __len__   s    z_MagicFormatMapping.__len__N)r*   rn   ro   rQ   rx   rS   r~   r   r   r   r   r   rg      s
   rg   rj   c               @   s(   e  Z d  Z d d   Z d d   Z d S)rf   c             C   s   | |  _  d  S)N)r   )r   r   r   r   r   rx     s    zEscapeFormatter.__init__c             C   s   t  | d  r! | j |  } n` t  | d  rc | rT t d j | t |     | j   } n t j j |  | t	 |   } t	 |  j
 |   S)Nrl   r   zFormat specifier {0} given, but {1} does not define __html_format__. A class that defines __html__ must define __html_format__ to work with format specifiers.)r   rl   r>   rj   typer   string	Formatterformat_fieldr   r   )r   valuerk   rK   r   r   r   r     s    zEscapeFormatter.format_fieldN)r*   rn   ro   rx   r   r   r   r   r   rf     s   rf   c             C   sI   xB | D]: \ } } t  | d  s1 t | t  r | |  |  | <q W|  S)z,Helper for various string-wrapped functions.r   )r   r   r   )objiterabler   r{   r   r   r   r   rL     s    rL   c               @   sd   e  Z d  Z d Z d d   Z d d   Z d d   Z e Z d d	   Z d
 d   Z	 d d   Z
 d S)r#   zHelper for Markup.__mod__c             C   s   | |  _  | |  _ d  S)N)r   r   )r   r   r   r   r   r   rx   (  s    	z_MarkupEscapeHelper.__init__c             C   s   t  |  j | |  j  S)N)r#   r   r   )r   itemr   r   r   rS   ,  s    z_MarkupEscapeHelper.__getitem__c             C   s   t  |  j |  j   S)N)r   r   r   )r   r   r   r   __str__/  s    z_MarkupEscapeHelper.__str__c             C   s   t  |  j t |  j    S)N)rz   r   reprr   )r   r   r   r   r+   4  s    z_MarkupEscapeHelper.__repr__c             C   s   t  |  j  S)N)r<   r   )r   r   r   r   __int__7  s    z_MarkupEscapeHelper.__int__c             C   s   t  |  j  S)N)floatr   )r   r   r   r   	__float__:  s    z_MarkupEscapeHelper.__float__N)r*   rn   ro   rQ   rx   rS   r   __unicode__r+   r   r   r   r   r   r   r#   %  s   r#   )r   r   r   soft_str) rQ   rer   _compatr   r   r   r   r   r   r	   __version____all__compilerG   rC   r
   rg   r   r   rf   rL   objectr#   Z	_speedupsr   r   r   ImportErrorZ_nativer   appendr   r   r   r   <module>   s4    !