ó
ù¢TQc           @  s  d  d l  m Z d  d l 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 m Z d  d l m Z d  d l m Z d  d	 l m Z m Z m Z d  d
 l m Z e j sâ d  d l	 m Z n  d „  Z e e e j ƒ Z e j d e j e j Bƒ Z  e j d e j ƒ Z! d „  Z" e e" e j ƒ Z" d e f d „  ƒ  YZ# d d „ Z$ e e$ e j ƒ Z$ d d „ Z% e e% e j ƒ Z% d „  Z& e e& e j ƒ Z& e d ƒ d „ Z' e e' e j ƒ Z' d „  Z( e e( e j ƒ Z( d „  Z) e e) ƒ Z) d „  Z* e e* ƒ Z* d „  Z+ d e, f d „  ƒ  YZ- d „  Z. e j d ƒ Z/ e0 d  „ Z1 e e1 e j ƒ Z1 e j d! e j2 ƒ Z3 d" „  Z4 e e4 e j ƒ Z4 d# „  Z5 e j d$ ƒ Z6 d% „  Z7 e e7 e j ƒ Z7 d& „  Z8 e e8 ƒ Z8 d' „  Z9 e e9 e j ƒ Z9 d S((   iÿÿÿÿ(   t   unicode_literalsN(   t   GzipFile(   t   BytesIO(   t
   force_text(   t
   allow_lazyt   SimpleLazyObject(   t   six(   t   html_entities(   t   ugettext_lazyt   ugettextt   pgettext(   t	   mark_safe(   t   force_unicodec         C  s(   |  o' t  |  ƒ d j ƒ  t  |  ƒ d S(   Ni    i   (   R   t   upper(   t   x(    (    s*   ../Django//lib/python/django/utils/text.pyt   <lambda>   s    u   &.*?;|<.*?>|(\w[\w-]*)u   <(/)?([^ ]+?)(?: (/)| .*?)?>c           s.   t  ˆ  ƒ ‰  ‡  ‡ f d †  } d j | ƒ  ƒ S(   u™   
    A word-wrap function that preserves existing line breaks and most spaces in
    the text. Expects that existing line breaks are posix newlines.
    c          3  sê   t  ˆ  j d ƒ ƒ }  t |  ƒ } | Vt | ƒ | j d ƒ d } x  |  D]˜ } d | k rn | j d ƒ } n	 | f } | t | d ƒ d 7} | ˆ k r³ d Vt | d ƒ } n* d Vt | ƒ d k rÝ t | d ƒ } n  | VqJ Wd  S(   Nu    u   
i   i    iÿÿÿÿ(   t   itert   splitt   nextt   lent   rfind(   t   itt   wordt   post   lines(   t   textt   width(    s*   ../Django//lib/python/django/utils/text.pyt
   _generator$   s     	u    (   R   t   join(   R   R   R   (    (   R   R   s*   ../Django//lib/python/django/utils/text.pyt   wrap   s    t	   Truncatorc           B  sh   e  Z d  Z d „  Z d d „ Z d d „ Z e e ƒ Z d e d „ Z	 e e	 ƒ Z	 d „  Z
 d „  Z RS(   uI   
    An object used to truncate text, either by characters or words.
    c           s#   t  t |  ƒ j ‡  f d †  ƒ d  S(   Nc             s
   t  ˆ  ƒ S(   N(   R   (    (   R   (    s*   ../Django//lib/python/django/utils/text.pyR   @   s    (   t   superR   t   __init__(   t   selfR   (    (   R   s*   ../Django//lib/python/django/utils/text.pyR    ?   s    c         C  sf   | d  k r t d d ƒ } n  t | ƒ } d | k rE | i | d 6S| j | ƒ rX | Sd | | f S(   Nu%   String to return when truncating textu   %(truncated_text)s...u   %(truncated_text)su   truncated_textu   %s%s(   t   NoneR
   R   t   endswith(   R!   R   t   truncate(    (    s*   ../Django//lib/python/django/utils/text.pyt   add_truncation_textB   s    c   
      C  s  t  | ƒ } t j d |  j ƒ } | } xF |  j d | ƒ D]2 } t j | ƒ s: | d 8} | d k rl Pql q: q: Wd } d } x€ t | ƒ D]r \ }	 } t j | ƒ rª q‰ n  | d 7} | d k rÕ | | k rÕ |	 } n  | | k r‰ |  j | | pó d  | ƒ Sq‰ W| S(   u   
        Returns the text truncated to be no longer than the specified number
        of characters.

        Takes an optional argument of what should be used to notify that the
        string has been truncated, defaulting to a translatable string of an
        ellipsis (...).
        u   NFCu    i   i    N(   t   intt   unicodedatat	   normalizet   _wrappedR%   t	   combiningR"   t	   enumerate(
   R!   t   numR$   t   lengthR   t   truncate_lent   chart   s_lent	   end_indext   i(    (    s*   ../Django//lib/python/django/utils/text.pyt   charsR   s(    	

	c         C  s2   t  | ƒ } | r" |  j | | ƒ S|  j | | ƒ S(   uÓ   
        Truncates a string after a certain number of words. Takes an optional
        argument of what should be used to notify that the string has been
        truncated, defaulting to ellipsis (...).
        (   R&   t   _html_wordst   _text_words(   R!   R,   R$   t   htmlR-   (    (    s*   ../Django//lib/python/django/utils/text.pyt   wordsy   s    c         C  sQ   |  j  j ƒ  } t | ƒ | k rD | |  } |  j d j | ƒ | ƒ Sd j | ƒ S(   uw   
        Truncates a string after a certain number of words.

        Newlines in the string will be stripped.
        u    (   R)   R   R   R%   R   (   R!   R-   R$   R7   (    (    s*   ../Django//lib/python/django/utils/text.pyR5   …   s
    
c      	   C  sº  | d k r d Sd } d } d } d } g  } x!| | k rQt  j |  j | ƒ } | s\ Pn  | j d ƒ } | j d ƒ rŸ | d 7} | | k r1 | } q1 q1 n  t j | j d ƒ ƒ }	 |	 s1 | rÊ q1 n  |	 j ƒ  \ }
 } } | j ƒ  } | sN| | k r q1 |
 r>y | j	 | ƒ } Wn t
 k
 r,qNX| | d } q1 | j d | ƒ q1 W| | k re|  j S|  j |  } |  j d | ƒ } | r—| | 7} n  x | D] }	 | d |	 7} qžW| S(   uà   
        Truncates HTML to a certain number of words (not counting tags and
        comments). Closes opened tags if they were correctly closed in the
        given HTML.

        Newlines in the HTML are preserved.
        i    u    u   bru   colu   linku   baseu   imgu   paramu   areau   hru   inputi   u   </%s>(	   u   bru   colu   linku   baseu   imgu   paramu   areau   hru   input(   t   re_wordst   searchR)   t   endt   groupt   re_tagt   matcht   groupst   lowert   indext
   ValueErrort   insertR%   (   R!   R-   R$   t   html4_singletsR   t   end_text_posR7   t	   open_tagst   mt   tagt   closing_tagt   tagnamet   self_closingR2   t   outt   truncate_text(    (    s*   ../Django//lib/python/django/utils/text.pyR4   ‘   sR     
	N(   t   __name__t
   __module__t   __doc__R    R"   R%   R3   R   t   FalseR7   R5   R4   (    (    (    s*   ../Django//lib/python/django/utils/text.pyR   ;   s   	%
	u   ...c         C  sB   t  j d d t ƒ| r# d | p& d } t |  ƒ j | d | ƒS(   NuX   This function has been deprecated. Use the Truncator class in django.utils.text instead.t   categoryu    %su    R$   (   t   warningst   warnt   DeprecationWarningR   R7   (   t   sR,   t   end_textR$   (    (    s*   ../Django//lib/python/django/utils/text.pyt   truncate_wordsÔ   s    c         C  sH   t  j d d t ƒ| r# d | p& d } t |  ƒ j | d | d t ƒS(   NuX   This function has been deprecated. Use the Truncator class in django.utils.text instead.RQ   u    %su    R$   R6   (   RR   RS   RT   R   R7   t   True(   RU   R,   RV   R$   (    (    s*   ../Django//lib/python/django/utils/text.pyt   truncate_html_wordsÛ   s    c         C  s1   t  |  ƒ j ƒ  j d d ƒ }  t j d d |  ƒ S(   u  
    Returns the given string converted to a string that can be used for a clean
    filename. Specifically, leading and trailing spaces are removed; other
    spaces are converted to underscores; and anything that is not a unicode
    alphanumeric, dash, underscore, or dot, is removed.
    >>> get_valid_filename("john's portrait in 2004.jpg")
    'johns_portrait_in_2004.jpg'
    u    u   _u   (?u)[^-\w.]u    (   R   t   stript   replacet   ret   sub(   RU   (    (    s*   ../Django//lib/python/django/utils/text.pyt   get_valid_filenameâ   s    	u   orc         C  sƒ   t  |  ƒ d k r d St  |  ƒ d k r6 t |  d ƒ Sd t d ƒ j g  |  D] } t | ƒ ^ qL d  ƒ t | ƒ t |  d ƒ f S(   u   
    >>> get_text_list(['a', 'b', 'c', 'd'])
    'a, b, c or d'
    >>> get_text_list(['a', 'b', 'c'], 'and')
    'a, b and c'
    >>> get_text_list(['a', 'b'], 'and')
    'a and b'
    >>> get_text_list(['a'])
    'a'
    >>> get_text_list([])
    ''
    i    u    i   u   %s %s %su   , iÿÿÿÿ(   R   R   t   _R   (   t   list_t	   last_wordR2   (    (    s*   ../Django//lib/python/django/utils/text.pyt   get_text_listï   s      /c         C  s   t  t j d d |  ƒ ƒ S(   Nu
   \r\n|\r|\nu   
(   R   R\   R]   (   R   (    (    s*   ../Django//lib/python/django/utils/text.pyt   normalize_newlines  s    c         C  s:   t  |  ƒ j ƒ  }  t j d ƒ } | j d „  |  ƒ }  |  S(   uC   Recapitalizes text, placing caps after end-of-sentence punctuation.u   (?:^|(?<=[\.\?\!] ))([a-z])c         S  s   |  j  d ƒ j ƒ  S(   Ni   (   R;   R   (   R   (    (    s*   ../Django//lib/python/django/utils/text.pyR     s    (   R   R?   R\   t   compileR]   (   R   t   capsRE(    (    s*   ../Django//lib/python/django/utils/text.pyt   recapitalize  s    c           sß   i d d 6d d 6d d 6d d 6d d 6d d 6d	 d
 6d	 d 6d	 d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d d! 6d d" 6‰  d# j  ‡  f d$ †  |  j ƒ  Dƒ ƒ S(%   uA   Converts a phone number with letters into its numeric equivalent.u   2u   au   bu   cu   3u   du   eu   fu   4u   gu   hu   iu   5u   ju   ku   lu   6u   mu   nu   ou   7u   pu   qu   ru   su   8u   tu   uu   vu   9u   wu   xu   yu   zu    c         3  s!   |  ] } ˆ  j  | | ƒ Vq d  S(   N(   t   get(   t   .0t   c(   t   char2number(    s*   ../Django//lib/python/django/utils/text.pys	   <genexpr>  s    (   R   R?   (   t   phone(    (   Rj   s*   ../Django//lib/python/django/utils/text.pyt   phone2numeric  s
    -11-c         C  sE   t  ƒ  } t d d d d d | ƒ } | j |  ƒ | j ƒ  | j ƒ  S(   Nt   modeu   wbt   compressleveli   t   fileobj(   R   R   t   writet   closet   getvalue(   RU   t   zbuft   zfile(    (    s*   ../Django//lib/python/django/utils/text.pyt   compress_string  s
    	
t   StreamingBufferc           B  s5   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C  s   g  |  _  d  S(   N(   t   vals(   R!   (    (    s*   ../Django//lib/python/django/utils/text.pyR    $  s    c         C  s   |  j  j | ƒ d  S(   N(   Rw   t   append(   R!   t   val(    (    s*   ../Django//lib/python/django/utils/text.pyRp   '  s    c         C  s   d j  |  j ƒ } g  |  _ | S(   Nt    (   R   Rw   (   R!   t   ret(    (    s*   ../Django//lib/python/django/utils/text.pyt   read*  s    	c         C  s   d  S(   N(    (   R!   (    (    s*   ../Django//lib/python/django/utils/text.pyt   flush/  s    c         C  s   d  S(   N(    (   R!   (    (    s*   ../Django//lib/python/django/utils/text.pyRq   2  s    (   RM   RN   R    Rp   R|   R}   Rq   (    (    (    s*   ../Django//lib/python/django/utils/text.pyRv   #  s
   				c         c  s{   t  ƒ  } t d d d d d | ƒ } | j ƒ  Vx0 |  D]( } | j | ƒ | j ƒ  | j ƒ  Vq6 W| j ƒ  | j ƒ  Vd  S(   NRm   u   wbRn   i   Ro   (   Rv   R   R|   Rp   R}   Rq   (   t   sequencet   bufRt   t   item(    (    s*   ../Django//lib/python/django/utils/text.pyt   compress_sequence6  s    	

u
   ([Â€-ï¿¿])c         C  sÜ   d „  } t  |  ƒ t k r- |  j d ƒ }  n$ t  |  ƒ t j k rQ t |  ƒ ‚ n  |  j d d ƒ }  |  j d d ƒ }  |  j d d ƒ }  |  j d	 d
 ƒ }  |  j d d ƒ }  | rÆ |  j d d ƒ }  n  t t j	 | |  ƒ ƒ S(   Nc         S  s   d t  |  j d ƒ ƒ S(   Nu   \u%04xi   (   t   ordR;   (   R=   (    (    s*   ../Django//lib/python/django/utils/text.pyt   fixF  s    u   utf-8u   \u   \\u   u   \ru   
u   \nu   	u   \tu   'u   \'u   "u   &quot;(
   t   typet   bytest   decodeR   t	   text_typet	   TypeErrorR[   t   strt
   ustring_reR]   (   RU   t   quote_double_quotesRƒ   (    (    s*   ../Django//lib/python/django/utils/text.pyt   javascript_quoteD  s    	uŠ   
    ((?:
        [^\s'"]*
        (?:
            (?:"(?:[^"\\]|\\.)*" | '(?:[^'\\]|\\.)*')
            [^\s'"]*
        )+
    ) | \S+)
c         c  s8   t  |  ƒ }  x% t j |  ƒ D] } | j d ƒ Vq Wd S(   u£  
    Generator that splits a string by spaces, leaving quoted phrases together.
    Supports both single and double quotes, and supports escaping quotes with
    backslashes. In the output, strings will keep their initial and trailing
    quote marks and escaped quotes will remain escaped (the results can then
    be further processed with unescape_string_literal()).

    >>> list(smart_split(r'This is "a person\'s" test.'))
    ['This', 'is', '"a person\\\'s"', 'test.']
    >>> list(smart_split(r"Another 'person\'s' test."))
    ['Another', "'person\\'s'", 'test.']
    >>> list(smart_split(r'A "\"funky\" style" test.'))
    ['A', '"\\"funky\\" style"', 'test.']
    i    N(   R   t   smart_split_ret   finditerR;   (   R   t   bit(    (    s*   ../Django//lib/python/django/utils/text.pyt   smart_splitc  s    c         C  sÇ   |  j  d ƒ } | d d k rŠ | d } y@ | d d k rR t | d d ƒ } n t | ƒ } t | ƒ SWqÃ t k
 r† |  j  d ƒ SXn9 y t t j | ƒ SWn! t t f k
 rÂ |  j  d ƒ SXd  S(   Ni   i    u   #u   xXi   (   R;   R&   t   unichrRA   R   t   name2codepointt   KeyError(   R=   R   Ri   (    (    s*   ../Django//lib/python/django/utils/text.pyt   _replace_entityw  s    
u#   &(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));c         C  s   t  j t |  ƒ S(   N(   t
   _entity_reR]   R”   (   R   (    (    s*   ../Django//lib/python/django/utils/text.pyt   unescape_entities‹  s    c         C  sh   |  d d k s$ |  d |  d k r7 t  d |  ƒ ‚ n  |  d } |  d d !j d | | ƒ j d d ƒ S(	   ul  
    Convert quoted string literals to unquoted strings with escaped quotes and
    backslashes unquoted::

        >>> unescape_string_literal('"abc"')
        'abc'
        >>> unescape_string_literal("'abc'")
        'abc'
        >>> unescape_string_literal('"a \"bc\""')
        'a "bc"'
        >>> unescape_string_literal("'\'ab\' c'")
        "'ab' c"
    i    u   "'iÿÿÿÿu   Not a string literal: %ri   u   \%su   \\u   \(   RA   R[   (   RU   t   quote(    (    s*   ../Django//lib/python/django/utils/text.pyt   unescape_string_literal  s    $
c         C  sa   t  j d |  ƒ j d d ƒ j d ƒ }  t j d d |  ƒ j ƒ  j ƒ  }  t t j d d |  ƒ ƒ S(   u±   
    Converts to lowercase, removes non-word characters (alphanumerics and
    underscores) and converts spaces to hyphens. Also strips leading and
    trailing whitespace.
    u   NFKDu   asciiu   ignoreu   [^\w\s-]u    u   [-\s]+u   -(	   R'   R(   t   encodeR†   R\   R]   RZ   R?   R   (   t   value(    (    s*   ../Django//lib/python/django/utils/text.pyt   slugify£  s    '!(:   t
   __future__R    R\   R'   RR   t   gzipR   t   ioR   t   django.utils.encodingR   t   django.utils.functionalR   R   t   django.utilsR   t   django.utils.six.movesR   t   django.utils.translationR   R	   R_   R
   t   django.utils.safestringR   t   PY3R   t   capfirstR‡   Rd   t   Ut   SR8   R<   R   R   RW   RY   R^   Rb   Rc   Rf   Rl   Ru   t   objectRv   R   RŠ   RP   RŒ   t   VERBOSER   R   R”   R•   R–   R˜   R›   (    (    (    s*   ../Django//lib/python/django/utils/text.pyt   <module>   sh   			™												