
 X,9                 @   s  d  d l  m 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 m Z m Z d  d l m Z m Z d  d	 l m Z d  d
 l m Z m Z m Z e	 j r d  d l
 m Z n  d d   Z e e  Z e j d e j e j B Z  e j d e j e j B Z! e j d e j  Z" e j d  Z# e j d  Z$ e d d    Z% Gd d   d e  Z& e d d    Z' e e d  d d   Z( e d d    Z) e d d    Z* d  d!   Z+ Gd" d#   d# e,  Z- d$ d%   Z. e j d& e j/  Z0 d' d(   Z1 d) d*   Z2 e j d+  Z3 e d, d-    Z4 e d. d/    Z5 e e	 j6 e  d0 d1 d2   Z7 d3 d4   Z8 d S)5    )unicode_literalsN)GzipFile)BytesIO)six)
force_text)SimpleLazyObject	keep_lazykeep_lazy_text)SafeText	mark_safe)html_entities)pgettextugettextugettext_lazy)force_unicodec             C   s.   |  o- t  |   d j   t  |   d d   S)Nr      )r   upper)x r   ?/home/ubuntu/projects/ifolica/build/django/django/utils/text.pycapfirst   s    r   z<.*?>|((?:\w[-\w]*|&.*?;)+)z	<.*?>|(.)z<(/)?([^ ]+?)(?:(\s*/)| .*?)?>z\r\n|\rz&(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))c                s1   t          f d d   } d j |    S)aJ  
    A word-wrap function that preserves existing line breaks. Expects that
    existing line breaks are posix newlines.

    All white space is preserved except added line breaks consume the space on
    which they break the line.

    Long words are not wrapped, so the output text may have lines longer than
    ``width``.
    c              3   s  x  j  d  D]}  t |  j d  r2  d p5    } x t |   | k r|  d  | d  j d  d } | d k r |  j d  d } | d k r |  Vd }  Pq n  d |  d  | d  V|  | d   }  t |  j d  r  d p    } qA W|  r |  Vq q Wd  S)NT
r    r    z%s
)
splitlinesminendswithlenrfindfind)lineZ	max_widthspace)textwidthr   r   
_generator0   s    (!,zwrap.<locals>._generatorr   )r   join)r"   r#   r$   r   )r"   r#   r   wrap"   s    r&   c                   s   e  Z d  Z d Z   f d d   Z d d d  Z d d d d	  Z d
 d   Z d d d d  Z d d   Z	 d d   Z
   S)	TruncatorzI
    An object used to truncate text, either by characters or words.
    c                s&   t  t |   j   f d d    d  S)Nc                  s
   t     S)N)r   r   )r"   r   r   <lambda>H   s    z$Truncator.__init__.<locals>.<lambda>)superr'   __init__)selfr"   )	__class__)r"   r   r*   G   s    zTruncator.__init__Nc             C   sf   | d  k r t  d d  } n  t |  } d | k rE | i | d 6S| j |  rX | Sd | | f S)Nz%String to return when truncating textz%(truncated_text)s...z%(truncated_text)sZtruncated_textz%s%s)r   r   r   )r+   r"   truncater   r   r   add_truncation_textJ   s    zTruncator.add_truncation_textFc             C   s   |  j    t |  } t j d |  j  } | } xF |  j d |  D]2 } t j |  sD | d 8} | d k rv Pqv qD qD W| r |  j | | | | d  S|  j | | | |  S)a   
        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 (...).
        ZNFCr   r   r   F)	_setupintunicodedata	normalize_wrappedr.   	combining_truncate_html_text_chars)r+   numr-   htmllengthr"   truncate_lencharr   r   r   charsZ   s    	

