Ticket #7542: simple_urlize_diff.diff

File simple_urlize_diff.diff, 5.1 KB (added by devin, 16 years ago)

fixes urlize

  • tests/regressiontests/defaultfilters/tests.py

     
    174174u'<a href="http://google.com/" rel="nofollow">http://google.com/</a>'
    175175
    176176>>> urlize('www.google.com')
    177 u'<a href="http://www.google.com" rel="nofollow">http://www.google.com</a>'
     177u'<a href="http://www.google.com" rel="nofollow">www.google.com</a>'
    178178
    179179>>> urlize('djangoproject.org')
    180 u'<a href="http://djangoproject.org" rel="nofollow">http://djangoproject.org</a>'
     180u'<a href="http://djangoproject.org" rel="nofollow">djangoproject.org</a>'
    181181
    182182>>> urlize('info@djangoproject.org')
    183183u'<a href="mailto:info@djangoproject.org">info@djangoproject.org</a>'
  • django/utils/html.py

     
    7676    """
    7777    Converts any URLs in text into clickable links.
    7878
    79     Works on http://, https://, and www. links.  Links can have trailing
    80     punctuation (periods, commas, close-parens) and leading punctuation
    81     (opening parens) and it'll still do the right thing.
     79    Works on http://, https://, www. links and links ending in .org, .net, or .com.
     80    Links can have trailing punctuation (periods, commas, close-parens) and
     81    leading punctuation (opening parens) and it'll still do the right thing.
    8282
    8383    If trim_url_limit is not None, the URLs in link text longer than this limit
    8484    will truncated to trim_url_limit-3 characters and appended with an elipsis.
    8585
    8686    If nofollow is True, the URLs in link text will get a rel="nofollow"
    8787    attribute.
     88   
     89    If autoescape is True, the link text and URLs will get autoescaped.
    8890    """
    89     if autoescape:
    90         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)
    91     else:
    92         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
     91    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
    9392    safe_input = isinstance(text, SafeData)
    9493    words = word_split_re.split(force_unicode(text))
    95     nofollow_attr = nofollow and ' rel="nofollow"' or ''
     94    nofollow_attr = ' rel="nofollow"' if nofollow else ''
    9695    for i, word in enumerate(words):
    9796        match = punctuation_re.match(word)
    9897        if match:
    9998            lead, middle, trail = match.groups()
    100             if safe_input:
    101                 middle = mark_safe(middle)
    102             if middle.startswith('www.') or ('@' not in middle and not (middle.startswith('http://') or middle.startswith('https://')) and \
    103                     len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \
    104                     (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
    105                 middle = 'http://%s' % middle
     99            # Make URL we want to point to.
     100            url = None
    106101            if middle.startswith('http://') or middle.startswith('https://'):
    107102                url = urlquote(middle, safe='/&=:;#?+*')
     103            elif middle.startswith('www.') or ('@' not in middle and \
     104                    len(middle) > 0 and middle[0] in string.ascii_letters + string.digits and \
     105                    (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
     106                url = urlquote('http://%s' % middle, safe='/&=:;#?+*')
     107            elif '@' in middle and not ':' in middle and simple_email_re.match(middle):
     108                url = 'mailto:%s' % middle
     109                nofollow_attr = ''
     110            # Make link.
     111            if url:
     112                trimmed = trim_url(middle)
    108113                if autoescape and not safe_input:
    109                     url = escape(url)
    110                 trimmed_url = trim_url(middle)
    111                 middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr,
    112                         trimmed_url)
    113             elif '@' in middle and not middle.startswith('www.') and \
    114                       not ':' in middle and simple_email_re.match(middle):
    115                 if autoescape:
    116                     middle = conditional_escape(middle)
    117                 middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
    118             if lead + middle + trail != word:
    119                 if autoescape and not safe_input:
    120114                    lead, trail = escape(lead), escape(trail)
     115                    url, trimmed = escape(url), escape(trimmed)
     116                middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed)
    121117                words[i] = mark_safe('%s%s%s' % (lead, middle, trail))
    122             elif autoescape and not safe_input:
    123                 words[i] = escape(word)
     118            else:
     119                if safe_input:
     120                    words[i] = mark_safe(word)
     121                elif autoescape:
     122                    words[i] = escape(word)
    124123        elif safe_input:
    125124            words[i] = mark_safe(word)
    126125        elif autoescape:
Back to Top