]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Modified linkify to accept a callable for extra_params. This allows different params...
authorJason Choy <jjwchoy@gmail.com>
Sun, 6 May 2012 12:22:40 +0000 (13:22 +0100)
committerJason Choy <jjwchoy@gmail.com>
Sun, 6 May 2012 12:22:40 +0000 (13:22 +0100)
tornado/escape.py

index 4222864988f9fcbb1f353a3b8f3ba7dcd5536f42..71083e7755a2c7301f40c11f9c52b6557e8a8113 100644 (file)
@@ -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