Armin Ronacher | 07bc684 | 2008-03-31 14:18:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | jinja2.utils |
| 4 | ~~~~~~~~~~~~ |
| 5 | |
| 6 | Utility functions. |
| 7 | |
| 8 | :copyright: 2008 by Armin Ronacher. |
| 9 | :license: BSD, see LICENSE for more details. |
| 10 | """ |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 11 | import re |
| 12 | import string |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame^] | 13 | from functools import update_wrapper |
| 14 | from itertools import imap |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 15 | |
| 16 | |
| 17 | def escape(obj, attribute=False): |
| 18 | """HTML escape an object.""" |
| 19 | if hasattr(obj, '__html__'): |
| 20 | return obj.__html__() |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame^] | 21 | return Markup(unicode(obj) |
| 22 | .replace('&', '&') |
| 23 | .replace('>', '>') |
| 24 | .replace('<', '<') |
Armin Ronacher | 449167d | 2008-04-11 17:55:05 +0200 | [diff] [blame] | 25 | .replace('"', '"') |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame^] | 26 | ) |
Christoph Hack | e9e43bb | 2008-04-13 23:35:48 +0200 | [diff] [blame] | 27 | |
| 28 | |
| 29 | def pformat(obj, verbose=False): |
| 30 | """ |
| 31 | Prettyprint an object. Either use the `pretty` library or the |
| 32 | builtin `pprint`. |
| 33 | """ |
| 34 | try: |
| 35 | from pretty import pretty |
| 36 | return pretty(obj, verbose=verbose) |
| 37 | except ImportError: |
| 38 | from pprint import pformat |
| 39 | return pformat(obj) |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 40 | |
| 41 | |
| 42 | _word_split_re = re.compile(r'(\s+)') |
| 43 | |
| 44 | _punctuation_re = re.compile( |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame^] | 45 | '^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % ( |
| 46 | '|'.join(imap(re.escape, ('(', '<', '<'))), |
| 47 | '|'.join(imap(re.escape, ('.', ',', ')', '>', '\n', '>'))) |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 48 | ) |
| 49 | ) |
| 50 | |
| 51 | _simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$') |
| 52 | |
| 53 | |
| 54 | def urlize(text, trim_url_limit=None, nofollow=False): |
| 55 | """ |
| 56 | Converts any URLs in text into clickable links. Works on http://, |
| 57 | https:// and www. links. Links can have trailing punctuation (periods, |
| 58 | commas, close-parens) and leading punctuation (opening parens) and |
| 59 | it'll still do the right thing. |
| 60 | |
| 61 | If trim_url_limit is not None, the URLs in link text will be limited |
| 62 | to trim_url_limit characters. |
| 63 | |
| 64 | If nofollow is True, the URLs in link text will get a rel="nofollow" |
| 65 | attribute. |
| 66 | """ |
| 67 | trim_url = lambda x, limit=trim_url_limit: limit is not None \ |
| 68 | and (x[:limit] + (len(x) >=limit and '...' |
| 69 | or '')) or x |
| 70 | words = _word_split_re.split(text) |
| 71 | nofollow_attr = nofollow and ' rel="nofollow"' or '' |
| 72 | for i, word in enumerate(words): |
| 73 | match = _punctuation_re.match(word) |
| 74 | if match: |
| 75 | lead, middle, trail = match.groups() |
| 76 | if middle.startswith('www.') or ( |
| 77 | '@' not in middle and |
| 78 | not middle.startswith('http://') and |
| 79 | len(middle) > 0 and |
| 80 | middle[0] in string.letters + string.digits and ( |
| 81 | middle.endswith('.org') or |
| 82 | middle.endswith('.net') or |
| 83 | middle.endswith('.com') |
| 84 | )): |
| 85 | middle = '<a href="http://%s"%s>%s</a>' % (middle, |
| 86 | nofollow_attr, trim_url(middle)) |
| 87 | if middle.startswith('http://') or \ |
| 88 | middle.startswith('https://'): |
| 89 | middle = '<a href="%s"%s>%s</a>' % (middle, |
| 90 | nofollow_attr, trim_url(middle)) |
| 91 | if '@' in middle and not middle.startswith('www.') and \ |
| 92 | not ':' in middle and _simple_email_re.match(middle): |
| 93 | middle = '<a href="mailto:%s">%s</a>' % (middle, middle) |
| 94 | if lead + middle + trail != word: |
| 95 | words[i] = lead + middle + trail |
| 96 | return u''.join(words) |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame^] | 97 | |
| 98 | |
| 99 | class Markup(unicode): |
| 100 | """Marks a string as being safe for inclusion in HTML/XML output without |
| 101 | needing to be escaped. This implements the `__html__` interface a couple |
| 102 | of frameworks and web applications use. |
| 103 | |
| 104 | The `escape` function returns markup objects so that double escaping can't |
| 105 | happen. If you want to use autoescaping in Jinja just set the finalizer |
| 106 | of the environment to `escape`. |
| 107 | """ |
| 108 | |
| 109 | __slots__ = () |
| 110 | |
| 111 | def __html__(self): |
| 112 | return self |
| 113 | |
| 114 | def __add__(self, other): |
| 115 | if hasattr(other, '__html__') or isinstance(other, basestring): |
| 116 | return self.__class__(unicode(self) + unicode(escape(other))) |
| 117 | return NotImplemented |
| 118 | |
| 119 | def __radd__(self, other): |
| 120 | if hasattr(other, '__html__') or isinstance(other, basestring): |
| 121 | return self.__class__(unicode(escape(other)) + unicode(self)) |
| 122 | return NotImplemented |
| 123 | |
| 124 | def __mul__(self, num): |
| 125 | if not isinstance(num, (int, long)): |
| 126 | return NotImplemented |
| 127 | return self.__class__(unicode.__mul__(self, num)) |
| 128 | __rmul__ = __mul__ |
| 129 | |
| 130 | def __mod__(self, arg): |
| 131 | if isinstance(arg, tuple): |
| 132 | arg = tuple(imap(_MarkupEscapeHelper, arg)) |
| 133 | else: |
| 134 | arg = _MarkupEscapeHelper(arg) |
| 135 | return self.__class__(unicode.__mod__(self, arg)) |
| 136 | |
| 137 | def __repr__(self): |
| 138 | return '%s(%s)' % ( |
| 139 | self.__class__.__name__, |
| 140 | unicode.__repr__(self) |
| 141 | ) |
| 142 | |
| 143 | def join(self, seq): |
| 144 | return self.__class__(unicode.join(self, imap(escape, seq))) |
| 145 | |
| 146 | def split(self, *args, **kwargs): |
| 147 | return map(self.__class__, unicode.split(self, *args, **kwargs)) |
| 148 | |
| 149 | def rsplit(self, *args, **kwargs): |
| 150 | return map(self.__class__, unicode.rsplit(self, *args, **kwargs)) |
| 151 | |
| 152 | def splitlines(self, *args, **kwargs): |
| 153 | return map(self.__class__, unicode.splitlines(self, *args, **kwargs)) |
| 154 | |
| 155 | def make_wrapper(name): |
| 156 | orig = getattr(unicode, name) |
| 157 | def func(self, *args, **kwargs): |
| 158 | args = list(args) |
| 159 | for idx, arg in enumerate(args): |
| 160 | if hasattr(arg, '__html__') or isinstance(arg, basestring): |
| 161 | args[idx] = escape(arg) |
| 162 | for name, arg in kwargs.iteritems(): |
| 163 | if hasattr(arg, '__html__') or isinstance(arg, basestring): |
| 164 | kwargs[name] = escape(arg) |
| 165 | return self.__class__(orig(self, *args, **kwargs)) |
| 166 | return update_wrapper(func, orig, ('__name__', '__doc__')) |
| 167 | for method in '__getitem__', '__getslice__', 'capitalize', \ |
| 168 | 'title', 'lower', 'upper', 'replace', 'ljust', \ |
| 169 | 'rjust', 'lstrip', 'rstrip', 'partition', 'center', \ |
| 170 | 'strip', 'translate', 'expandtabs', 'rpartition', \ |
| 171 | 'swapcase', 'zfill': |
| 172 | locals()[method] = make_wrapper(method) |
| 173 | del method, make_wrapper |
| 174 | |
| 175 | |
| 176 | class _MarkupEscapeHelper(object): |
| 177 | """Helper for Markup.__mod__""" |
| 178 | |
| 179 | def __init__(self, obj): |
| 180 | self.obj = obj |
| 181 | |
| 182 | __getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x]) |
| 183 | __unicode__ = lambda s: unicode(escape(s.obj)) |
| 184 | __str__ = lambda s: str(escape(s.obj)) |
| 185 | __repr__ = lambda s: str(repr(escape(s.obj))) |
| 186 | __int__ = lambda s: int(s.obj) |
| 187 | __float__ = lambda s: float(s.obj) |