Ticket #16656: urlize_tlds.diff

File urlize_tlds.diff, 3.5 KB (added by Matt Stevenson <mattoc@…>, 4 years ago)

Patch to add new urlize functionality

  • django/template/defaultfilters.py

    diff -r 548df236b2cc django/template/defaultfilters.py
    a b  
    315315
    316316@register.filter
    317317@stringfilter
    318 def urlize(value, autoescape=None):
     318def urlize(value, autoescape=None, additional_tlds=None):
    319319    """Converts URLs in plain text into clickable links."""
    320     return mark_safe(urlize_impl(value, nofollow=True, autoescape=autoescape))
     320    return mark_safe(urlize_impl(value, nofollow=True, autoescape=autoescape, additional_tlds=additional_tlds))
    321321urlize.is_safe = True
    322322urlize.needs_autoescape = True
    323323
  • django/utils/html.py

    diff -r 548df236b2cc django/utils/html.py
    a b  
    100100    return unencoded_ampersands_re.sub('&amp;', force_unicode(value))
    101101fix_ampersands = allow_lazy(fix_ampersands, unicode)
    102102
    103 def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
     103def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False, additional_tlds=None):
    104104    """
    105105    Converts any URLs in text into clickable links.
    106106
     
    116116    attribute.
    117117
    118118    If autoescape is True, the link text and URLs will get autoescaped.
     119
     120    If additional_tlds is passed then URLs ending with the default or custom
     121    TLD will be converted into an anchor.
    119122    """
    120123    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
    121124    safe_input = isinstance(text, SafeData)
    122125    words = word_split_re.split(force_unicode(text))
    123126    nofollow_attr = nofollow and ' rel="nofollow"' or ''
     127
     128    # The expected behaviour is that the default TLDs will still be urlized
     129    default_tlds = ('.com', '.net', '.org')
     130    if additional_tlds is not None:
     131        tlds = default_tlds + additional_tlds
     132    else:
     133        tlds = default_tlds
     134
    124135    for i, word in enumerate(words):
    125136        match = None
    126137        if '.' in word or '@' in word or ':' in word:
     
    133144                url = urlquote(middle, safe='/&=:;#?+*')
    134145            elif middle.startswith('www.') or ('@' not in middle and \
    135146                    middle and middle[0] in string.ascii_letters + string.digits and \
    136                     (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
     147                    middle.endswith(tlds)):
    137148                url = urlquote('http://%s' % middle, safe='/&=:;#?+*')
    138149            elif '@' in middle and not ':' in middle and simple_email_re.match(middle):
    139150                url = 'mailto:%s' % middle
  • tests/regressiontests/defaultfilters/tests.py

    diff -r 548df236b2cc tests/regressiontests/defaultfilters/tests.py
    a b  
    217217        self.assertEqual(urlize('https://google.com'),
    218218            u'<a href="https://google.com" rel="nofollow">https://google.com</a>')
    219219
     220        # Check with custom TLDs
     221        self.assertEqual(urlize('http://google.com.au/', additional_tlds=('.com.au', '.co.uk')),
     222            u'<a href="http://google.com.au/" rel="nofollow">http://google.com.au/</a>')
     223        self.assertEqual(urlize('google.co.uk', additional_tlds=('.com.au', '.co.uk')),
     224            u'<a href="http://google.co.uk" rel="nofollow">google.co.uk</a>')
     225
     226
    220227    def test_wordcount(self):
    221228        self.assertEqual(wordcount(''), 0)
    222229        self.assertEqual(wordcount(u'oneword'), 1)
Back to Top