Ticket #6799: ellipsis.diff
File ellipsis.diff, 1.3 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 def truncate_words(s, num ):39 def truncate_words(s, num, end_text='...'): 40 40 "Truncates a string after a certain number of words." 41 41 s = force_unicode(s) 42 42 length = int(num) 43 43 words = s.split() 44 44 if len(words) > length: 45 45 words = words[:length] 46 if not words[-1].endswith( '...'):47 words.append( '...')46 if not words[-1].endswith(end_text): 47 words.append(end_text) 48 48 return u' '.join(words) 49 49 truncate_words = allow_lazy(truncate_words, unicode) 50 50 51 def truncate_html_words(s, num ):51 def truncate_html_words(s, num, end_text='...'): 52 52 """ 53 53 Truncates html to a certain number of words (not counting tags and 54 54 comments). Closes opened tags if they were correctly closed in the given … … 103 103 if words <= length: 104 104 # Don't try to close tags if we don't need to truncate 105 105 return s 106 out = s[:ellipsis_pos] + ' ...'106 out = s[:ellipsis_pos] + end_text 107 107 # Close any tags still open 108 108 for tag in open_tags: 109 109 out += '</%s>' % tag