1 # -*- coding: utf-8 -*-
8 :copyright: 2008 by Armin Ronacher.
9 :license: BSD, see LICENSE for more details.
13 from collections import deque
14 from copy import deepcopy
15 from itertools import imap
18 _word_split_re = re.compile(r'(\s+)')
19 _punctuation_re = re.compile(
20 '^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % (
21 '|'.join(imap(re.escape, ('(', '<', '<'))),
22 '|'.join(imap(re.escape, ('.', ',', ')', '>', '\n', '>')))
25 _simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
28 # special singleton representing missing values for the runtime
29 missing = type('MissingType', (), {'__repr__': lambda x: 'missing'})()
32 def contextfunction(f):
33 """This decorator can be used to mark a callable as context callable. A
34 context callable is passed the active context as first argument if it
35 was directly stored in the context.
37 f.contextfunction = True
41 def environmentfunction(f):
42 """This decorator can be used to mark a callable as environment callable.
43 A environment callable is passed the current environment as first argument
44 if it was directly stored in the context.
46 f.environmentfunction = True
50 def import_string(import_name, silent=False):
51 """Imports an object based on a string. This use useful if you want to
52 use import paths as endpoints or something similar. An import path can
53 be specified either in dotted notation (``xml.sax.saxutils.escape``)
54 or with a colon as object delimiter (``xml.sax.saxutils:escape``).
56 If the `silent` is True the return value will be `None` if the import
59 :return: imported object
62 if ':' in import_name:
63 module, obj = import_name.split(':', 1)
64 elif '.' in import_name:
65 items = import_name.split('.')
66 module = '.'.join(items[:-1])
69 return __import__(import_name)
70 return getattr(__import__(module, None, None, [obj]), obj)
71 except (ImportError, AttributeError):
76 def pformat(obj, verbose=False):
77 """Prettyprint an object. Either use the `pretty` library or the
81 from pretty import pretty
82 return pretty(obj, verbose=verbose)
84 from pprint import pformat
88 def urlize(text, trim_url_limit=None, nofollow=False):
89 """Converts any URLs in text into clickable links. Works on http://,
90 https:// and www. links. Links can have trailing punctuation (periods,
91 commas, close-parens) and leading punctuation (opening parens) and
92 it'll still do the right thing.
94 If trim_url_limit is not None, the URLs in link text will be limited
95 to trim_url_limit characters.
97 If nofollow is True, the URLs in link text will get a rel="nofollow"
100 trim_url = lambda x, limit=trim_url_limit: limit is not None \
101 and (x[:limit] + (len(x) >=limit and '...'
103 words = _word_split_re.split(text)
104 nofollow_attr = nofollow and ' rel="nofollow"' or ''
105 for i, word in enumerate(words):
106 match = _punctuation_re.match(word)
108 lead, middle, trail = match.groups()
109 if middle.startswith('www.') or (
110 '@' not in middle and
111 not middle.startswith('http://') and
113 middle[0] in string.letters + string.digits and (
114 middle.endswith('.org') or
115 middle.endswith('.net') or
116 middle.endswith('.com')
118 middle = '<a href="http://%s"%s>%s</a>' % (middle,
119 nofollow_attr, trim_url(middle))
120 if middle.startswith('http://') or \
121 middle.startswith('https://'):
122 middle = '<a href="%s"%s>%s</a>' % (middle,
123 nofollow_attr, trim_url(middle))
124 if '@' in middle and not middle.startswith('www.') and \
125 not ':' in middle and _simple_email_re.match(middle):
126 middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
127 if lead + middle + trail != word:
128 words[i] = lead + middle + trail
129 return u''.join(words)
132 def generate_lorem_ipsum(n=5, html=True, min=20, max=100):
133 """Generate some lorem impsum for the template."""
134 from jinja2.constants import LOREM_IPSUM_WORDS
135 from random import choice, random, randrange
136 words = LOREM_IPSUM_WORDS.split()
140 next_capitalized = True
141 last_comma = last_fullstop = 0
146 # each paragraph contains out of 20 to 100 words.
147 for idx, _ in enumerate(xrange(randrange(min, max))):
154 word = word.capitalize()
155 next_capitalized = False
157 if idx - randrange(3, 8) > last_comma:
161 # add end of sentences
162 if idx - randrange(10, 20) > last_fullstop:
163 last_comma = last_fullstop = idx
165 next_capitalized = True
168 # ensure that the paragraph ends with a dot.
172 elif not p.endswith('.'):
177 return u'\n\n'.join(result)
178 return Markup(u'\n'.join(u'<p>%s</p>' % escape(x) for x in result))
181 class Markup(unicode):
182 """Marks a string as being safe for inclusion in HTML/XML output without
183 needing to be escaped. This implements the `__html__` interface a couple
184 of frameworks and web applications use.
186 The `escape` function returns markup objects so that double escaping can't
187 happen. If you want to use autoescaping in Jinja just set the finalizer
188 of the environment to `escape`.
195 def __add__(self, other):
196 if hasattr(other, '__html__') or isinstance(other, basestring):
197 return self.__class__(unicode(self) + unicode(escape(other)))
198 return NotImplemented
200 def __radd__(self, other):
201 if hasattr(other, '__html__') or isinstance(other, basestring):
202 return self.__class__(unicode(escape(other)) + unicode(self))
203 return NotImplemented
205 def __mul__(self, num):
206 if not isinstance(num, (int, long)):
207 return NotImplemented
208 return self.__class__(unicode.__mul__(self, num))
211 def __mod__(self, arg):
212 if isinstance(arg, tuple):
213 arg = tuple(imap(_MarkupEscapeHelper, arg))
215 arg = _MarkupEscapeHelper(arg)
216 return self.__class__(unicode.__mod__(self, arg))
220 self.__class__.__name__,
221 unicode.__repr__(self)
225 return self.__class__(unicode.join(self, imap(escape, seq)))
226 join.__doc__ = unicode.join.__doc__
228 def split(self, *args, **kwargs):
229 return map(self.__class__, unicode.split(self, *args, **kwargs))
230 split.__doc__ = unicode.split.__doc__
232 def rsplit(self, *args, **kwargs):
233 return map(self.__class__, unicode.rsplit(self, *args, **kwargs))
234 rsplit.__doc__ = unicode.rsplit.__doc__
236 def splitlines(self, *args, **kwargs):
237 return map(self.__class__, unicode.splitlines(self, *args, **kwargs))
238 splitlines.__doc__ = unicode.splitlines.__doc__
240 def make_wrapper(name):
241 orig = getattr(unicode, name)
242 def func(self, *args, **kwargs):
244 for idx, arg in enumerate(args):
245 if hasattr(arg, '__html__') or isinstance(arg, basestring):
246 args[idx] = escape(arg)
247 for name, arg in kwargs.iteritems():
248 if hasattr(arg, '__html__') or isinstance(arg, basestring):
249 kwargs[name] = escape(arg)
250 return self.__class__(orig(self, *args, **kwargs))
251 func.__name__ = orig.__name__
252 func.__doc__ = orig.__doc__
254 for method in '__getitem__', '__getslice__', 'capitalize', \
255 'title', 'lower', 'upper', 'replace', 'ljust', \
256 'rjust', 'lstrip', 'rstrip', 'center', 'strip', \
257 'translate', 'expandtabs', 'swapcase', 'zfill':
258 locals()[method] = make_wrapper(method)
261 if hasattr(unicode, 'partition'):
263 partition=make_wrapper('partition'),
264 rpartition=make_wrapper('rpartition')
266 del method, make_wrapper
269 class _MarkupEscapeHelper(object):
270 """Helper for Markup.__mod__"""
272 def __init__(self, obj):
275 __getitem__ = lambda s, x: _MarkupEscapeHelper(s.obj[x])
276 __unicode__ = lambda s: unicode(escape(s.obj))
277 __str__ = lambda s: str(escape(s.obj))
278 __repr__ = lambda s: str(repr(escape(s.obj)))
279 __int__ = lambda s: int(s.obj)
280 __float__ = lambda s: float(s.obj)
283 class LRUCache(object):
284 """A simple LRU Cache implementation."""
285 # this is fast for small capacities (something around 200) but doesn't
286 # scale. But as long as it's only used for the database connections in
287 # a non request fallback it's fine.
289 def __init__(self, capacity):
290 self.capacity = capacity
292 self._queue = deque()
294 # alias all queue methods for faster lookup
295 self._popleft = self._queue.popleft
296 self._pop = self._queue.pop
297 if hasattr(self._queue, 'remove'):
298 self._remove = self._queue.remove
299 self._append = self._queue.append
301 def _remove(self, obj):
302 """Python 2.4 compatibility."""
303 for idx, item in enumerate(self._queue):
309 """Return an shallow copy of the instance."""
310 rv = self.__class__(self.capacity)
311 rv._mapping.update(self._mapping)
312 rv._queue = deque(self._queue)
315 def get(self, key, default=None):
316 """Return an item from the cache dict or `default`"""
321 def setdefault(self, key, default=None):
322 """Set `default` if the key is not in the cache otherwise
323 leave unchanged. Return the value of this key.
331 """Clear the cache."""
332 self._mapping.clear()
335 def __contains__(self, key):
336 """Check if a key exists in this cache."""
337 return key in self._mapping
340 """Return the current size of the cache."""
341 return len(self._mapping)
345 self.__class__.__name__,
349 def __getitem__(self, key):
350 """Get an item from the cache. Moves the item up so that it has the
351 highest priority then.
353 Raise an `KeyError` if it does not exist.
355 rv = self._mapping[key]
356 if self._queue[-1] != key:
361 def __setitem__(self, key, value):
362 """Sets the value for an item. Moves the item up so that it
363 has the highest priority then.
365 if key in self._mapping:
367 elif len(self._mapping) == self.capacity:
368 del self._mapping[self._popleft()]
370 self._mapping[key] = value
372 def __delitem__(self, key):
373 """Remove an item from the cache dict.
374 Raise an `KeyError` if it does not exist.
376 del self._mapping[key]
380 """Iterate over all values in the cache dict, ordered by
381 the most recent usage.
383 return reversed(self._queue)
385 def __reversed__(self):
386 """Iterate over the values in the cache dict, oldest items
389 return iter(self._queue)
394 # we have to import it down here as the speedups module imports the
395 # markup type which is define above.
397 from jinja2._speedups import escape, soft_unicode
400 """Convert the characters &, <, >, and " in string s to HTML-safe
401 sequences. Use this if you need to display text that might contain
402 such characters in HTML.
404 if hasattr(obj, '__html__'):
405 return obj.__html__()
406 return Markup(unicode(obj)
407 .replace('&', '&')
408 .replace('>', '>')
409 .replace('<', '<')
410 .replace('"', '"')
414 """Make a string unicode if it isn't already. That way a markup
415 string is not converted back to unicode.
417 if not isinstance(s, unicode):
424 from functools import partial
426 class partial(object):
427 def __init__(self, _func, *args, **kwargs):
430 self._kwargs = kwargs
431 def __call__(self, *args, **kwargs):
432 kwargs.update(self._kwargs)
433 return self._func(*(self._args + args), **kwargs)