Ticket #1919: truncate_words_preserving_whites.diff
File truncate_words_preserving_whites.diff, 962 bytes (added by , 17 years ago) |
---|
-
django/utils/text.py
35 35 return u''.join(_generator()) 36 36 wrap = allow_lazy(wrap, unicode) 37 37 38 word_re = re.compile(r'((?:\*|_)*\w+(?:\*|_)*)',re.UNICODE) 38 39 def truncate_words(s, num): 39 "Truncates a string after a certain number of words ."40 "Truncates a string after a certain number of words, preserving whites." 40 41 s = force_unicode(s) 41 42 length = int(num) 42 words = s.split()43 if len(words) > length:44 words = words[: length]43 words = word_re.split(s,length) 44 if len(words) > (2*length): 45 words = words[:-1] 45 46 if not words[-1].endswith('...'): 46 47 words.append('...') 47 return u' 48 return u''.join(words) 48 49 truncate_words = allow_lazy(truncate_words, unicode) 49 50 50 51 def truncate_html_words(s, num):