Ticket #1919: truncate_words_preserving_whites.diff

File truncate_words_preserving_whites.diff, 962 bytes (added by didier Belot <dib@…>, 17 years ago)

patch against trunk rev. 5783, simplified

  • django/utils/text.py

     
    3535    return u''.join(_generator())
    3636wrap = allow_lazy(wrap, unicode)
    3737
     38word_re = re.compile(r'((?:\*|_)*\w+(?:\*|_)*)',re.UNICODE)
    3839def truncate_words(s, num):
    39     "Truncates a string after a certain number of words."
     40    "Truncates a string after a certain number of words, preserving whites."
    4041    s = force_unicode(s)
    4142    length = int(num)
    42     words = s.split()
    43     if len(words) > length:
    44         words = words[:length]
     43    words = word_re.split(s,length)
     44    if len(words) > (2*length):
     45        words = words[:-1]
    4546        if not words[-1].endswith('...'):
    4647            words.append('...')
    47     return u' '.join(words)
     48    return u''.join(words)
    4849truncate_words = allow_lazy(truncate_words, unicode)
    4950
    5051def truncate_html_words(s, num):
Back to Top