Issue #126: Add `target` keyword argument to urlize.
diff --git a/jinja2/utils.py b/jinja2/utils.py
index 1c71789..1045e3f 100644
--- a/jinja2/utils.py
+++ b/jinja2/utils.py
@@ -183,7 +183,7 @@
return pformat(obj)
-def urlize(text, trim_url_limit=None, nofollow=False):
+def urlize(text, trim_url_limit=None, nofollow=False, target=None):
"""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
@@ -194,12 +194,18 @@
If nofollow is True, the URLs in link text will get a rel="nofollow"
attribute.
+
+ If target is not None, a target attribute will be added to the link.
"""
trim_url = lambda x, limit=trim_url_limit: limit is not None \
and (x[:limit] + (len(x) >=limit and '...'
or '')) or x
words = _word_split_re.split(text_type(escape(text)))
nofollow_attr = nofollow and ' rel="nofollow"' or ''
+ if target is not None and isinstance(target, string_types):
+ target_attr = ' target="%s"' % target
+ else:
+ target_attr = ''
for i, word in enumerate(words):
match = _punctuation_re.match(word)
if match:
@@ -214,12 +220,12 @@
middle.endswith('.net') or
middle.endswith('.com')
)):
- middle = '<a href="http://%s"%s>%s</a>' % (middle,
- nofollow_attr, trim_url(middle))
+ middle = '<a href="http://%s"%s%s>%s</a>' % (middle,
+ nofollow_attr, target_attr, trim_url(middle))
if middle.startswith('http://') or \
middle.startswith('https://'):
- middle = '<a href="%s"%s>%s</a>' % (middle,
- nofollow_attr, trim_url(middle))
+ middle = '<a href="%s"%s%s>%s</a>' % (middle,
+ nofollow_attr, target_attr, trim_url(middle))
if '@' in middle and not middle.startswith('www.') and \
not ':' in middle and _simple_email_re.match(middle):
middle = '<a href="mailto:%s">%s</a>' % (middle, middle)