
 XV8                 @   s  d  Z  d d l m Z d d l Z d d l m Z d d l m Z m Z d d l	 m
 Z
 m 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 m Z m Z m Z d d
 l m Z d d l m Z m Z e j d  Z  d[ d\ d] d^ d_ d` g Z! d d d d d d g Z" e j d  Z# e j d  Z$ e j d  e j%  Z& e j d! e j%  Z' e j d"  Z( e j d#  Z) e j d$ e j%  Z* e j d% d& j+ d' d(   e" D  e j,  Z- e j d)  Z. e
 e j/ e  d* d+    Z0 i d, e1 d-  6d. e1 d  6d/ e1 d  6d0 e1 d  6d1 e1 d  6d2 e1 d3  6d4 e1 d5  6d6 e1 d7  6d8 e1 d9  6d: e1 d;  6d< e1 d=  6Z2 e2 j3 d> d(   e4 d?  D  e
 e j/ e  d@ dA    Z5 dB dC   Z6 dD dE   Z7 dF dG   Z8 e dH dI dJ   Z9 GdK dL   dL e  Z: dM dN   Z; e dO dP    Z< e dQ dR    Z= dS dT   Z> e d dH dH dU dV   Z? dW dX   Z@ dY dZ   ZA d S)az'HTML utilities suitable for global use.    )unicode_literalsN)six)	force_str
force_text)	keep_lazykeep_lazy_text)RFC3986_GENDELIMSRFC3986_SUBDELIMS)SafeDataSafeText	mark_safe)	parse_qslquoteunquote	urlencodeurlsplit
urlunsplit)normalize_newlines   )HTMLParseError
HTMLParserz^(.*?)([.,:;!]+)$()<>[]&lt;&gt;"'z&middot;*u   •z&#149;z&bull;z&#8226;z&(?!(\w+|#\d+);)z([\s<>"']+)z^https?://\[?\wz>^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org)($|/.*)$z^\S+@\S+\.\S+$z(<a [^>]*?)target=[^\s>]+zb(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)z%((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)|c             c   s   |  ] } t  j |  Vq d  S)N)reescape).0x r'   ?/home/ubuntu/projects/ifolica/build/django/django/utils/html.py	<genexpr>)   s    r)   z((?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Zc             C   sL   t  t |   j d d  j d d  j d d  j d d  j d	 d
   S)a*  
    Returns the given text with ampersands, quotes and angle brackets encoded
    for use in HTML.

    This function always escapes its input, even if it's already escaped and
    marked as such. This may result in double-escaping. If this is a concern,
    use conditional_escape() instead.
    &z&amp;r   z&lt;r   z&gt;r   z&quot;r    z&#39;)r   r   replace)textr'   r'   r(   r$   .   s    
$r$   z\u005C\z\u0027z\u0022z\u003Ez\u003Cz\u0026r*   z\u003D=z\u002D-z\u003B;z\u2028u    z\u2029u    c             c   s)   |  ] } t  d  |  d | f Vq d S)z%cz\u%04XN)ord)r%   zr'   r'   r(   r)   L   s        c             C   s   t  t |   j t   S)z5Hex encodes characters for use in JavaScript strings.)r   r   	translate_js_escapes)valuer'   r'   r(   escapejsO   s    r7   c             C   s'   t  |  d  r |  j   St |   Sd S)z
    Similar to escape(), except that it doesn't operate on pre-escaped strings.

    This function relies on the __html__ convention used both by Django's
    SafeData class and by third-party libraries like markupsafe.
    __html__N)hasattrr8   r$   )r,   r'   r'   r(   conditional_escapeU   s    
r:   c             O   sA   t  t |  } d d   t j |  D } t |  j | |    S)z
    Similar to str.format, but passes all arguments through conditional_escape,
    and calls 'mark_safe' on the result. This function should be used instead
    of str.format or % interpolation to build up small HTML fragments.
    c             S   s%   i  |  ] \ } } t  |  |  q Sr'   )r:   )r%   kvr'   r'   r(   
<dictcomp>i   s   	 zformat_html.<locals>.<dictcomp>)mapr:   r   	iteritemsr   format)format_stringargskwargsZ	args_safeZkwargs_safer'   r'   r(   format_htmlb   s    rD   c                s,   t  t |   j   f d d   | D   S)a  
    A wrapper of format_html, for the common case of a group of arguments that
    need to be formatted using the same format string, and then joined using
    'sep'. 'sep' is also passed through conditional_escape.

    'args_generator' should be an iterator that returns the sequence of 'args'
    that will be passed to format_html.

    Example:

      format_html_join('
', "<li>{} {}</li>", ((u.first_name, u.last_name)
                                                  for u in users))
    c             3   s$   |  ] } t    t |   Vq d  S)N)rD   tuple)r%   rB   )rA   r'   r(   r)   |   s   z#format_html_join.<locals>.<genexpr>)r   r:   join)seprA   Zargs_generatorr'   )rA   r(   format_html_joinm   s    rH   Fc             C   s`   t  t |    }  t j d |   } | r@ d d   | D } n d d   | D } d j |  S)z'Converts newlines into <p> and <br />s.z
{2,}c             S   s,   g  |  ]" } d  t  |  j d d   q S)z	<p>%s</p>
z<br />)r$   r+   )r%   pr'   r'   r(   
<listcomp>   s   	 zlinebreaks.<locals>.<listcomp>c             S   s&   g  |  ] } d  | j  d d   q S)z	<p>%s</p>rI   z<br />)r+   )r%   rJ   r'   r'   r(   rK      s   	 z

)r   r   r#   splitrF   )r6   
autoescapeZparasr'   r'   r(   
linebreaks   s    rN   c               @   sL   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d S)
MLStripperc             C   s$   t  j |   |  j   g  |  _ d  S)N)r   __init__resetfed)selfr'   r'   r(   rP      s    
zMLStripper.__init__c             C   s   |  j  j |  d  S)N)rR   append)rS   dr'   r'   r(   handle_data   s    zMLStripper.handle_datac             C   s   |  j  j d |  d  S)Nz&%s;)rR   rT   )rS   namer'   r'   r(   handle_entityref   s    zMLStripper.handle_entityrefc             C   s   |  j  j d |  d  S)Nz&#%s;)rR   rT   )rS   rW   r'   r'   r(   handle_charref   s    zMLStripper.handle_charrefc             C   s   d j  |  j  S)N )rF   rR   )rS   r'   r'   r(   get_data   s    zMLStripper.get_dataN)__name__
__module____qualname__rP   rV   rX   rY   r[   r'   r'   r'   r(   rO      s
   rO   c             C   su   t    } y | j |   Wn t k
 r2 |  SYn Xy | j   Wn# t k
 rf | j   | j SYn X| j   Sd S)z<
    Internal tag stripping utility used by strip_tags.
    N)rO   feedr   closer[   rawdata)r6   sr'   r'   r(   _strip_once   s    		rc   c             C   s]   t  |   }  xJ d |  k rX d |  k rX t |   } t |  t |   k rO Pn  | }  q W|  S)z.Returns the given HTML with all tags stripped.r   r   )r   rc   len)r6   Z	new_valuer'   r'   r(   
strip_tags   s    
re   c             C   s   t  j d d t |    S)z8Returns the given HTML with spaces between tags removed.z>\s+<z><)r#   subr   )r6   r'   r'   r(   strip_spaces_between_tags   s    rg   c             C   s   d d   } y t  |   \ } } } } } Wn t k
 rI | |   SYn Xy | j d  j d  } Wn t k
 r | |   SYn X| r d d   t | d d D } t |  } n  | |  } | |  } t | | | | | f  S)	z(Quotes a URL if it isn't already quoted.c             S   s<   t  t |    }  t |  d t t t d  }  t |   S)Nsafe~)r   r   r   r	   r   strr   )Zsegmentr'   r'   r(   unquote_quote   s     z%smart_urlquote.<locals>.unquote_quoteidnaasciic             S   s<   g  |  ]2 } t  t | d    t  t | d   f  q S)r   r   )r   r   )r%   qr'   r'   r(   rK      s   	z"smart_urlquote.<locals>.<listcomp>keep_blank_valuesT)r   
ValueErrorencodedecodeUnicodeErrorr   r   r   )urlrk   schemenetlocpathqueryfragmentZquery_partsr'   r'   r(   smart_urlquote   s     		rz   c             C   s  t  |  t  } | d d  } d d   } d d   } t j t |    } x\t |  D]N\ }	 }
 d |
 k s d |
 k s d	 |
 k rtd
 |
 d
 } } } | | | |  \ } } } d } | r d n d
 } t j |  r| | |  \ } } } t |  } n t	 j |  r?| | |  \ } } } t d |  } n d	 | k rt
 j |  r| j d d  \ } } y | j d  j d  } Wn t k
 rwX Yn Xd | | f } d
 } n  | r?| |  } | r| rt |  t |  } } t |  } n  d t |  | | f } t d | | | f  | |	 <q| rXt |
  | |	 <q| rt |
  | |	 <qqX | rt |
  | |	 <qX | rX t |
  | |	 <qX qX Wd
 j |  S)a  
    Converts any URLs in text into clickable links.

    Works on http://, https://, www. links, and also on links ending in one of
    the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).
    Links can have trailing punctuation (periods, commas, close-parens) and
    leading punctuation (opening parens) and it'll still do the right thing.

    If trim_url_limit is not None, the URLs in the link text longer than this
    limit will be truncated to trim_url_limit-3 characters and appended with
    an ellipsis.

    If nofollow is True, the links will get a rel="nofollow" attribute.

    If autoescape is True, the link text and URLs will be autoescaped.
    c             S   sA   | d  k s t  |   | k r" |  Sd |  d  t d | d   S)Nz%s...r      )rd   max)r&   limitr'   r'   r(   trim_url   s    zurlize.<locals>.trim_urlc             S   s   |  | j  d d  j  d d  j  d d  j  d d  j  d	 d
  } | ru | j |  ru | d t |   } n | d k r |  | 7}  d } n  |  | | f S)z
        If input URL is HTML-escaped, unescape it so as we can safely feed it to
        smart_urlquote. For example:
        http://example.com?x=1&amp;y=&lt;2&gt; => http://example.com?x=1&y=<2>
        z&amp;r*   z&lt;r   z&gt;r   z&quot;r   z&#39;r    Nr0   rZ   )r+   endswithrd   )r,   trailZ	unescapedr'   r'   r(   unescape  s    
