Issue #126: Add `target` keyword argument to urlize.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Berker Peksag · committed by Berker Peksag · parent 63227f0 · Browse files · 3 files·+32 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+32 added−7 removed
- other:+32 added−7 removed
Provenance
- Commit
d55e1e5- Tree
a2df0e0- Author
Berker Peksag <berker.peksag@gmail.com>- Committer
Berker Peksag <berker.peksag@gmail.com>- Parent
63227f0
3 files changed+32 added−7 removed
modified:jinja2/testsuite/filters.py+8 added
236 unmodified lines237 tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize }}')238 assert tmpl.render() == 'foo <a href="http://www.example.com/">'\239 'http://www.example.com/</a> bar'240 241+ def test_urlize_target_parameter(self):242+ tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize(target="_blank") }}')243+ assert tmpl.render() == 'foo <a href="http://www.example.com/" target="_blank">'\244+ 'http://www.example.com/</a> bar'245+ tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize(target=42) }}')246+ assert tmpl.render() == 'foo <a href="http://www.example.com/">'\247+ 'http://www.example.com/</a> bar'248+249 def test_wordcount(self):250 tmpl = env.from_string('{{ "foo bar baz"|wordcount }}')251 assert tmpl.render() == '3'252 279 unmodified linesmodified:jinja2/filters.py+13 added-2 removed
407 unmodified lines408 return pformat(value, verbose=verbose)409 410 411 @evalcontextfilter412-def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False):412+def do_urlize(eval_ctx, value, trim_url_limit=None, nofollow=False,413+ target=None):414 """Converts URLs in plain text into clickable links.415 416 If you pass the filter an additional integer it will shorten the urls417 to that number. Also a third argument exists that makes the urls2 unmodified lines420 .. sourcecode:: jinja421 422 {{ mytext|urlize(40, true) }}423 links are shortened to 40 chars and defined with rel="nofollow"424+425+ If *target* is specified, the ``target`` attribute will be added to the426+ ``<a>`` tag:427+428+ .. sourcecode:: jinja429+430+ {{ mytext|urlize(40, target='_blank') }}431+432+ .. versionchanged:: 2.8+433+ The *target* parameter was added.434 """424- rv = urlize(value, trim_url_limit, nofollow)435+ rv = urlize(value, trim_url_limit, nofollow, target)436 if eval_ctx.autoescape:437 rv = Markup(rv)438 return rv439 560 unmodified linesmodified:jinja2/utils.py+11 added-5 removed
181 unmodified lines182 from pprint import pformat183 return pformat(obj)184 185 186-def urlize(text, trim_url_limit=None, nofollow=False):186+def urlize(text, trim_url_limit=None, nofollow=False, target=None):187 """Converts any URLs in text into clickable links. Works on http://,188 https:// and www. links. Links can have trailing punctuation (periods,189 commas, close-parens) and leading punctuation (opening parens) and190 it'll still do the right thing.2 unmodified lines193 to trim_url_limit characters.194 195 If nofollow is True, the URLs in link text will get a rel="nofollow"196 attribute.197+198+ If target is not None, a target attribute will be added to the link.199 """200 trim_url = lambda x, limit=trim_url_limit: limit is not None \201 and (x[:limit] + (len(x) >=limit and '...'202 or '')) or x203 words = _word_split_re.split(text_type(escape(text)))204 nofollow_attr = nofollow and ' rel="nofollow"' or ''205+ if target is not None and isinstance(target, string_types):206+ target_attr = ' target="%s"' % target207+ else:208+ target_attr = ''209 for i, word in enumerate(words):210 match = _punctuation_re.match(word)211 if match:212 lead, middle, trail = match.groups()6 unmodified lines219 middle.endswith('.org') or220 middle.endswith('.net') or221 middle.endswith('.com')222 )):217- middle = '<a href="http://%s"%s>%s</a>' % (middle,218- nofollow_attr, trim_url(middle))223+ middle = '<a href="http://%s"%s%s>%s</a>' % (middle,224+ nofollow_attr, target_attr, trim_url(middle))225 if middle.startswith('http://') or \226 middle.startswith('https://'):221- middle = '<a href="%s"%s>%s</a>' % (middle,222- nofollow_attr, trim_url(middle))227+ middle = '<a href="%s"%s%s>%s</a>' % (middle,228+ nofollow_attr, target_attr, trim_url(middle))229 if '@' in middle and not middle.startswith('www.') and \230 not ':' in middle and _simple_email_re.match(middle):231 middle = '<a href="mailto:%s">%s</a>' % (middle, middle)232 if lead + middle + trail != word:295 unmodified lines