Ticket #6799: ellipsis-330.2.diff
File ellipsis-330.2.diff, 3.5 KB (added by , 16 years ago) |
---|
-
django/utils/text.py
37 37 return u''.join(_generator()) 38 38 wrap = allow_lazy(wrap, unicode) 39 39 40 def truncate_words(s, num): 41 "Truncates a string after a certain number of words." 40 def truncate_words(s, num, end_text='...'): 41 """Truncates a string after a certain number of words. Takes an optional argument of what 42 should be used to notify that the string has been 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 def truncate_html_words(s, num, end_text='...'): 53 54 """ 54 55 Truncates html to a certain number of words (not counting tags and 55 56 comments). Closes opened tags if they were correctly closed in the given 56 html. 57 html. Takes an optional argument of what should be used to notify that 58 the string has been truncated, defaults to ellipsis (...) 57 59 """ 58 60 s = force_unicode(s) 59 61 length = int(num) … … 104 106 if words <= length: 105 107 # Don't try to close tags if we don't need to truncate 106 108 return s 107 out = s[:ellipsis_pos] + ' ...'109 out = s[:ellipsis_pos] + ' ' + 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
9 9 import timesince 10 10 import datastructures 11 11 import itercompat 12 import text 12 13 from decorators import DecoratorFromMiddlewareTests 13 14 14 15 # We need this because "datastructures" uses sorted() and the tests are run in … … 23 24 'timesince': timesince, 24 25 'datastructures': datastructures, 25 26 'itercompat': itercompat, 27 'text': text, 26 28 } 27 29 28 30 class TestUtilsHtml(TestCase): -
tests/regressiontests/utils/text.py
1 """ 2 ### Tests for the utils text library 3 4 >>> from django.utils.text import * 5 6 # Check the truncate_words function 7 >>> truncate_words(u'The quick brown fox jumped over the lazy dog.', 10) 8 u'The quick brown fox jumped over the lazy dog....' 9 10 >>> truncate_words('The quick brown fox jumped over the lazy dog.', 4) 11 u'The quick brown fox ...' 12 13 >>> truncate_words('The quick brown fox jumped over the lazy dog.', 4, '....') 14 u'The quick brown fox ....' 15 16 >>> truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10) 17 u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>' 18 19 >>> truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4) 20 u'<p><strong><em>The quick brown fox ...</em></strong></p>' 21 22 >>> truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....') 23 u'<p><strong><em>The quick brown fox ....</em></strong></p>' 24 25 """