From 53e7c751c31b676e53611c7a1aa0125c9acc6251 Mon Sep 17 00:00:00 2001 From: Jason Choy Date: Sun, 6 May 2012 13:22:40 +0100 Subject: [PATCH 1/3] Modified linkify to accept a callable for extra_params. This allows different params based for each link, for example to treat internal and external links differently --- tornado/escape.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tornado/escape.py b/tornado/escape.py index 42228649..71083e77 100644 --- a/tornado/escape.py +++ b/tornado/escape.py @@ -246,8 +246,15 @@ def linkify(text, shorten=False, extra_params="", shorten: Long urls will be shortened for display. - extra_params: Extra text to include in the link tag, + extra_params: Extra text to include in the link tag, or a callable + taking the link as an argument and returning the extra text e.g. linkify(text, extra_params='rel="nofollow" class="external"') + or def extra_params_cb(url): + if url.startswith("http://example.com"): + return 'class="internal"' + else: + return 'class="external" rel="nofollow"' + linkify(text, extra_params=extra_params_cb) require_protocol: Only linkify urls which include a protocol. If this is False, urls such as www.facebook.com will also be linkified. @@ -256,7 +263,7 @@ def linkify(text, shorten=False, extra_params="", e.g. linkify(text, permitted_protocols=["http", "ftp", "mailto"]). It is very unsafe to include protocols such as "javascript". """ - if extra_params: + if extra_params and not callable(extra_params): extra_params = " " + extra_params.strip() def make_link(m): @@ -272,7 +279,10 @@ def linkify(text, shorten=False, extra_params="", if not proto: href = "http://" + href # no proto specified, use http - params = extra_params + if callable(extra_params): + params = " " + extra_params(href).strip() + else: + params = extra_params # clip long urls. max_len is just an approximation max_len = 30 From 4fd35b8c340b01ffbdb6921902d3b57fecad79e7 Mon Sep 17 00:00:00 2001 From: Jason Choy Date: Tue, 8 May 2012 10:15:24 +0100 Subject: [PATCH 2/3] Added a test case for extra_params as a callable --- tornado/test/escape_test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tornado/test/escape_test.py b/tornado/test/escape_test.py index 25b5dae1..4baa70d2 100644 --- a/tornado/test/escape_test.py +++ b/tornado/test/escape_test.py @@ -121,6 +121,10 @@ linkify_tests = [ ("www.external-link.com", {"extra_params": 'rel="nofollow" class="external"'}, u'www.external-link.com'), + + ("www.external-link.com and www.internal-link.com/blogs extra", + {"extra_params": lambda(href):'class="internal"' if href.startswith("http://www.internal-link.com") else 'rel="nofollow" class="external"'}, + u'www.external-link.com and www.internal-link.com/blogs extra'), ] From 783d76cbfcccf19ac3e905db06f4237443a46afe Mon Sep 17 00:00:00 2001 From: Jason Choy Date: Tue, 8 May 2012 10:24:46 +0100 Subject: [PATCH 3/3] Test strip and add space still works when extra_params is a callable --- tornado/test/escape_test.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tornado/test/escape_test.py b/tornado/test/escape_test.py index 4baa70d2..defca1ba 100644 --- a/tornado/test/escape_test.py +++ b/tornado/test/escape_test.py @@ -125,6 +125,10 @@ linkify_tests = [ ("www.external-link.com and www.internal-link.com/blogs extra", {"extra_params": lambda(href):'class="internal"' if href.startswith("http://www.internal-link.com") else 'rel="nofollow" class="external"'}, u'www.external-link.com and www.internal-link.com/blogs extra'), + + ("www.external-link.com", + {"extra_params": lambda(href):' rel="nofollow" class="external" '}, + u'www.external-link.com'), ]