Permalink
Browse files

Issue #126: Add `target` keyword argument to urlize.

  • Loading branch information...
1 parent 63227f0 commit d55e1e59c1220030390e8d6a9487a1b516ed1829 @berkerpeksag berkerpeksag committed Aug 21, 2013
Showing with 32 additions and 7 deletions.
  1. +13 −2 jinja2/filters.py
  2. +8 −0 jinja2/testsuite/filters.py
  3. +11 −5 jinja2/utils.py
View
@@ -409,7 +409,8 @@ def do_pprint(value, verbose=False):
@evalcontextfilter
-def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False):
+def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False,
+ target=None):
"""Converts URLs in plain text into clickable links.
If you pass the filter an additional integer it will shorten the urls
@@ -420,8 +421,18 @@ def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False):
{{ mytext|urlize(40, true) }}
links are shortened to 40 chars and defined with rel="nofollow"
+
+ If *target* is specified, the ``target`` attribute will be added to the
+ ``<a>`` tag:
+
+ .. sourcecode:: jinja
+
+ {{ mytext|urlize(40, target='_blank') }}
+
+ .. versionchanged:: 2.8+
+ The *target* parameter was added.
"""
- rv = urlize(value, trim_url_limit, nofollow)
+ rv = urlize(value, trim_url_limit, nofollow, target)
if eval_ctx.autoescape:
rv = Markup(rv)
return rv
@@ -238,6 +238,14 @@ def test_urlize(self):
assert tmpl.render() == 'foo <a href="http://www.example.com/">'\
'http://www.example.com/</a> bar'
+ def test_urlize_target_parameter(self):
+ tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize(target="_blank") }}')
+ assert tmpl.render() == 'foo <a href="http://www.example.com/" target="_blank">'\
+ 'http://www.example.com/</a> bar'
+ tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize(target=42) }}')
+ assert tmpl.render() == 'foo <a href="http://www.example.com/">'\
+ 'http://www.example.com/</a> bar'
+
def test_wordcount(self):
tmpl = env.from_string('{{ "foo bar baz"|wordcount }}')
assert tmpl.render() == '3'
View
@@ -183,7 +183,7 @@ def pformat(obj, verbose=False):
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 @@ def urlize(text, trim_url_limit=None, nofollow=False):
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 @@ def urlize(text, trim_url_limit=None, nofollow=False):
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)

0 comments on commit d55e1e5

Please sign in to comment.