Automated merge with http://dev.pocoo.org/hg/jinja2-main/
[jinja2.git] / jinja2 / ext.py
1 # -*- coding: utf-8 -*-
2 """
3     jinja2.ext
4     ~~~~~~~~~~
5
6     Jinja extensions allow to add custom tags similar to the way django custom
7     tags work.  By default two example extensions exist: an i18n and a cache
8     extension.
9
10     :copyright: Copyright 2008 by Armin Ronacher.
11     :license: BSD.
12 """
13 from collections import deque
14 from jinja2 import nodes
15 from jinja2.defaults import *
16 from jinja2.environment import get_spontaneous_environment
17 from jinja2.runtime import Undefined, concat
18 from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError
19 from jinja2.utils import contextfunction, import_string, Markup
20
21
22 # the only real useful gettext functions for a Jinja template.  Note
23 # that ugettext must be assigned to gettext as Jinja doesn't support
24 # non unicode strings.
25 GETTEXT_FUNCTIONS = ('_', 'gettext', 'ngettext')
26
27
28 class ExtensionRegistry(type):
29     """Gives the extension an unique identifier."""
30
31     def __new__(cls, name, bases, d):
32         rv = type.__new__(cls, name, bases, d)
33         rv.identifier = rv.__module__ + '.' + rv.__name__
34         return rv
35
36
37 class Extension(object):
38     """Extensions can be used to add extra functionality to the Jinja template
39     system at the parser level.  Custom extensions are bound to an environment
40     but may not store environment specific data on `self`.  The reason for
41     this is that an extension can be bound to another environment (for
42     overlays) by creating a copy and reassigning the `environment` attribute.
43
44     As extensions are created by the environment they cannot accept any
45     arguments for configuration.  One may want to work around that by using
46     a factory function, but that is not possible as extensions are identified
47     by their import name.  The correct way to configure the extension is
48     storing the configuration values on the environment.  Because this way the
49     environment ends up acting as central configuration storage the
50     attributes may clash which is why extensions have to ensure that the names
51     they choose for configuration are not too generic.  ``prefix`` for example
52     is a terrible name, ``fragment_cache_prefix`` on the other hand is a good
53     name as includes the name of the extension (fragment cache).
54     """
55     __metaclass__ = ExtensionRegistry
56
57     #: if this extension parses this is the list of tags it's listening to.
58     tags = set()
59
60     def __init__(self, environment):
61         self.environment = environment
62
63     def bind(self, environment):
64         """Create a copy of this extension bound to another environment."""
65         rv = object.__new__(self.__class__)
66         rv.__dict__.update(self.__dict__)
67         rv.environment = environment
68         return rv
69
70     def preprocess(self, source, name, filename=None):
71         """This method is called before the actual lexing and can be used to
72         preprocess the source.  The `filename` is optional.  The return value
73         must be the preprocessed source.
74         """
75         return source
76
77     def filter_stream(self, stream):
78         """It's passed a :class:`~jinja2.lexer.TokenStream` that can be used
79         to filter tokens returned.  This method has to return an iterable of
80         :class:`~jinja2.lexer.Token`\s, but it doesn't have to return a
81         :class:`~jinja2.lexer.TokenStream`.
82
83         In the `ext` folder of the Jinja2 source distribution there is a file
84         called `inlinegettext.py` which implements a filter that utilizes this
85         method.
86         """
87         return stream
88
89     def parse(self, parser):
90         """If any of the :attr:`tags` matched this method is called with the
91         parser as first argument.  The token the parser stream is pointing at
92         is the name token that matched.  This method has to return one or a
93         list of multiple nodes.
94         """
95         raise NotImplementedError()
96
97     def attr(self, name, lineno=None):
98         """Return an attribute node for the current extension.  This is useful
99         to pass constants on extensions to generated template code::
100
101             self.attr('_my_attribute', lineno=lineno)
102         """
103         return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)
104
105     def call_method(self, name, args=None, kwargs=None, dyn_args=None,
106                     dyn_kwargs=None, lineno=None):
107         """Call a method of the extension.  This is a shortcut for
108         :meth:`attr` + :class:`jinja2.nodes.Call`.
109         """
110         if args is None:
111             args = []
112         if kwargs is None:
113             kwargs = []
114         return nodes.Call(self.attr(name, lineno=lineno), args, kwargs,
115                           dyn_args, dyn_kwargs, lineno=lineno)
116
117
118 @contextfunction
119 def _gettext_alias(context, string):
120     return context.resolve('gettext')(string)
121
122
123 class InternationalizationExtension(Extension):
124     """This extension adds gettext support to Jinja2."""
125     tags = set(['trans'])
126
127     def __init__(self, environment):
128         Extension.__init__(self, environment)
129         environment.globals['_'] = _gettext_alias
130         environment.extend(
131             install_gettext_translations=self._install,
132             install_null_translations=self._install_null,
133             uninstall_gettext_translations=self._uninstall,
134             extract_translations=self._extract
135         )
136
137     def _install(self, translations):
138         self.environment.globals.update(
139             gettext=translations.ugettext,
140             ngettext=translations.ungettext
141         )
142
143     def _install_null(self):
144         self.environment.globals.update(
145             gettext=lambda x: x,
146             ngettext=lambda s, p, n: (n != 1 and (p,) or (s,))[0]
147         )
148
149     def _uninstall(self, translations):
150         for key in 'gettext', 'ngettext':
151             self.environment.globals.pop(key, None)
152
153     def _extract(self, source, gettext_functions=GETTEXT_FUNCTIONS):
154         if isinstance(source, basestring):
155             source = self.environment.parse(source)
156         return extract_from_ast(source, gettext_functions)
157
158     def parse(self, parser):
159         """Parse a translatable tag."""
160         lineno = parser.stream.next().lineno
161
162         # find all the variables referenced.  Additionally a variable can be
163         # defined in the body of the trans block too, but this is checked at
164         # a later state.
165         plural_expr = None
166         variables = {}
167         while parser.stream.current.type is not 'block_end':
168             if variables:
169                 parser.stream.expect('comma')
170
171             # skip colon for python compatibility
172             if parser.stream.skip_if('colon'):
173                 break
174
175             name = parser.stream.expect('name')
176             if name.value in variables:
177                 parser.fail('translatable variable %r defined twice.' %
178                             name.value, name.lineno,
179                             exc=TemplateAssertionError)
180
181             # expressions
182             if parser.stream.current.type is 'assign':
183                 parser.stream.next()
184                 variables[name.value] = var = parser.parse_expression()
185             else:
186                 variables[name.value] = var = nodes.Name(name.value, 'load')
187             if plural_expr is None:
188                 plural_expr = var
189
190         parser.stream.expect('block_end')
191
192         plural = plural_names = None
193         have_plural = False
194         referenced = set()
195
196         # now parse until endtrans or pluralize
197         singular_names, singular = self._parse_block(parser, True)
198         if singular_names:
199             referenced.update(singular_names)
200             if plural_expr is None:
201                 plural_expr = nodes.Name(singular_names[0], 'load')
202
203         # if we have a pluralize block, we parse that too
204         if parser.stream.current.test('name:pluralize'):
205             have_plural = True
206             parser.stream.next()
207             if parser.stream.current.type is not 'block_end':
208                 plural_expr = parser.parse_expression()
209             parser.stream.expect('block_end')
210             plural_names, plural = self._parse_block(parser, False)
211             parser.stream.next()
212             referenced.update(plural_names)
213         else:
214             parser.stream.next()
215
216         # register free names as simple name expressions
217         for var in referenced:
218             if var not in variables:
219                 variables[var] = nodes.Name(var, 'load')
220
221         # no variables referenced?  no need to escape
222         if not referenced:
223             singular = singular.replace('%%', '%')
224             if plural:
225                 plural = plural.replace('%%', '%')
226
227         if not have_plural:
228             plural_expr = None
229         elif plural_expr is None:
230             parser.fail('pluralize without variables', lineno)
231
232         if variables:
233             variables = nodes.Dict([nodes.Pair(nodes.Const(x, lineno=lineno), y)
234                                     for x, y in variables.items()])
235         else:
236             variables = None
237
238         node = self._make_node(singular, plural, variables, plural_expr)
239         node.set_lineno(lineno)
240         return node
241
242     def _parse_block(self, parser, allow_pluralize):
243         """Parse until the next block tag with a given name."""
244         referenced = []
245         buf = []
246         while 1:
247             if parser.stream.current.type is 'data':
248                 buf.append(parser.stream.current.value.replace('%', '%%'))
249                 parser.stream.next()
250             elif parser.stream.current.type is 'variable_begin':
251                 parser.stream.next()
252                 name = parser.stream.expect('name').value
253                 referenced.append(name)
254                 buf.append('%%(%s)s' % name)
255                 parser.stream.expect('variable_end')
256             elif parser.stream.current.type is 'block_begin':
257                 parser.stream.next()
258                 if parser.stream.current.test('name:endtrans'):
259                     break
260                 elif parser.stream.current.test('name:pluralize'):
261                     if allow_pluralize:
262                         break
263                     parser.fail('a translatable section can have only one '
264                                 'pluralize section')
265                 parser.fail('control structures in translatable sections are '
266                             'not allowed')
267             elif parser.stream.eos:
268                 parser.fail('unclosed translation block')
269             else:
270                 assert False, 'internal parser error'
271
272         return referenced, concat(buf)
273
274     def _make_node(self, singular, plural, variables, plural_expr):
275         """Generates a useful node from the data provided."""
276         # singular only:
277         if plural_expr is None:
278             gettext = nodes.Name('gettext', 'load')
279             node = nodes.Call(gettext, [nodes.Const(singular)],
280                               [], None, None)
281
282         # singular and plural
283         else:
284             ngettext = nodes.Name('ngettext', 'load')
285             node = nodes.Call(ngettext, [
286                 nodes.Const(singular),
287                 nodes.Const(plural),
288                 plural_expr
289             ], [], None, None)
290
291         # mark the return value as safe if we are in an
292         # environment with autoescaping turned on
293         if self.environment.autoescape:
294             node = nodes.MarkSafe(node)
295
296         if variables:
297             node = nodes.Mod(node, variables)
298         return nodes.Output([node])
299
300
301 class ExprStmtExtension(Extension):
302     """Adds a `do` tag to Jinja2 that works like the print statement just
303     that it doesn't print the return value.
304     """
305     tags = set(['do'])
306
307     def parse(self, parser):
308         node = nodes.ExprStmt(lineno=parser.stream.next().lineno)
309         node.node = parser.parse_tuple()
310         return node
311
312
313 class LoopControlExtension(Extension):
314     """Adds break and continue to the template engine."""
315     tags = set(['break', 'continue'])
316
317     def parse(self, parser):
318         token = parser.stream.next()
319         if token.value == 'break':
320             return nodes.Break(lineno=token.lineno)
321         return nodes.Continue(lineno=token.lineno)
322
323
324 def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,
325                      babel_style=True):
326     """Extract localizable strings from the given template node.  Per
327     default this function returns matches in babel style that means non string
328     parameters as well as keyword arguments are returned as `None`.  This
329     allows Babel to figure out what you really meant if you are using
330     gettext functions that allow keyword arguments for placeholder expansion.
331     If you don't want that behavior set the `babel_style` parameter to `False`
332     which causes only strings to be returned and parameters are always stored
333     in tuples.  As a consequence invalid gettext calls (calls without a single
334     string parameter or string parameters after non-string parameters) are
335     skipped.
336
337     This example explains the behavior:
338
339     >>> from jinja2 import Environment
340     >>> env = Environment()
341     >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')
342     >>> list(extract_from_ast(node))
343     [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]
344     >>> list(extract_from_ast(node, babel_style=False))
345     [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]
346
347     For every string found this function yields a ``(lineno, function,
348     message)`` tuple, where:
349
350     * ``lineno`` is the number of the line on which the string was found,
351     * ``function`` is the name of the ``gettext`` function used (if the
352       string was extracted from embedded Python code), and
353     *  ``message`` is the string itself (a ``unicode`` object, or a tuple
354        of ``unicode`` objects for functions with multiple string arguments).
355     """
356     for node in node.find_all(nodes.Call):
357         if not isinstance(node.node, nodes.Name) or \
358            node.node.name not in gettext_functions:
359             continue
360
361         strings = []
362         for arg in node.args:
363             if isinstance(arg, nodes.Const) and \
364                isinstance(arg.value, basestring):
365                 strings.append(arg.value)
366             else:
367                 strings.append(None)
368
369         for arg in node.kwargs:
370             strings.append(None)
371         if node.dyn_args is not None:
372             strings.append(None)
373         if node.dyn_kwargs is not None:
374             strings.append(None)
375
376         if not babel_style:
377             strings = tuple(x for x in strings if x is not None)
378             if not strings:
379                 continue
380         else:
381             if len(strings) == 1:
382                 strings = strings[0]
383             else:
384                 strings = tuple(strings)
385         yield node.lineno, node.node.name, strings
386
387
388 def babel_extract(fileobj, keywords, comment_tags, options):
389     """Babel extraction method for Jinja templates.
390
391     :param fileobj: the file-like object the messages should be extracted from
392     :param keywords: a list of keywords (i.e. function names) that should be
393                      recognized as translation functions
394     :param comment_tags: a list of translator tags to search for and include
395                          in the results.  (Unused)
396     :param options: a dictionary of additional options (optional)
397     :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
398              (comments will be empty currently)
399     """
400     extensions = set()
401     for extension in options.get('extensions', '').split(','):
402         extension = extension.strip()
403         if not extension:
404             continue
405         extensions.add(import_string(extension))
406     if InternationalizationExtension not in extensions:
407         extensions.add(InternationalizationExtension)
408
409     environment = get_spontaneous_environment(
410         options.get('block_start_string', BLOCK_START_STRING),
411         options.get('block_end_string', BLOCK_END_STRING),
412         options.get('variable_start_string', VARIABLE_START_STRING),
413         options.get('variable_end_string', VARIABLE_END_STRING),
414         options.get('comment_start_string', COMMENT_START_STRING),
415         options.get('comment_end_string', COMMENT_END_STRING),
416         options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,
417         str(options.get('trim_blocks', TRIM_BLOCKS)).lower() in \
418             ('1', 'on', 'yes', 'true'),
419         NEWLINE_SEQUENCE, frozenset(extensions),
420         # fill with defaults so that environments are shared
421         # with other spontaneus environments.  The rest of the
422         # arguments are optimizer, undefined, finalize, autoescape,
423         # loader, cache size and auto reloading setting
424         True, Undefined, None, False, None, 0, False
425     )
426
427     source = fileobj.read().decode(options.get('encoding', 'utf-8'))
428     try:
429         node = environment.parse(source)
430     except TemplateSyntaxError, e:
431         # skip templates with syntax errors
432         return
433     for lineno, func, message in extract_from_ast(node, keywords):
434         yield lineno, func, message, []
435
436
437 #: nicer import names
438 i18n = InternationalizationExtension
439 do = ExprStmtExtension
440 loopcontrols = LoopControlExtension