$
	zurlize.<locals>.unescapec             S   s  d } x | rd } t  j |  } | rU | j d  } | j d  | } d } n  x t D] \ } } | j |  r | t |  d  } |  | 7}  d } n  | j |  r\ | j |  | j |  d k r\ | d t |   } | | } d } q\ q\ Wq	 W|  | | f S)zr
        Trim trailing and wrapping punctuation from `middle`. Return the items
        of the new state.
        TFr      N)TRAILING_PUNCTUATION_REmatchgroupWRAPPING_PUNCTUATION
startswithrd   r   count)leadmiddler   Ztrimmed_somethingr   Zopeningclosingr'   r'   r(   trim_punctuation  s&    		
	"
z urlize.<locals>.trim_punctuation.@:rZ   Nz rel="nofollow"z	http://%sr   rl   rm   zmailto:%s@%sz<a href="%s"%s>%s</a>z%s%s%s)
isinstancer
   word_split_rerL   r   	enumeratesimple_url_rer   rz   simple_url_2_resimple_email_rersplitrq   rr   rs   r$   r   rF   )r,   Ztrim_url_limitZnofollowrM   Z
safe_inputr~   r   r   wordsiwordr   r   r   rt   Znofollow_attrZmiddle_unescapedlocaldomainZtrimmedr'   r'   r(   urlize   sR    $	 r   c             C   s   |  j  d d  S)z
    Avoid text wrapping in the middle of a phrase by adding non-breaking
    spaces where there previously were normal spaces.
         )r+   )r6   r'   r'   r(   avoid_wrappinge  s    r   c                s   d |  j  k r% t d |  j   n  t j r d |  j  k rS t d |  j   n  |  j   f d d   |  _ d d   |  _ nR d |  j  k r t d	 |  j   n  |  j     f d
 d   |  _ d d   |  _ |  S)z
    A decorator that defines the __html__ method. This helps non-Django
    templates to detect classes whose __str__ methods return SafeText.
    r8   z;can't apply @html_safe to %s because it defines __html__().__unicode__zEcan't apply @html_safe to %s because it doesn't define __unicode__().c                s   t    |    S)N)r   )rS   )klass_unicoder'   r(   <lambda>~  s    zhtml_safe.<locals>.<lambda>c             S   s
   t  |   S)N)unicode)rS   r'   r'   r(   r     s    __str__zAcan't apply @html_safe to %s because it doesn't define __str__().c                s   t    |    S)N)r   )rS   )	klass_strr'   r(   r     s    c             S   s
   t  |   S)N)rj   )rS   r'   r'   r(   r     s    )__dict__rp   r\   r   PY2r   r8   r   )klassr'   )r   r   r(   	html_safem  s(    			r   )r   r   )r   r   )r   r   )r   r   )r   r   )r    r    )B__doc__
__future__r   r#   Zdjango.utilsr   Zdjango.utils.encodingr   r   Zdjango.utils.functionalr   r   Zdjango.utils.httpr   r	   Zdjango.utils.safestringr
   r   r   Z#django.utils.six.moves.urllib.parser   r   r   r   r   r   Zdjango.utils.textr   html_parserr   r   compiler   r   ZDOTSZunencoded_ampersands_rer   
IGNORECASEr   r   r   Zlink_target_attribute_reZhtml_gunk_rerF   DOTALLZhard_coded_bullets_reZtrailing_empty_content_re	text_typer$   r1   r5   updateranger7   r:   rD   rH   rN   rO   rc   re   rg   rz   r   r   r   r'   r'   r'   r(   <module>   sn   .	) $y