diff -r 548df236b2cc django/template/defaultfilters.py
a
|
b
|
|
315 | 315 | |
316 | 316 | @register.filter |
317 | 317 | @stringfilter |
318 | | def urlize(value, autoescape=None): |
| 318 | def urlize(value, autoescape=None, additional_tlds=None): |
319 | 319 | """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)) |
321 | 321 | urlize.is_safe = True |
322 | 322 | urlize.needs_autoescape = True |
323 | 323 | |
diff -r 548df236b2cc django/utils/html.py
a
|
b
|
|
100 | 100 | return unencoded_ampersands_re.sub('&', force_unicode(value)) |
101 | 101 | fix_ampersands = allow_lazy(fix_ampersands, unicode) |
102 | 102 | |
103 | | def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): |
| 103 | def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False, additional_tlds=None): |
104 | 104 | """ |
105 | 105 | Converts any URLs in text into clickable links. |
106 | 106 | |
… |
… |
|
116 | 116 | attribute. |
117 | 117 | |
118 | 118 | 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. |
119 | 122 | """ |
120 | 123 | 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 |
121 | 124 | safe_input = isinstance(text, SafeData) |
122 | 125 | words = word_split_re.split(force_unicode(text)) |
123 | 126 | 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 | |
124 | 135 | for i, word in enumerate(words): |
125 | 136 | match = None |
126 | 137 | if '.' in word or '@' in word or ':' in word: |
… |
… |
|
133 | 144 | url = urlquote(middle, safe='/&=:;#?+*') |
134 | 145 | elif middle.startswith('www.') or ('@' not in middle and \ |
135 | 146 | 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)): |
137 | 148 | url = urlquote('http://%s' % middle, safe='/&=:;#?+*') |
138 | 149 | elif '@' in middle and not ':' in middle and simple_email_re.match(middle): |
139 | 150 | url = 'mailto:%s' % middle |
diff -r 548df236b2cc tests/regressiontests/defaultfilters/tests.py
a
|
b
|
|
217 | 217 | self.assertEqual(urlize('https://google.com'), |
218 | 218 | u'<a href="https://google.com" rel="nofollow">https://google.com</a>') |
219 | 219 | |
| 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 | |
220 | 227 | def test_wordcount(self): |
221 | 228 | self.assertEqual(wordcount(''), 0) |
222 | 229 | self.assertEqual(wordcount(u'oneword'), 1) |