Ticket #5025: updated-truncatechars-5075.diff
File updated-truncatechars-5075.diff, 4.9 KB (added by , 14 years ago) |
---|
-
django/utils/text.py
1 1 import re 2 import unicodedata 2 3 from django.utils.encoding import force_unicode 3 4 from django.utils.functional import allow_lazy 4 5 from django.utils.translation import ugettext_lazy, ugettext as _ … … 36 37 return u''.join(_generator()) 37 38 wrap = allow_lazy(wrap, unicode) 38 39 40 def truncate_chars(s, num, end_text='...'): 41 """Truncates a string after a certain number of characters. Takes an 42 optional argument of what should be used to notify that the string has been 43 truncated, defaulting to ellipsis (...)""" 44 s = force_unicode(s) 45 length = int(num) 46 47 normalized = unicodedata.normalize('NFC', s) 48 49 # If the text is short enough, we can bail out right now 50 if len(normalized) <= length: 51 return normalized 52 53 translated_end_text = _(end_text) 54 len_end_text = len(translated_end_text) 55 56 end_index = max(length-len_end_text, 0) 57 while unicodedata.combining(normalized[end_index]) and end_index < len(normalized): 58 end_index += 1 59 60 return u'%s%s' % (normalized[:end_index], translated_end_text) 61 truncate_chars = allow_lazy(truncate_chars, unicode) 62 39 63 def truncate_words(s, num, end_text='...'): 40 64 """Truncates a string after a certain number of words. Takes an optional 41 65 argument of what should be used to notify that the string has been -
django/template/defaultfilters.py
251 251 title.is_safe = True 252 252 title = stringfilter(title) 253 253 254 def truncatechars(value, arg): 255 """ 256 Truncates a string after a certain number of characters. 257 258 Argument: Number of characters to truncate after. 259 """ 260 from django.utils.text import truncate_chars 261 try: 262 length = int(arg) 263 except ValueError: # Invalid literal for int(). 264 return value # Fail silently. 265 return truncate_chars(value, length) 266 truncatechars.is_safe = True 267 truncatechars = stringfilter(truncatechars) 268 254 269 def truncatewords(value, arg): 255 270 """ 256 271 Truncates a string after a certain number of words. -
tests/regressiontests/utils/text.py
1 # -*- coding: utf-8 -*- 1 2 import unittest 2 3 3 4 from django.utils import text … … 4 5 5 6 class TestUtilsText(unittest.TestCase): 6 7 8 def test_truncate_chars(self): 9 self.assertEqual(u'The quick brown fox jumped over the lazy dog.', 10 text.truncate_chars(u'The quick brown fox jumped over the lazy dog.', 100)) 11 self.assertEqual(u'The quick brown fox ...', 12 text.truncate_chars('The quick brown fox jumped over the lazy dog.', 23)) 13 self.assertEqual(u'The quick brown fox ....', 14 text.truncate_chars('The quick brown fox jumped over the lazy dog.', 24, '....')) 15 16 # Ensure that we normalize our unicode data first 17 nfc = u'o\xfco\xfco\xfco\xfc' 18 nfd = u'ou\u0308ou\u0308ou\u0308ou\u0308' 19 self.assertEqual(u'oüoüoüoü', text.truncate_chars(nfc, 8)) 20 self.assertEqual(u'oüoüoüoü', text.truncate_chars(nfd, 8)) 21 self.assertEqual(u'oü...', text.truncate_chars(nfc, 5)) 22 self.assertEqual(u'oü...', text.truncate_chars(nfd, 5)) 23 24 # We shouldn't split combining characters up 25 self.assertEqual(u'__B\u030A...', 26 text.truncate_chars(u'__B\u030A____', 6)) 27 28 # Make a best effort to shorten to the desired length, but requesting 29 # a length shorter than the ellipsis shouldn't break 30 self.assertEqual(u'...', text.truncate_chars(u'asdf', 1)) 31 7 32 def test_truncate_words(self): 8 33 self.assertEqual(u'The quick brown fox jumped over the lazy dog.', 9 34 text.truncate_words(u'The quick brown fox jumped over the lazy dog.', 10)) -
docs/ref/templates/builtins.txt
1989 1989 1990 1990 If ``value`` is ``"my first post"``, the output will be ``"My First Post"``. 1991 1991 1992 .. templatefilter:: truncatechars 1993 1994 truncatechars 1995 ~~~~~~~~~~~~~ 1996 1997 Truncates a string after a certain number of characters. 1998 1999 **Argument:** Number of characters to truncate after 2000 2001 For example:: 2002 2003 {{ value|truncatechars:11 }} 2004 2005 If ``value`` is ``"Joel is a slug"``, the output will be ``"Joel is ..."``. 2006 1992 2007 .. templatefilter:: truncatewords 1993 2008 1994 2009 truncatewords