Ticket #6799: ellipsis.diff

File ellipsis.diff, 1.3 KB (added by anonymous, 16 years ago)
  • django/utils/text.py

     
    3636    return u''.join(_generator())
    3737wrap = allow_lazy(wrap, unicode)
    3838
    39 def truncate_words(s, num):
     39def truncate_words(s, num, end_text='...'):
    4040    "Truncates a string after a certain number of words."
    4141    s = force_unicode(s)
    4242    length = int(num)
    4343    words = s.split()
    4444    if len(words) > length:
    4545        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)
    4848    return u' '.join(words)
    4949truncate_words = allow_lazy(truncate_words, unicode)
    5050
    51 def truncate_html_words(s, num):
     51def truncate_html_words(s, num, end_text='...'):
    5252    """
    5353    Truncates html to a certain number of words (not counting tags and
    5454    comments). Closes opened tags if they were correctly closed in the given
     
    103103    if words <= length:
    104104        # Don't try to close tags if we don't need to truncate
    105105        return s
    106     out = s[:ellipsis_pos] + ' ...'
     106    out = s[:ellipsis_pos] + end_text
    107107    # Close any tags still open
    108108    for tag in open_tags:
    109109        out += '</%s>' % tag
Back to Top