summaryrefslogtreecommitdiff
path: root/django/utils/html.py
diff options
context:
space:
mode:
authorBrian Rosner <brosner@gmail.com>2008-06-26 15:42:33 +0000
committerBrian Rosner <brosner@gmail.com>2008-06-26 15:42:33 +0000
commitc8da0874c78ed4c6e1ad08cc78228799a333f76c (patch)
treebef645a8eb2c1a17f013a1031ed34494547dced0 /django/utils/html.py
parentf15845c573f019fc7f6d7404add122f9b7c52dc4 (diff)
newforms-admin: Merged from trunk up to [7766].
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@7770 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/html.py')
-rw-r--r--django/utils/html.py52
1 files changed, 26 insertions, 26 deletions
diff --git a/django/utils/html.py b/django/utils/html.py
index 07e4f0d3f4..747af52879 100644
--- a/django/utils/html.py
+++ b/django/utils/html.py
@@ -76,20 +76,20 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
"""
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 ''
@@ -97,30 +97,30 @@ def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):
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://') 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
+ # 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 \
+ len(middle) > 0 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: