Django

Code

Ticket #4657: urlize_trunc.2.patch

File urlize_trunc.2.patch, 2.6 kB (added by SmileyChris, 1 year ago)

with tests this time

  • django/utils/html.py

    old new  
    5757    www. links. Links can have trailing punctuation (periods, commas, close-parens) 
    5858    and leading punctuation (opening parens) and it'll still do the right thing. 
    5959 
    60     If trim_url_limit is not None, the URLs in link text will be limited to 
    61     trim_url_limit characters. 
     60    If trim_url_limit is not None, the URLs in link text longer than this limit 
     61    will truncated to trim_url_limit-3 characters and appended with an elipsis. 
    6262 
    6363    If nofollow is True, the URLs in link text will get a rel="nofollow" attribute. 
    6464    """ 
    65     trim_url = lambda x, limit=trim_url_limit: limit is not None and (x[:limit] + (len(x) >=limit and '...' or '')) or x 
     65    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 
    6666    words = word_split_re.split(text) 
    6767    nofollow_attr = nofollow and ' rel="nofollow"' or '' 
    6868    for i, word in enumerate(words): 
  • docs/templates.txt

    old new  
    12661266urlizetrunc 
    12671267~~~~~~~~~~~ 
    12681268 
    1269 Converts URLs into clickable links, truncating URLs to the given character limit. 
     1269Converts URLs into clickable links, truncating URLs longer than the given 
     1270character limit. 
    12701271 
    12711272**Argument:** Length to truncate URLs to 
    12721273 
  • tests/regressiontests/defaultfilters/tests.py

    old new  
    121121'<a href="http://short.com/" rel="nofollow">http://short.com/</a>' 
    122122 
    123123>>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) 
    124 '<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google.co...</a>' 
     124'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>' 
    125125 
     126# Check truncating of URIs which are the exact length 
     127>>> uri = 'http://31characteruri.com/test/' 
     128>>> len(uri) 
     12931 
     130>>> urlizetrunc(uri, 31) 
     131'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/test/</a>' 
     132>>> urlizetrunc(uri, 30) 
     133'<a href="http://31characteruri.com/test/" rel="nofollow">http://31characteruri.com/t...</a>' 
     134 
    126135>>> wordcount('') 
    1271360 
    128137