Ticket #3963: truncate_optional_replacement_v2.patch

File truncate_optional_replacement_v2.patch, 3.3 KB (added by Johan Bergström <bugs@…>, 17 years ago)

updated patch which fixes issues mentioned earlier

  • django/utils/text.py

     
    3131            yield word
    3232    return "".join(_generator())
    3333
    34 def truncate_words(s, num):
     34def truncate_words(s, num, replacement='...'):
    3535    "Truncates a string after a certain number of words."
    3636    length = int(num)
    3737    words = s.split()
    3838    if len(words) > length:
    3939        words = words[:length]
    40         if not words[-1].endswith('...'):
    41             words.append('...')
     40        if not words[-1].endswith(replacement):
     41            words.append(replacement)
    4242    return ' '.join(words)
    4343
    44 def truncate_html_words(s, num):
     44def truncate_html_words(s, num, replacement=' ...'):
    4545    """
    4646    Truncates html to a certain number of words (not counting tags and comments).
    4747    Closes opened tags if they were correctly closed in the given html.
     
    9494    if words <= length:
    9595        # Don't try to close tags if we don't need to truncate
    9696        return s
    97     out = s[:ellipsis_pos] + ' ...'
     97    out = s[:ellipsis_pos] + replacement
    9898    # Close any tags still open
    9999    for tag in open_tags:
    100100        out += '</%s>' % tag
  • django/template/defaultfilters.py

     
    148148    """
    149149    Truncates a string after a certain number of words
    150150
    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
    152153    """
     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
    153162    from django.utils.text import truncate_words
    154163    try:
    155         length = int(arg)
     164        length = int(bits[0])
    156165    except ValueError: # invalid literal for int()
    157166        return value # Fail silently.
    158167    if not isinstance(value, basestring):
    159168        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)
    161173truncatewords = stringfilter(truncatewords)
    162174
    163175def truncatewords_html(value, arg):
    164176    """
    165177    Truncates HTML after a certain number of words
    166178
    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
    168181    """
     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
    169190    from django.utils.text import truncate_html_words
    170191    try:
    171         length = int(arg)
     192        length = int(bits[0])
    172193    except ValueError: # invalid literal for int()
    173194        return value # Fail silently.
    174195    if not isinstance(value, basestring):
    175196        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)
    177202truncatewords_html = stringfilter(truncatewords_html)
    178203
    179204def upper(value):
Back to Top