app/django/utils/html.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
--- a/app/django/utils/html.py	Tue Oct 14 12:36:55 2008 +0000
+++ b/app/django/utils/html.py	Tue Oct 14 16:00:59 2008 +0000
@@ -76,51 +76,53 @@
     """
     Converts any URLs in text into clickable links.
 
-    Works on http://, https://, and www. links.  Links can have trailing
-    punctuation (periods, commas, close-parens) and leading punctuation
-    (opening parens) and it'll still do the right thing.
+    Works on http://, https://, www. links and links ending in .org, .net or
+    .com. Links can have trailing punctuation (periods, commas, close-parens)
+    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
     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"
     attribute.
+
+    If autoescape is True, the link text and URLs will get autoescaped.
     """
-    if autoescape:
-        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
+    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)
     words = word_split_re.split(force_unicode(text))
     nofollow_attr = nofollow and ' rel="nofollow"' or ''
     for i, word in enumerate(words):
-        match = punctuation_re.match(word)
+        match = None
+        if '.' in word or '@' in word or ':' in word:
+            match = punctuation_re.match(word)
         if match:
             lead, middle, trail = match.groups()
-            if safe_input:
-                middle = mark_safe(middle)
-            if middle.startswith('www.') or ('@' not in middle and not middle.startswith('http://') 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
+            # Make URL we want to point to.
+            url = None
             if middle.startswith('http://') or middle.startswith('https://'):
                 url = urlquote(middle, safe='/&=:;#?+*')
-                if autoescape and not safe_input:
-                    url = escape(url)
-                trimmed_url = trim_url(middle)
-                middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr,
-                        trimmed_url)
-            elif '@' in middle and not middle.startswith('www.') and \
-                      not ':' in middle and simple_email_re.match(middle):
-                if autoescape:
-                    middle = conditional_escape(middle)
-                middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
-            if lead + middle + trail != word:
+            elif middle.startswith('www.') or ('@' not in middle and \
+                    middle and middle[0] in string.ascii_letters + string.digits and \
+                    (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
+                url = urlquote('http://%s' % middle, safe='/&=:;#?+*')
+            elif '@' in middle and not ':' in middle and simple_email_re.match(middle):
+                url = 'mailto:%s' % middle
+                nofollow_attr = ''
+            # Make link.
+            if url:
+                trimmed = trim_url(middle)
                 if autoescape and not safe_input:
                     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))
-            elif autoescape and not safe_input:
-                words[i] = escape(word)
+            else:
+                if safe_input:
+                    words[i] = mark_safe(word)
+                elif autoescape:
+                    words[i] = escape(word)
         elif safe_input:
             words[i] = mark_safe(word)
         elif autoescape: