Ticket #1919: truncate_words_faster.diff
File truncate_words_faster.diff, 2.0 KB (added by , 17 years ago) |
---|
-
django/utils/text.py
36 36 return u''.join(_generator()) 37 37 wrap = allow_lazy(wrap, unicode) 38 38 39 word_re = re.compile(r'\S+') 39 40 def truncate_words(s, num): 40 41 "Truncates a string after a certain number of words." 41 42 s = force_unicode(s) 42 43 length = int(num) 43 words = s.split() 44 if len(words) > length: 45 words = words[:length] 46 if not words[-1].endswith('...'): 47 words.append('...') 48 return u' '.join(words) 44 if length < 1: 45 return u'' 46 words = 0 47 for match in word_re.finditer(s): 48 words += 1 49 if words == length: 50 end = match.end() 51 if len(s) > end: 52 s = s[:end] 53 if not s.endswith('...'): 54 s += ' ...' 55 break 56 return s 49 57 truncate_words = allow_lazy(truncate_words, unicode) 50 58 51 59 def truncate_html_words(s, num): -
tests/regressiontests/defaultfilters/tests.py
98 98 >>> truncatewords(u'A sentence with a few words in it', 'not a number') 99 99 u'A sentence with a few words in it' 100 100 101 >>> truncatewords(u'Double-spaced sentence with a few words', 2) 102 u'Double-spaced sentence ...' 103 104 >>> truncatewords(u' Two leading spaces for this sentence', 3) 105 u' Two leading spaces ...' 106 107 >>> truncatewords(u' Text with leading and trailing whitespace ', 10) 108 u' Text with leading and trailing whitespace ' 109 110 >>> truncatewords(u' Text with leading and trailing whitespace ', 6) 111 u' Text with leading and trailing whitespace ...' 112 113 >>> truncatewords(u' Text with leading and trailing whitespace ', 1) 114 u' Text ...' 115 101 116 >>> truncatewords_html(u'<p>one <a href="#">two - three <br>four</a> five</p>', 0) 102 117 u'' 103 118