Ticket #3963: truncate_optional_replacement_v2.patch
File truncate_optional_replacement_v2.patch, 3.3 KB (added by , 18 years ago) |
---|
-
django/utils/text.py
31 31 yield word 32 32 return "".join(_generator()) 33 33 34 def truncate_words(s, num ):34 def truncate_words(s, num, replacement='...'): 35 35 "Truncates a string after a certain number of words." 36 36 length = int(num) 37 37 words = s.split() 38 38 if len(words) > length: 39 39 words = words[:length] 40 if not words[-1].endswith( '...'):41 words.append( '...')40 if not words[-1].endswith(replacement): 41 words.append(replacement) 42 42 return ' '.join(words) 43 43 44 def truncate_html_words(s, num ):44 def truncate_html_words(s, num, replacement=' ...'): 45 45 """ 46 46 Truncates html to a certain number of words (not counting tags and comments). 47 47 Closes opened tags if they were correctly closed in the given html. … … 94 94 if words <= length: 95 95 # Don't try to close tags if we don't need to truncate 96 96 return s 97 out = s[:ellipsis_pos] + ' ...'97 out = s[:ellipsis_pos] + replacement 98 98 # Close any tags still open 99 99 for tag in open_tags: 100 100 out += '</%s>' % tag -
django/template/defaultfilters.py
148 148 """ 149 149 Truncates a string after a certain number of words 150 150 151 Argument: Number of words to truncate after 151 Arguments: Number of words to truncate after and an optional replacement 152 adding after the truncated text 152 153 """ 154 155 arg = str(arg) 156 if not ',' in arg: 157 arg += ',' 158 bits = arg.split(',') 159 if len(bits) > 2: 160 return value # Fail silently if comma is used as replacement 161 153 162 from django.utils.text import truncate_words 154 163 try: 155 length = int( arg)164 length = int(bits[0]) 156 165 except ValueError: # invalid literal for int() 157 166 return value # Fail silently. 158 167 if not isinstance(value, basestring): 159 168 value = str(value) 160 return truncate_words(value, length) 169 if bits[1]: 170 return truncate_words(value, length, bits[1]) 171 else: 172 return truncate_words(value, length) 161 173 truncatewords = stringfilter(truncatewords) 162 174 163 175 def truncatewords_html(value, arg): 164 176 """ 165 177 Truncates HTML after a certain number of words 166 178 167 Argument: Number of words to truncate after 179 Arguments: Number of words to truncate after and an optional replacement 180 adding after the truncated text 168 181 """ 182 183 arg = str(arg) 184 if not ',' in arg: 185 arg += ',' 186 bits = arg.split(',') 187 if len(bits) > 2: 188 return value # Fail silently if comma is used as replacement 189 169 190 from django.utils.text import truncate_html_words 170 191 try: 171 length = int( arg)192 length = int(bits[0]) 172 193 except ValueError: # invalid literal for int() 173 194 return value # Fail silently. 174 195 if not isinstance(value, basestring): 175 196 value = str(value) 176 return truncate_html_words(value, length) 197 198 if bits[1]: 199 return truncate_html_words(value, length, bits[1]) 200 else: 201 return truncate_html_words(value, length) 177 202 truncatewords_html = stringfilter(truncatewords_html) 178 203 179 204 def upper(value):