ó
ù¢TQc           @  sý  d  Z  d d l m Z d d l Z d d l Z y  d d l m Z m Z m Z Wn7 e	 k
 r‡ d d l
 m Z d d l m Z m Z n Xd d l m Z m Z d d l m Z m Z d d	 l m Z d d
 l m Z d d l m Z d d d d d g Z dN dO dP dQ g Z d d d d d d g Z e j d ƒ 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% g  e D] Z& e j' e& ƒ ^ qÃƒ e j( ƒ Z) e j d) ƒ Z* e j d* e j ƒ Z+ d+ „  Z' e e' e j, ƒ Z' i d, e- d- ƒ 6d. e- d/ ƒ 6d0 e- d1 ƒ 6d2 e- d ƒ 6d3 e- d ƒ 6d4 e- d5 ƒ 6d6 e- d7 ƒ 6d8 e- d9 ƒ 6d: e- d ƒ 6d; e- d< ƒ 6d= e- d> ƒ 6Z. e. j/ d? „  e0 d@ ƒ Dƒ ƒ dA „  Z1 e e1 e j, ƒ Z1 dB „  Z2 dC „  Z3 dD „  Z4 e5 dE „ Z6 e e6 e j, ƒ Z6 dF „  Z7 e e7 ƒ Z7 dG „  Z8 e e8 e j, ƒ Z8 dH „  Z9 e e9 e j, ƒ Z9 dI „  Z: e e: e j, ƒ Z: dJ „  Z; e e; e j, ƒ Z; dK „  Z< d e5 e5 dL „ Z> e e> e j, ƒ Z> dM „  Z? e e? e j, ƒ Z? d S(R   u'   HTML utilities suitable for global use.iÿÿÿÿ(   t   unicode_literalsN(   t   quotet   urlsplitt
   urlunsplit(   R   (   R   R   (   t   SafeDatat	   mark_safe(   t   force_bytest
   force_text(   t
   allow_lazy(   t   six(   t   normalize_newlinesu   .u   ,u   :u   ;u   .)u   (u   )u   <u   >u   [u   ]u   &lt;u   &gt;u   &middot;u   *u   â€¢u   &#149;u   &bull;u   &#8226;u   &(?!(\w+|#\d+);)u   %(?![0-9A-Fa-f]{2})u   (\s+)u   ^https?://\wu7   ^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org)$u   ^\S+@\S+\.\S+$u   (<a [^>]*?)target=[^\s>]+ub   (?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)u%   ((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)u   |u(   (?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Zu   <[^>]*?>c         C  sL   t  t |  ƒ j d d ƒ j d d ƒ j d d ƒ j d d ƒ j d	 d
 ƒ ƒ S(   ud   
    Returns the given text with ampersands, quotes and angle brackets encoded for use in HTML.
    u   &u   &amp;u   <u   &lt;u   >u   &gt;u   "u   &quot;u   'u   &#39;(   R   R   t   replace(   t   text(    (    s*   ../Django//lib/python/django/utils/html.pyt   escape'   s    u   \u005Cu   \u   \u0027u   'u   \u0022u   "u   \u003Eu   \u003Cu   \u0026u   &u   \u003Du   =u   \u002Du   -u   \u003Bu   \u2028u   â€¨u   \u2029u   â€©c         c  s)   |  ] } t  d  | ƒ d | f Vq d S(   u   %cu   \u%04XN(   t   ord(   t   .0t   z(    (    s*   ../Django//lib/python/django/utils/html.pys	   <genexpr>=   s    i    c         C  s   t  t |  ƒ j t ƒ ƒ S(   u5   Hex encodes characters for use in JavaScript strings.(   R   R   t	   translatet   _js_escapes(   t   value(    (    s*   ../Django//lib/python/django/utils/html.pyt   escapejs?   s    c         C  s!   t  |  t ƒ r |  St |  ƒ Sd S(   uU   
    Similar to escape(), except that it doesn't operate on pre-escaped strings.
    N(   t
   isinstanceR   R   (   R   (    (    s*   ../Django//lib/python/django/utils/html.pyt   conditional_escapeD   s    c         O  s_   t  t | ƒ } t g  t j | ƒ D] \ } } | t | ƒ f ^ q" ƒ } t |  j | | Ž  ƒ S(   uê   
    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.
    (   t   mapR   t   dictR	   t	   iteritemsR   t   format(   t   format_stringt   argst   kwargst	   args_safet   kt   vt   kwargs_safe(    (    s*   ../Django//lib/python/django/utils/html.pyt   format_htmlM   s    4c           s)   t  t |  ƒ j ‡  f d †  | Dƒ ƒ ƒ S(   uõ  
    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>{0} {1}</li>", ((u.first_name, u.last_name)
                                                  for u in users))

    c         3  s$   |  ] } t  ˆ  t | ƒ Œ Vq d  S(   N(   R"   t   tuple(   R   R   (   R   (    s*   ../Django//lib/python/django/utils/html.pys	   <genexpr>h   s   (   R   R   t   join(   t   sepR   t   args_generator(    (   R   s*   ../Django//lib/python/django/utils/html.pyt   format_html_joinX   s    c         C  sŒ   t  |  ƒ }  t j d |  ƒ } | rV g  | D]" } d t | ƒ j d d ƒ ^ q+ } n) g  | D] } d | j d d ƒ ^ q] } d j | ƒ S(   u'   Converts newlines into <p> and <br />s.u   
{2,}u	   <p>%s</p>u   
u   <br />u   

(   R
   t   ret   splitR   R   R$   (   R   t
   autoescapet   parast   p(    (    s*   ../Django//lib/python/django/utils/html.pyt
   linebreaksl   s    2)c         C  s   t  j d t |  ƒ ƒ S(   u.   Returns the given HTML with all tags stripped.u    (   t   strip_tags_ret   subR   (   R   (    (    s*   ../Django//lib/python/django/utils/html.pyt
   strip_tagsw   s    c         C  s   g  | j  ƒ  D] } t j | ƒ ^ q } d d j | ƒ } t j d | t j ƒ } t j d | ƒ } | j d |  ƒ }  | j d |  ƒ }  |  S(   u/   Returns the given HTML with given tags removed.u   (%s)u   |u   <%s(/?>|(\s+[^>]*>))u   </%s>u    (   R)   R(   R   R$   t   compilet   UR/   (   t   htmlt   tagst   tagt   tags_ret   starttag_ret	   endtag_re(    (    s*   ../Django//lib/python/django/utils/html.pyt   remove_tags|   s    (c         C  s   t  j d d t |  ƒ ƒ S(   u8   Returns the given HTML with spaces between tags removed.u   >\s+<u   ><(   R(   R/   R   (   R   (    (    s*   ../Django//lib/python/django/utils/html.pyt   strip_spaces_between_tags‡   s    c         C  s   t  j d d t |  ƒ ƒ S(   u@   Returns the given HTML with all entities (&something;) stripped.u   &(?:\w+|#\d+);u    (   R(   R/   R   (   R   (    (    s*   ../Django//lib/python/django/utils/html.pyt   strip_entitiesŒ   s    c         C  s   t  j d t |  ƒ ƒ S(   uG   Returns the given HTML with all unencoded ampersands encoded correctly.u   &amp;(   t   unencoded_ampersands_reR/   R   (   R   (    (    s*   ../Django//lib/python/django/utils/html.pyt   fix_ampersands‘   s    c         C  s¦   t  |  ƒ \ } } } } } y | j d ƒ j d ƒ } Wn t k
 rJ n Xt | | | | | f ƒ }  d |  k s t j |  ƒ rœ t t |  ƒ d d ƒ}  n  t	 |  ƒ S(   u(   Quotes a URL if it isn't already quoted.u   idnau   asciiu   %t   safes   !*'();:@&=+$,/?#[]~(
   R   t   encodet   decodet   UnicodeErrorR   t   unquoted_percents_ret   searchR   R   R   (   t   urlt   schemet   netloct   patht   queryt   fragment(    (    s*   ../Django//lib/python/django/utils/html.pyt   smart_urlquote–   s    c      
   C  s-  | d „ } t  |  t ƒ } t j t |  ƒ ƒ } xít | ƒ D]ß\ } } d }	 d | k ss d | k ss d | k rêd | d }
 } } x; t D]3 } | j | ƒ rŽ | t	 | ƒ  } | | } qŽ qŽ Wx t
 D]‡ \ } } | j | ƒ r| t	 | ƒ } |
 | }
 n  | j | ƒ rÌ | j | ƒ | j | ƒ d k rÌ | t	 | ƒ  } | | } qÌ qÌ Wd } | ri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q= n Xd | | f } d } n  | rµ| | ƒ } | r‚| r‚t |
 ƒ t | ƒ }
 } t | ƒ t | ƒ } } n  d | | | f } t d |
 | | f ƒ | | <q| rÎt | ƒ | | <q| rt | ƒ | | <qq= | rt | ƒ | | <q= | r= t | ƒ | | <q= q= Wd j | ƒ S(   u²  
    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 link text longer than this limit
    will truncated to trim_url_limit-3 characters and appended with an elipsis.

    If nofollow is True, the URLs in link text will get a rel="nofollow"
    attribute.

    If autoescape is True, the link text and URLs will get autoescaped.
    c         S  s=   | d  k	 r9 t |  ƒ | k r9 d |  t d | d ƒ  p< |  S(   Nu   %s...i    i   (   t   Nonet   lent   max(   t   xt   limit(    (    s*   ../Django//lib/python/django/utils/html.pyt   <lambda>º   s    u   .u   @u   :u    i   u    rel="nofollow"u	   http://%su   idnau   asciiu   mailto:%s@%su   <a href="%s"%s>%s</a>u   %s%s%sN(   R   R   t   word_split_reR)   R   t	   enumerateRK   t   TRAILING_PUNCTUATIONt   endswithRL   t   WRAPPING_PUNCTUATIONt
   startswitht   countt   simple_url_ret   matchRJ   t   simple_url_2_ret   simple_email_ret   rsplitR?   R@   RA   R   R   R$   (   R   t   trim_url_limitt   nofollowR*   t   trim_urlt
   safe_inputt   wordst   it   wordRY   t   leadt   middlet   trailt   punctuationt   openingt   closingRD   t   nofollow_attrt   localt   domaint   trimmed(    (    s*   ../Django//lib/python/django/utils/html.pyt   urlize©   sb    $"	 c         C  s­   d d l  m } | t |  ƒ ƒ }  t j d d |  ƒ }  t j d d |  ƒ }  t |  ƒ }  t j d |  ƒ }  t j d |  ƒ }  d	 „  } t j | |  ƒ }  t	 j d |  ƒ }  |  S(
   uþ  
    Clean the given HTML.  Specifically, do the following:
        * Convert <b> and <i> to <strong> and <em>.
        * Encode all ampersands correctly.
        * Remove all "target" attributes from <a> tags.
        * Remove extraneous HTML, such as presentational tags that open and
          immediately close and <br clear="all">.
        * Convert hard-coded bullets into HTML unordered lists.
        * Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the
          bottom of the text.
    iÿÿÿÿ(   R
   u   <(/?)\s*b\s*>u
   <\1strong>u   <(/?)\s*i\s*>u   <\1em>u   \1u    c         S  sG   |  j  ƒ  j d d ƒ } x$ t D] } | j d | d ƒ } q Wd | S(   Nu   </p>u   </li>u   <p>%su   <li>u   <ul>
%s
</ul>(   t   groupR   t   DOTS(   RY   t   st   d(    (    s*   ../Django//lib/python/django/utils/html.pyt   replace_p_tags
  s    (
   t   django.utils.textR
   R   R(   R/   R=   t   link_target_attribute_ret   html_gunk_ret   hard_coded_bullets_ret   trailing_empty_content_re(   R   R
   Rs   (    (    s*   ../Django//lib/python/django/utils/html.pyt
   clean_htmlô   s    	(   u   (u   )(   u   <u   >(   u   [u   ](   u   &lt;u   &gt;(@   t   __doc__t
   __future__R    R(   t   stringt   urllib.parseR   R   R   t   ImportErrort   urllibt   urlparset   django.utils.safestringR   R   t   django.utils.encodingR   R   t   django.utils.functionalR   t   django.utilsR	   Rt   R
   RS   RU   Rp   R1   R<   RB   RQ   t
   IGNORECASERX   RZ   R[   Ru   Rv   R$   RN   R   t   DOTALLRw   Rx   R.   t	   text_typeR   R   t   updatet   rangeR   R   R"   R'   t   FalseR-   R0   R9   R:   R;   R=   RJ   RK   Rn   Ry   (    (    (    s*   ../Django//lib/python/django/utils/html.pyt   <module>   s€    >														I	 