Ticket #3963: truncate_optional_replacement.patch
File truncate_optional_replacement.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 to the truncated text 152 153 """ 154 155 if not ',' in arg: 156 arg += ',' 157 bits = arg.split(',') 158 if len(bits) > 2: 159 return value # Fail silently if comma is used as replacement. 160 153 161 from django.utils.text import truncate_words 154 162 try: 155 length = int( arg)163 length = int(bits[0]) 156 164 except ValueError: # invalid literal for int() 157 165 return value # Fail silently. 158 166 if not isinstance(value, basestring): 159 167 value = str(value) 160 return truncate_words(value, length) 168 if bits[1]: 169 return truncate_words(value, length, bits[1]) 170 else: 171 return truncate_words(value, length) 161 172 truncatewords = stringfilter(truncatewords) 162 173 163 174 def truncatewords_html(value, arg): 164 175 """ 165 176 Truncates HTML after a certain number of words 166 177 167 Argument: Number of words to truncate after 178 Argument: Number of words to truncate after, and an optional replacement 179 adding to the truncated text 168 180 """ 181 182 if not ',' in arg: 183 arg += ',' 184 bits = arg.split(',') 185 if len(bits) > 2: 186 return value # Fail silently if comma is used as replacement. 187 169 188 from django.utils.text import truncate_html_words 170 189 try: 171 length = int( arg)190 length = int(bits[0]) 172 191 except ValueError: # invalid literal for int() 173 192 return value # Fail silently. 174 193 if not isinstance(value, basestring): 175 194 value = str(value) 176 return truncate_html_words(value, length) 195 196 if bits[1]: 197 return truncate_html_words(value, length, bits[1]) 198 else: 199 return truncate_html_words(value, length) 177 200 truncatewords_html = stringfilter(truncatewords_html) 178 201 179 202 def upper(value):