Ticket #5025: truncate.patch

File truncate.patch, 4.3 KB (added by Chris Beaven, 17 years ago)
  • django/template/defaultfilters.py

     
    144144    return re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), value.title())
    145145title = stringfilter(title)
    146146
     147def truncate(value, limit):
     148    """
     149    Truncates strings longer than limit to limit-3 characters, appending an
     150    elipsis.
     151   
     152    Argument: Number of characters to truncate to
     153    """
     154    from django.utils.text import truncate
     155    try:
     156        limit = int(limit)
     157    except ValueError: # invalid literal for int()
     158        return value # Fail silently.
     159    return truncate(value, limit)
     160truncate = stringfilter(truncate)
     161
    147162def truncatewords(value, arg):
    148163    """
    149164    Truncates a string after a certain number of words
     
    600615register.filter(timesince)
    601616register.filter(timeuntil)
    602617register.filter(title)
     618register.filter(truncate)
    603619register.filter(truncatewords)
    604620register.filter(truncatewords_html)
    605621register.filter(unordered_list)
  • django/utils/html.py

     
    55
    66from django.utils.encoding import force_unicode
    77from django.utils.functional import allow_lazy
     8from django.utils.text import truncate
    89
    910# Configuration for urlize() function
    1011LEADING_PUNCTUATION  = ['(', '<', '&lt;']
     
    7273    If nofollow is True, the URLs in link text will get a rel="nofollow"
    7374    attribute.
    7475    """
    75     trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x
     76    trim_url = lambda x, limit=trim_url_limit: truncate(x, limit)
    7677    words = word_split_re.split(force_unicode(text))
    7778    nofollow_attr = nofollow and ' rel="nofollow"' or ''
    7879    for i, word in enumerate(words):
  • django/utils/text.py

     
    3535    return u''.join(_generator())
    3636wrap = allow_lazy(wrap, unicode)
    3737
     38def truncate(s, limit):
     39    """
     40    Truncates strings longer than limit to limit-3 characters, appending an
     41    elipsis. At least one character will always be displayed, so the functional
     42    minimum limit is 4.
     43    """
     44    s = force_unicode(s)
     45    if len(s) <= limit:
     46        return s
     47    return '%s...' % s[:max(1, limit - 3)]
     48truncate = allow_lazy(truncate, unicode)
     49
    3850def truncate_words(s, num):
    3951    "Truncates a string after a certain number of words."
    4052    s = force_unicode(s)
  • docs/templates.txt

     
    12721272
    12731273Converts a string into titlecase.
    12741274
     1275truncate
     1276~~~~~~~~
     1277
     1278Ensures a string is no longer than the given character limit, truncating to
     1279limit-3 characters and appending an elipsis for strings which exceed this limit.
     1280For example, if ``email`` contains ``'info@example.com'``, then
     1281``{{ email|truncate:10 }}`` will return "``info@ex...``".
     1282
     1283**Argument:** Length to truncate string to
     1284
    12751285truncatewords
    12761286~~~~~~~~~~~~~
    12771287
    1278 Truncates a string after a certain number of words.
     1288Truncates a string to a certain number of words, also appending an elipsis to
     1289strings which exceed this number of words.
    12791290
    12801291**Argument:** Number of words to truncate after
    12811292
  • tests/regressiontests/defaultfilters/tests.py

     
    8282>>> title(u'discoth\xe8que')
    8383u'Discoth\xe8que'
    8484
     85>>> truncate(u'A sentence with a few words in it', 2)
     86u'A...'
     87
     88>>> truncate(u'A sentence with a few words in it', 6)
     89u'A s...'
     90
     91>>> truncate(u'A sentence with a few words in it', 10)
     92u'A sente...'
     93
     94>>> truncate(u'A sentence with a few words in it', 100)
     95u'A sentence with a few words in it'
     96
     97>>> truncate(u'A sentence with a few words in it', 'not a number')
     98u'A sentence with a few words in it'
     99
    85100>>> truncatewords(u'A sentence with a few words in it', 1)
    86101u'A ...'
    87102
Back to Top