Ticket #6799: 6799.r11858.diff
File 6799.r11858.diff, 4.9 KB (added by , 15 years ago) |
---|
-
django/utils/text.py
diff --git a/django/utils/text.py b/django/utils/text.py index fe46e26..6138041 100644
a b 1 1 import re 2 from django.conf import settings3 2 from django.utils.encoding import force_unicode 4 3 from django.utils.functional import allow_lazy 5 4 from django.utils.translation import ugettext_lazy … … def wrap(text, width): 37 36 return u''.join(_generator()) 38 37 wrap = allow_lazy(wrap, unicode) 39 38 40 def truncate_words(s, num): 41 "Truncates a string after a certain number of words." 39 def truncate_words(s, num, end_text='...'): 40 """Truncates a string after a certain number of words. Takes an optional 41 argument of what should be used to notify that the string has been 42 truncated, defaults to ellipsis (...)""" 42 43 s = force_unicode(s) 43 44 length = int(num) 44 45 words = s.split() 45 46 if len(words) > length: 46 47 words = words[:length] 47 if not words[-1].endswith( '...'):48 words.append( '...')48 if not words[-1].endswith(end_text): 49 words.append(end_text) 49 50 return u' '.join(words) 50 51 truncate_words = allow_lazy(truncate_words, unicode) 51 52 52 def truncate_html_words(s, num): 53 """ 54 Truncates html to a certain number of words (not counting tags and 53 def truncate_html_words(s, num, end_text='...'): 54 """Truncates html to a certain number of words (not counting tags and 55 55 comments). Closes opened tags if they were correctly closed in the given 56 html. 57 """56 html. Takes an optional argument of what should be used to notify that the 57 string has been truncated, defaults to ellipsis (...).""" 58 58 s = force_unicode(s) 59 59 length = int(num) 60 60 if length <= 0: … … def truncate_html_words(s, num): 65 65 re_tag = re.compile(r'<(/)?([^ ]+?)(?: (/)| .*?)?>') 66 66 # Count non-HTML words and keep note of open tags 67 67 pos = 0 68 e llipsis_pos = 068 end_text_pos = 0 69 69 words = 0 70 70 open_tags = [] 71 71 while words <= length: … … def truncate_html_words(s, num): 78 78 # It's an actual non-HTML word 79 79 words += 1 80 80 if words == length: 81 e llipsis_pos = pos81 end_text_pos = pos 82 82 continue 83 83 # Check for tag 84 84 tag = re_tag.match(m.group(0)) 85 if not tag or e llipsis_pos:85 if not tag or end_text_pos: 86 86 # Don't worry about non tags or tags after our truncate point 87 87 continue 88 88 closing_tag, tagname, self_closing = tag.groups() … … def truncate_html_words(s, num): 104 104 if words <= length: 105 105 # Don't try to close tags if we don't need to truncate 106 106 return s 107 out = s[:ellipsis_pos] + ' ...' 107 out = s[:end_text_pos] 108 if end_text: 109 out += ' ' + end_text 108 110 # Close any tags still open 109 111 for tag in open_tags: 110 112 out += '</%s>' % tag -
tests/regressiontests/utils/tests.py
diff --git a/tests/regressiontests/utils/tests.py b/tests/regressiontests/utils/tests.py index a7a6e4c..46f0b85 100644
a b Tests for django.utils. 4 4 5 5 from unittest import TestCase 6 6 7 from django.utils import html, checksums 7 from django.utils import html, checksums, text 8 8 from django.utils.functional import SimpleLazyObject 9 9 10 10 import timesince … … class TestUtilsSimpleLazyObject(TestCase): 237 237 s3 = copy.deepcopy(s) 238 238 self.assertEqual(s3, complex_object()) 239 239 240 class TestUtilsText(TestCase): 241 242 def test_truncate_words(self): 243 self.assertEqual(u'The quick brown fox jumped over the lazy dog.', 244 text.truncate_words(u'The quick brown fox jumped over the lazy dog.', 10)) 245 self.assertEqual(u'The quick brown fox ...', 246 text.truncate_words('The quick brown fox jumped over the lazy dog.', 4)) 247 self.assertEqual(u'The quick brown fox ....', 248 text.truncate_words('The quick brown fox jumped over the lazy dog.', 4, '....')) 249 self.assertEqual(u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 250 text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10)) 251 self.assertEqual(u'<p><strong><em>The quick brown fox ...</em></strong></p>', 252 text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4)) 253 self.assertEqual(u'<p><strong><em>The quick brown fox ....</em></strong></p>', 254 text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....')) 255 self.assertEqual(u'<p><strong><em>The quick brown fox</em></strong></p>', 256 text.truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, None)) 257 240 258 if __name__ == "__main__": 241 259 import doctest 242 260 doctest.testmod()