zTruncator.charsc       	      C   s   d } d } x t  |  D]x \ } } t j |  r: q n  | d 7} | d k re | | k re | } n  | | k r |  j | d | p d  |  Sq W| S)zE
        Truncates a string after a certain number of chars.
        r   Nr   )	enumerater1   r4   r.   )	r+   r9   r-   r"   r:   Zs_lenZ	end_indexir;   r   r   r   r6   r   s    
	zTruncator._text_charsc             C   sH   |  j    t |  } | r8 |  j | | |  j | d  S|  j | |  S)z
        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 (...).
        T)r/   r0   r5   r3   _text_words)r+   r7   r-   r8   r9   r   r   r   words   s
    
zTruncator.wordsc             C   sW   |  j  j   } t |  | k rJ | d |  } |  j d j |  |  Sd j |  S)zw
        Truncates a string after a certain number of words.

        Newlines in the string will be stripped.
        Nr   )r3   splitr   r.   r%   )r+   r9   r-   r@   r   r   r   r?      s
    zTruncator._text_wordsc             C   s  | r | d k r d Sd } d } d } d }	 g  }
 | r@ t  n t } x+|	 | k rs| j | |  } | sq Pn  | j d  } | j d  r |	 d 7}	 |	 | k rI | } qI qI n  t j | j d   } | sI |	 | k r qI n  | j   \ } } } | j   } | sp| | k rqI | r`y |
 j	 |  } Wn t
 k
 rHYqpX|
 | d d  }
 qI |
 j d |  qI W|	 | k r| S| d |  } |  j d |  } | r| | 7} n  x |
 D] } | d | 7} qW| S)a  
        Truncates HTML to a certain number of chars (not counting tags and
        comments), or, if words is True, then to a certain number of words.
        Closes opened tags if they were correctly closed in the given HTML.

        Newlines in the HTML are preserved.
        r   r   brcollinkbaseimgparamareahrinputr   Nz</%s>)	zbrzcolzlinkzbasezimgzparamzareazhrzinput)re_wordsre_charssearchendgroupre_tagmatchgroupslowerindex
ValueErrorinsertr.   )r+   r9   r-   r"   r:   r@   Zhtml4_singletsposZend_text_posZcurrent_lenZ	open_tagsregexmtagZclosing_tagtagnameZself_closingr>   outZtruncate_textr   r   r   r5      sT     
	zTruncator._truncate_html)__name__
__module____qualname____doc__r*   r.   r<   r6   r@   r?   r5   r   r   )r,   r   r'   C   s   r'   c             C   s1   t  |   j   j d d  }  t j d d |   S)a  
    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'
    r   _z(?u)[^-\w.]r   )r   stripreplaceresub)sr   r   r   get_valid_filename   s    
rg   orc             C   s}   t  |   d k r d St  |   d k r6 t |  d  Sd t d  j d d   |  d d	  D  t |  t |  d
  f S)a   
    >>> 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([])
    ''
    r   r   r   z%s %s %sz, c             s   s   |  ] } t  |  Vq d  S)N)r   ).0r>   r   r   r   	<genexpr>  s    z get_text_list.<locals>.<genexpr>Nrk   )r   r   ra   r%   )Zlist_	last_wordr   r   r   get_text_list   s    )rm   c             C   s   t  |   }  t j d |   S)z+Normalizes CRLF and CR newlines to just LF.r   )r   re_newlinesre   )r"   r   r   r   normalize_newlines  s    ro   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$ d%   |  j   D  S)&zAConverts a phone number with letters into its numeric equivalent.2abc3def4ghr>   5jkl6rY   no7pqrrf   8tuv9wr   yzr   c             3   s!   |  ] }   j  | |  Vq d  S)N)get)ri   rs   )char2numberr   r   rj     s    z phone2numeric.<locals>.<genexpr>)r%   rS   )Zphoner   )r   r   phone2numeric  s    111&r   c             C   sE   t    } t d d d d d |  } | j |   | j   | j   S)Nmodewbcompresslevel   fileobj)r   r   writeclosegetvalue)rf   Zzbufzfiler   r   r   compress_string$  s
    	
r   c               @   sL   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 d
   Z d S)StreamingBufferc             C   s   g  |  _  d  S)N)vals)r+   r   r   r   r*   -  s    zStreamingBuffer.__init__c             C   s   |  j  j |  d  S)N)r   append)r+   valr   r   r   r   0  s    zStreamingBuffer.writec             C   s,   |  j  s d Sd j |  j   } g  |  _  | S)N    )r   r%   )r+   retr   r   r   read3  s
    		zStreamingBuffer.readc             C   s   d  S)Nr   )r+   r   r   r   flush:  s    zStreamingBuffer.flushc             C   s   d  S)Nr   )r+   r   r   r   r   =  s    zStreamingBuffer.closeN)r]   r^   r_   r*   r   r   r   r   r   r   r   r   r   ,  s
   r   c             c   s   t    } t d d d d d |  } | j   Vx5 |  D]- } | j |  | j   } | r6 | Vq6 q6 W| j   | j   Vd  S)Nr   r   r   r   r   )r   r   r   r   r   )sequencebufr   itemdatar   r   r   compress_sequenceB  s    	
r   z
    ((?:
        [^\s'"]*
        (?:
            (?:"(?:[^"\\]|\\.)*" | '(?:[^'\\]|\\.)*')
            [^\s'"]*
        )+
    ) | \S+)
c             c   s8   t  |   }  x% t j |   D] } | j d  Vq Wd S)a  
    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.']
    r   N)r   smart_split_refinditerrO   )r"   bitr   r   r   smart_split]  s    r   c             C   s   |  j  d  } | d d k r | d d   } yI | d d k r^ t | d d   d  } n t |  } t j |  SWq t k
 r |  j  d  SYq Xn@ y t j t j |  SWn% t t f k
 r |  j  d  SYn Xd  S)Nr   r   #xX   )rO   r0   r   unichrrU   r   name2codepointKeyError)rQ   r"   rs   r   r   r   _replace_entityq  s    r   z#&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));c             C   s   t  j t t |    S)N)
_entity_rere   r   r   )r"   r   r   r   unescape_entities  s    r   c             C   sk   |  d d k s$ |  d |  d k r7 t  d |    n  |  d } |  d d	  j d | |  j d d  S)
al  
    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"
    r   z"'r   zNot a string literal: %rz\%sz\\\rk   rk   )rU   rc   )rf   quoter   r   r   unescape_string_literal  s    $
r   Fc             C   s   t  |   }  | rp t j d |   }  t j d d |  d t j j   j   }  t t j d d |  d t j  St j d |   j	 d d	  j
 d  }  t j d d |   j   j   }  t t j d d |    S)
z
    Convert to ASCII if 'allow_unicode' is False. Convert spaces to hyphens.
    Remove characters that aren't alphanumerics, underscores, or hyphens.
    Convert to lowercase. Also strip leading and trailing whitespace.
    NFKCz[^\w\s-]r   flagsz[-\s]+-ZNFKDasciiignore)r   r1   r2   rd   re   Urb   rS   r   encodedecode)valueZallow_unicoder   r   r   slugify  s    *"'!r   c             C   s   t  j d |   j   j   S)zg
    Splits CamelCase and converts to lower case. Also strips leading and
    trailing whitespace.
    z \1)re_camel_casere   rb   rS   )r   r   r   r   camel_case_to_spaces  s    r   )9
__future__r   rd   r1   gzipr   ior   Zdjango.utilsr   Zdjango.utils.encodingr   Zdjango.utils.functionalr   r   r	   Zdjango.utils.safestringr
   r   Zdjango.utils.six.movesr   Zdjango.utils.translationr   r   ra   r   PY2r   r   compiler   SrK   rL   rP   rn   r   r&   r'   rg   rm   ro   r   r   objectr   r   VERBOSEr   r   r   r   r   r   	text_typer   r   r   r   r   r   <module>   sN   	!