Ticket #1919: 1919-1.diff
File 1919-1.diff, 2.5 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'((?:\*|_)*\w+(?:\*|_)*)',re.UNICODE) 39 40 def truncate_words(s, num): 40 "Truncates a string after a certain number of words ."41 "Truncates a string after a certain number of words, preserving whites." 41 42 s = force_unicode(s) 42 43 length = int(num) 43 words = s.split()44 if len(words) > length:45 words = words[: length]44 words = word_re.split(s,length) 45 if len(words) > (2*length): 46 words = words[:-1] 46 47 if not words[-1].endswith('...'): 47 words.append(' ...')48 return u' 48 words.append(' ...') 49 return u''.join(words) 49 50 truncate_words = allow_lazy(truncate_words, unicode) 50 51 51 52 def truncate_html_words(s, num): -
tests/regressiontests/templates/tests.py
781 781 'timeuntil04' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=1), 'b':NOW - timedelta(days=2)}, '1 day'), 782 782 'timeuntil05' : ('{{ a|timeuntil:b }}', {'a':NOW - timedelta(days=2), 'b':NOW - timedelta(days=2, minutes=1)}, '1 minute'), 783 783 784 # TRUNCATEWORDS TAG ################################################ 785 # Ensure that newlines are preserved 786 'truncatewords01': ('{{ var|truncatewords:4 }}', 787 {'var': """ 788 Hello, 789 boys. 790 How 791 are 792 you 793 gentlemen. 794 """}, 795 """ 796 Hello, 797 boys. 798 How 799 are ...""" 800 ), 801 784 802 ### URL TAG ######################################################## 785 803 # Successes 786 804 'url01' : ('{% url regressiontests.templates.views.client client.id %}', {'client': {'id': 1}}, '/url_tag/client/1/'),