From d55e1e59c1220030390e8d6a9487a1b516ed1829 Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Wed, 21 Aug 2013 13:13:48 +0300 Subject: [PATCH] Issue #126: Add `target` keyword argument to urlize. --- jinja2/filters.py | 15 +++++++++++++-- jinja2/testsuite/filters.py | 8 ++++++++ jinja2/utils.py | 16 +++++++++++----- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/jinja2/filters.py b/jinja2/filters.py index 70bb9482..43806875 100644 --- a/jinja2/filters.py +++ b/jinja2/filters.py @@ -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 + ```` 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 diff --git a/jinja2/testsuite/filters.py b/jinja2/testsuite/filters.py index 9d5c9522..c7c35990 100644 --- a/jinja2/testsuite/filters.py +++ b/jinja2/testsuite/filters.py @@ -238,6 +238,14 @@ class FilterTestCase(JinjaTestCase): assert tmpl.render() == 'foo '\ 'http://www.example.com/ bar' + def test_urlize_target_parameter(self): + tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize(target="_blank") }}') + assert tmpl.render() == 'foo '\ + 'http://www.example.com/ bar' + tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize(target=42) }}') + assert tmpl.render() == 'foo '\ + 'http://www.example.com/ bar' + def test_wordcount(self): tmpl = env.from_string('{{ "foo bar baz"|wordcount }}') assert tmpl.render() == '3' diff --git a/jinja2/utils.py b/jinja2/utils.py index 1c717899..1045e3f4 100644 --- a/jinja2/utils.py +++ b/jinja2/utils.py @@ -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 = '%s' % (middle, - nofollow_attr, trim_url(middle)) + middle = '%s' % (middle, + nofollow_attr, target_attr, trim_url(middle)) if middle.startswith('http://') or \ middle.startswith('https://'): - middle = '%s' % (middle, - nofollow_attr, trim_url(middle)) + middle = '%s' % (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 = '%s' % (middle, middle) -- 2.47.2