Reworked implementation of the urlescape filter, made it Python3 compatible, document...
[jinja2.git] / jinja2 / utils.py
index 49e9e9ae0737dd5876b1ca67d8f00185dcad0370..1554a143889da9dc2c0699ed6c56d3d5bd1d79b0 100644 (file)
 import re
 import sys
 import errno
+try:
+    from urllib.parse import quote_from_bytes as url_quote
+except ImportError:
+    from urllib import quote as url_quote
 try:
     from thread import allocate_lock
 except ImportError:
@@ -349,6 +353,21 @@ def generate_lorem_ipsum(n=5, html=True, min=20, max=100):
     return Markup(u'\n'.join(u'<p>%s</p>' % escape(x) for x in result))
 
 
+def unicode_urlescape(obj, charset='utf-8'):
+    """URL escapes a single bytestring or unicode string with the
+    given charset if applicable to URL safe quoting under all rules
+    that need to be considered under all supported Python versions.
+
+    If non strings are provided they are converted to their unicode
+    representation first.
+    """
+    if not isinstance(obj, basestring):
+        obj = unicode(obj)
+    if isinstance(obj, unicode):
+        obj = obj.encode(charset)
+    return unicode(url_quote(obj))
+
+
 class LRUCache(object):
     """A simple LRU Cache implementation."""