Ticket #3963: truncate_optional_replacement.patch

File truncate_optional_replacement.patch, 3.3 KB (added by bugs@…, 17 years ago)
  • 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 to the truncated text
    152153    """
     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
    153161    from django.utils.text import truncate_words
    154162    try:
    155         length = int(arg)
     163        length = int(bits[0])
    156164    except ValueError: # invalid literal for int()
    157165        return value # Fail silently.
    158166    if not isinstance(value, basestring):
    159167        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)
    161172truncatewords = stringfilter(truncatewords)
    162173
    163174def truncatewords_html(value, arg):
    164175    """
    165176    Truncates HTML after a certain number of words
    166177
    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
    168180    """
     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
    169188    from django.utils.text import truncate_html_words
    170189    try:
    171         length = int(arg)
     190        length = int(bits[0])
    172191    except ValueError: # invalid literal for int()
    173192        return value # Fail silently.
    174193    if not isinstance(value, basestring):
    175194        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)
    177200truncatewords_html = stringfilter(truncatewords_html)
    178201
    179202def upper(value):
Back to Top