Fixed #7542 -- Fixed bug in urlize where it was appending 'http://' to the link text. Thanks for the patch and tests, devin

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7755 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2008-06-26 05:07:13 +00:00
parent 74f0408fa2
commit b7fea94096
2 changed files with 29 additions and 29 deletions

View File

@ -76,20 +76,20 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
""" """
Converts any URLs in text into clickable links. Converts any URLs in text into clickable links.
Works on http://, https://, and www. links. Links can have trailing Works on http://, https://, www. links and links ending in .org, .net or
punctuation (periods, commas, close-parens) and leading punctuation .com. Links can have trailing punctuation (periods, commas, close-parens)
(opening parens) and it'll still do the right thing. and leading punctuation (opening parens) and it'll still do the right
thing.
If trim_url_limit is not None, the URLs in link text longer than this limit If trim_url_limit is not None, the URLs in link text longer than this limit
will truncated to trim_url_limit-3 characters and appended with an elipsis. will truncated to trim_url_limit-3 characters and appended with an elipsis.
If nofollow is True, the URLs in link text will get a rel="nofollow" If nofollow is True, the URLs in link text will get a rel="nofollow"
attribute. attribute.
If autoescape is True, the link text and URLs will get autoescaped.
""" """
if autoescape: 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
trim_url = lambda x, limit=trim_url_limit: conditional_escape(limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x)
else:
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
safe_input = isinstance(text, SafeData) safe_input = isinstance(text, SafeData)
words = word_split_re.split(force_unicode(text)) words = word_split_re.split(force_unicode(text))
nofollow_attr = nofollow and ' rel="nofollow"' or '' nofollow_attr = nofollow and ' rel="nofollow"' or ''
@ -97,30 +97,30 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
match = punctuation_re.match(word) match = punctuation_re.match(word)
if match: if match:
lead, middle, trail = match.groups() lead, middle, trail = match.groups()
if safe_input: # Make URL we want to point to.
middle = mark_safe(middle) url = None
if middle.startswith('www.') or ('@' not in middle and not (middle.startswith('http://') or middle.startswith('https://')) and \
len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \
(middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
middle = 'http://%s' % middle
if middle.startswith('http://') or middle.startswith('https://'): if middle.startswith('http://') or middle.startswith('https://'):
url = urlquote(middle, safe='/&=:;#?+*') url = urlquote(middle, safe='/&=:;#?+*')
if autoescape and not safe_input: elif middle.startswith('www.') or ('@' not in middle and \
url = escape(url) len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \
trimmed_url = trim_url(middle) (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, url = urlquote('http://%s' % middle, safe='/&=:;#?+*')
trimmed_url) elif '@' in middle and not ':' in middle and simple_email_re.match(middle):
elif '@' in middle and not middle.startswith('www.') and \ url = 'mailto:%s' % middle
not ':' in middle and simple_email_re.match(middle): nofollow_attr = ''
if autoescape: # Make link.
middle = conditional_escape(middle) if url:
middle = '<a href="mailto:%s">%s</a>' % (middle, middle) trimmed = trim_url(middle)
if lead + middle + trail != word:
if autoescape and not safe_input: if autoescape and not safe_input:
lead, trail = escape(lead), escape(trail) lead, trail = escape(lead), escape(trail)
url, trimmed = escape(url), escape(trimmed)
middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed)
words[i] = mark_safe('%s%s%s' % (lead, middle, trail)) words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
elif autoescape and not safe_input: else:
words[i] = escape(word) if safe_input:
words[i] = mark_safe(word)
elif autoescape:
words[i] = escape(word)
elif safe_input: elif safe_input:
words[i] = mark_safe(word) words[i] = mark_safe(word)
elif autoescape: elif autoescape:

View File

@ -150,7 +150,7 @@ u'fran%C3%A7ois%20%26%20jill'
u'<a href="http://short.com/" rel="nofollow">http://short.com/</a>' u'<a href="http://short.com/" rel="nofollow">http://short.com/</a>'
>>> urlizetrunc(u'http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) >>> urlizetrunc(u'http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20)
u'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google....</a>' u'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>'
>>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20) >>> urlizetrunc('http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=', 20)
u'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>' u'<a href="http://www.google.co.uk/search?hl=en&q=some+long+url&btnG=Search&meta=" rel="nofollow">http://www.google...</a>'
@ -174,10 +174,10 @@ u'<a href="http://google.com" rel="nofollow">http://google.com</a>'
u'<a href="http://google.com/" rel="nofollow">http://google.com/</a>' u'<a href="http://google.com/" rel="nofollow">http://google.com/</a>'
>>> urlize('www.google.com') >>> urlize('www.google.com')
u'<a href="http://www.google.com" rel="nofollow">http://www.google.com</a>' u'<a href="http://www.google.com" rel="nofollow">www.google.com</a>'
>>> urlize('djangoproject.org') >>> urlize('djangoproject.org')
u'<a href="http://djangoproject.org" rel="nofollow">http://djangoproject.org</a>' u'<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>'
>>> urlize('info@djangoproject.org') >>> urlize('info@djangoproject.org')
u'<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>' u'<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>'