Ticket #5025: truncate.patch
File truncate.patch, 4.3 KB (added by , 17 years ago) |
---|
-
django/template/defaultfilters.py
144 144 return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title()) 145 145 title = stringfilter(title) 146 146 147 def truncate(value, limit): 148 """ 149 Truncates strings longer than limit to limit-3 characters, appending an 150 elipsis. 151 152 Argument: Number of characters to truncate to 153 """ 154 from django.utils.text import truncate 155 try: 156 limit = int(limit) 157 except ValueError: # invalid literal for int() 158 return value # Fail silently. 159 return truncate(value, limit) 160 truncate = stringfilter(truncate) 161 147 162 def truncatewords(value, arg): 148 163 """ 149 164 Truncates a string after a certain number of words … … 600 615 register.filter(timesince) 601 616 register.filter(timeuntil) 602 617 register.filter(title) 618 register.filter(truncate) 603 619 register.filter(truncatewords) 604 620 register.filter(truncatewords_html) 605 621 register.filter(unordered_list) -
django/utils/html.py
5 5 6 6 from django.utils.encoding import force_unicode 7 7 from django.utils.functional import allow_lazy 8 from django.utils.text import truncate 8 9 9 10 # Configuration for urlize() function 10 11 LEADING_PUNCTUATION = ['(', '<', '<'] … … 72 73 If nofollow is True, the URLs in link text will get a rel="nofollow" 73 74 attribute. 74 75 """ 75 trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x76 trim_url = lambda x, limit=trim_url_limit: truncate(x, limit) 76 77 words = word_split_re.split(force_unicode(text)) 77 78 nofollow_attr = nofollow and ' rel="nofollow"' or '' 78 79 for i, word in enumerate(words): -
django/utils/text.py
35 35 return u''.join(_generator()) 36 36 wrap = allow_lazy(wrap, unicode) 37 37 38 def truncate(s, limit): 39 """ 40 Truncates strings longer than limit to limit-3 characters, appending an 41 elipsis. At least one character will always be displayed, so the functional 42 minimum limit is 4. 43 """ 44 s = force_unicode(s) 45 if len(s) <= limit: 46 return s 47 return '%s...' % s[:max(1, limit - 3)] 48 truncate = allow_lazy(truncate, unicode) 49 38 50 def truncate_words(s, num): 39 51 "Truncates a string after a certain number of words." 40 52 s = force_unicode(s) -
docs/templates.txt
1272 1272 1273 1273 Converts a string into titlecase. 1274 1274 1275 truncate 1276 ~~~~~~~~ 1277 1278 Ensures a string is no longer than the given character limit, truncating to 1279 limit-3 characters and appending an elipsis for strings which exceed this limit. 1280 For example, if ``email`` contains ``'info@example.com'``, then 1281 ``{{ email|truncate:10 }}`` will return "``info@ex...``". 1282 1283 **Argument:** Length to truncate string to 1284 1275 1285 truncatewords 1276 1286 ~~~~~~~~~~~~~ 1277 1287 1278 Truncates a string after a certain number of words. 1288 Truncates a string to a certain number of words, also appending an elipsis to 1289 strings which exceed this number of words. 1279 1290 1280 1291 **Argument:** Number of words to truncate after 1281 1292 -
tests/regressiontests/defaultfilters/tests.py
82 82 >>> title(u'discoth\xe8que') 83 83 u'Discoth\xe8que' 84 84 85 >>> truncate(u'A sentence with a few words in it', 2) 86 u'A...' 87 88 >>> truncate(u'A sentence with a few words in it', 6) 89 u'A s...' 90 91 >>> truncate(u'A sentence with a few words in it', 10) 92 u'A sente...' 93 94 >>> truncate(u'A sentence with a few words in it', 100) 95 u'A sentence with a few words in it' 96 97 >>> truncate(u'A sentence with a few words in it', 'not a number') 98 u'A sentence with a few words in it' 99 85 100 >>> truncatewords(u'A sentence with a few words in it', 1) 86 101 u'A ...' 87 102