Ticket #6799: ellipsis-330.diff

File ellipsis-330.diff, 3.5 KB (added by adamfast, 15 years ago)

Patch will apply cleanly to trunk and includes a small change to the original patch to address test suite failures.

  • django/utils/text.py

     
    3737    return u''.join(_generator())
    3838wrap = allow_lazy(wrap, unicode)
    3939
    40 def truncate_words(s, num):
    41     "Truncates a string after a certain number of words."
     40def truncate_words(s, num, end_text='...'):
     41    """Truncates a string after a certain number of words. Takes an optional argument of what
     42    should be used to notify that the string has been truncated, defaults to ellipsis (...)"""
    4243    s = force_unicode(s)
    4344    length = int(num)
    4445    words = s.split()
    4546    if len(words) > length:
    4647        words = words[:length]
    47         if not words[-1].endswith('...'):
    48             words.append('...')
     48        if not words[-1].endswith(end_text):
     49            words.append(end_text)
    4950    return u' '.join(words)
    5051truncate_words = allow_lazy(truncate_words, unicode)
    5152
    52 def truncate_html_words(s, num):
     53def truncate_html_words(s, num, end_text='...'):
    5354    """
    5455    Truncates html to a certain number of words (not counting tags and
    5556    comments). Closes opened tags if they were correctly closed in the given
    56     html.
     57    html. Takes an optional argument of what should be used to notify that
     58    the string has been truncated, defaults to ellipsis (...)
    5759    """
    5860    s = force_unicode(s)
    5961    length = int(num)
     
    104106    if words <= length:
    105107        # Don't try to close tags if we don't need to truncate
    106108        return s
    107     out = s[:ellipsis_pos] + ' ...'
     109    out = s[:ellipsis_pos] + ' ' + end_text
    108110    # Close any tags still open
    109111    for tag in open_tags:
    110112        out += '</%s>' % tag
  • tests/regressiontests/utils/tests.py

     
    99import timesince
    1010import datastructures
    1111import itercompat
     12import text
    1213from decorators import DecoratorFromMiddlewareTests
    1314
    1415# We need this because "datastructures" uses sorted() and the tests are run in
     
    2324    'timesince': timesince,
    2425    'datastructures': datastructures,
    2526    'itercompat': itercompat,
     27    'text': text,
    2628}
    2729
    2830class TestUtilsHtml(TestCase):
  • tests/regressiontests/utils/text.py

     
     1"""
     2### Tests for the utils text library
     3
     4>>> from django.utils.text import *
     5
     6# Check the truncate_words function
     7>>> truncate_words(u'The quick brown fox jumped over the lazy dog.', 10)
     8u'The quick brown fox jumped over the lazy dog....'
     9
     10>>> truncate_words('The quick brown fox jumped over the lazy dog.', 4)
     11u'The quick brown fox ...'
     12
     13>>> truncate_words('The quick brown fox jumped over the lazy dog.', 4, '....')
     14u'The quick brown fox ....'
     15
     16>>> truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 10)
     17u'<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>'
     18
     19>>> truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4)
     20u'<p><strong><em>The quick brown fox ...</em></strong></p>'
     21
     22>>> truncate_html_words('<p><strong><em>The quick brown fox jumped over the lazy dog.</em></strong></p>', 4, '....')
     23u'<p><strong><em>The quick brown fox ....</em></strong></p>'
     24
     25"""
Back to Top