blob: e46287b5ddbf669c598d67a5705fd7996b6e7e91 [file] [log] [blame]
Armin Ronacher05530932008-04-20 13:27:49 +02001# -*- coding: utf-8 -*-
2"""
3 jinja2.ext
4 ~~~~~~~~~~
5
Armin Ronacherb5124e62008-04-25 00:36:14 +02006 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.
Armin Ronacher05530932008-04-20 13:27:49 +02009
Armin Ronacher55494e42010-01-22 09:41:48 +010010 :copyright: (c) 2010 by the Jinja Team.
Armin Ronacher05530932008-04-20 13:27:49 +020011 :license: BSD.
12"""
Armin Ronacherb5124e62008-04-25 00:36:14 +020013from collections import deque
Armin Ronacher05530932008-04-20 13:27:49 +020014from jinja2 import nodes
Armin Ronacher4f5008f2008-05-23 23:36:07 +020015from jinja2.defaults import *
Armin Ronacherb5124e62008-04-25 00:36:14 +020016from jinja2.environment import get_spontaneous_environment
Armin Ronacher2feed1d2008-04-26 16:26:52 +020017from jinja2.runtime import Undefined, concat
Benjamin Wieganda3152742008-04-28 18:07:52 +020018from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError
Armin Ronacherbd357722009-08-05 20:25:06 +020019from jinja2.utils import contextfunction, import_string, Markup, next
Armin Ronacherb5124e62008-04-25 00:36:14 +020020
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.
25GETTEXT_FUNCTIONS = ('_', 'gettext', 'ngettext')
Armin Ronacher05530932008-04-20 13:27:49 +020026
27
Armin Ronacher023b5e92008-05-08 11:03:10 +020028class ExtensionRegistry(type):
Armin Ronacher5c047ea2008-05-23 22:26:45 +020029 """Gives the extension an unique identifier."""
Armin Ronacher023b5e92008-05-08 11:03:10 +020030
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
Armin Ronacher05530932008-04-20 13:27:49 +020037class Extension(object):
Armin Ronacher7259c762008-04-30 13:03:59 +020038 """Extensions can be used to add extra functionality to the Jinja template
Armin Ronacher9d42abf2008-05-14 18:10:41 +020039 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.
Armin Ronacher762079c2008-05-08 23:57:56 +020043
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).
Armin Ronacher7259c762008-04-30 13:03:59 +020054 """
Armin Ronacher023b5e92008-05-08 11:03:10 +020055 __metaclass__ = ExtensionRegistry
Armin Ronacher05530932008-04-20 13:27:49 +020056
57 #: if this extension parses this is the list of tags it's listening to.
58 tags = set()
59
Armin Ronacher5b3f4dc2010-04-12 14:04:14 +020060 #: the priority of that extension. This is especially useful for
61 #: extensions that preprocess values. A lower value means higher
62 #: priority.
63 #:
64 #: .. versionadded:: 2.4
65 priority = 100
66
Armin Ronacher05530932008-04-20 13:27:49 +020067 def __init__(self, environment):
68 self.environment = environment
69
Armin Ronacher7259c762008-04-30 13:03:59 +020070 def bind(self, environment):
71 """Create a copy of this extension bound to another environment."""
72 rv = object.__new__(self.__class__)
73 rv.__dict__.update(self.__dict__)
74 rv.environment = environment
75 return rv
76
Armin Ronacher9ad96e72008-06-13 22:44:01 +020077 def preprocess(self, source, name, filename=None):
78 """This method is called before the actual lexing and can be used to
79 preprocess the source. The `filename` is optional. The return value
80 must be the preprocessed source.
81 """
82 return source
83
84 def filter_stream(self, stream):
85 """It's passed a :class:`~jinja2.lexer.TokenStream` that can be used
86 to filter tokens returned. This method has to return an iterable of
87 :class:`~jinja2.lexer.Token`\s, but it doesn't have to return a
88 :class:`~jinja2.lexer.TokenStream`.
Armin Ronacherd02fc7d2008-06-14 14:19:47 +020089
90 In the `ext` folder of the Jinja2 source distribution there is a file
91 called `inlinegettext.py` which implements a filter that utilizes this
92 method.
Armin Ronacher9ad96e72008-06-13 22:44:01 +020093 """
94 return stream
95
Armin Ronacher05530932008-04-20 13:27:49 +020096 def parse(self, parser):
Armin Ronacher023b5e92008-05-08 11:03:10 +020097 """If any of the :attr:`tags` matched this method is called with the
98 parser as first argument. The token the parser stream is pointing at
99 is the name token that matched. This method has to return one or a
100 list of multiple nodes.
101 """
Armin Ronacher27069d72008-05-11 19:48:12 +0200102 raise NotImplementedError()
Armin Ronacher023b5e92008-05-08 11:03:10 +0200103
104 def attr(self, name, lineno=None):
105 """Return an attribute node for the current extension. This is useful
Armin Ronacher69e12db2008-05-12 09:00:03 +0200106 to pass constants on extensions to generated template code::
Armin Ronacher023b5e92008-05-08 11:03:10 +0200107
Armin Ronacher69e12db2008-05-12 09:00:03 +0200108 self.attr('_my_attribute', lineno=lineno)
Armin Ronacher023b5e92008-05-08 11:03:10 +0200109 """
110 return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)
Armin Ronacher05530932008-04-20 13:27:49 +0200111
Armin Ronacher27069d72008-05-11 19:48:12 +0200112 def call_method(self, name, args=None, kwargs=None, dyn_args=None,
113 dyn_kwargs=None, lineno=None):
Armin Ronacher69e12db2008-05-12 09:00:03 +0200114 """Call a method of the extension. This is a shortcut for
115 :meth:`attr` + :class:`jinja2.nodes.Call`.
116 """
Armin Ronacher27069d72008-05-11 19:48:12 +0200117 if args is None:
118 args = []
119 if kwargs is None:
120 kwargs = []
121 return nodes.Call(self.attr(name, lineno=lineno), args, kwargs,
122 dyn_args, dyn_kwargs, lineno=lineno)
123
Armin Ronacher05530932008-04-20 13:27:49 +0200124
Armin Ronacher5c047ea2008-05-23 22:26:45 +0200125@contextfunction
Armin Ronacher4da90342010-05-29 17:35:10 +0200126def _gettext_alias(__context, *args, **kwargs):
Armin Ronacherb8892e72010-05-29 17:58:06 +0200127 return __context.call(__context.resolve('gettext'), *args, **kwargs)
Armin Ronacher4da90342010-05-29 17:35:10 +0200128
129
130def _make_new_gettext(func):
131 @contextfunction
132 def gettext(__context, __string, **variables):
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200133 rv = __context.call(func, __string)
Armin Ronacher4da90342010-05-29 17:35:10 +0200134 if __context.eval_ctx.autoescape:
135 rv = Markup(rv)
136 return rv % variables
137 return gettext
138
139
140def _make_new_ngettext(func):
141 @contextfunction
Armin Ronacherb98dad92010-05-29 22:31:17 +0200142 def ngettext(__context, __singular, __plural, __num, **variables):
143 variables.setdefault('num', __num)
144 rv = __context.call(func, __singular, __plural, __num)
Armin Ronacher4da90342010-05-29 17:35:10 +0200145 if __context.eval_ctx.autoescape:
146 rv = Markup(rv)
147 return rv % variables
148 return ngettext
Armin Ronacher5c047ea2008-05-23 22:26:45 +0200149
150
Armin Ronachered98cac2008-05-07 08:42:11 +0200151class InternationalizationExtension(Extension):
Armin Ronacher762079c2008-05-08 23:57:56 +0200152 """This extension adds gettext support to Jinja2."""
Armin Ronacherb5124e62008-04-25 00:36:14 +0200153 tags = set(['trans'])
154
Armin Ronacher4720c362008-09-06 16:15:38 +0200155 # TODO: the i18n extension is currently reevaluating values in a few
156 # situations. Take this example:
157 # {% trans count=something() %}{{ count }} foo{% pluralize
158 # %}{{ count }} fooss{% endtrans %}
159 # something is called twice here. One time for the gettext value and
160 # the other time for the n-parameter of the ngettext function.
161
Armin Ronacherb5124e62008-04-25 00:36:14 +0200162 def __init__(self, environment):
163 Extension.__init__(self, environment)
Armin Ronacher5c047ea2008-05-23 22:26:45 +0200164 environment.globals['_'] = _gettext_alias
Armin Ronacher762079c2008-05-08 23:57:56 +0200165 environment.extend(
166 install_gettext_translations=self._install,
167 install_null_translations=self._install_null,
Armin Ronacher4da90342010-05-29 17:35:10 +0200168 install_gettext_callables=self._install_callables,
Armin Ronacher762079c2008-05-08 23:57:56 +0200169 uninstall_gettext_translations=self._uninstall,
Armin Ronacher4da90342010-05-29 17:35:10 +0200170 extract_translations=self._extract,
171 newstyle_gettext=False
Armin Ronacher762079c2008-05-08 23:57:56 +0200172 )
173
Armin Ronacher4da90342010-05-29 17:35:10 +0200174 def _install(self, translations, newstyle=None):
Armin Ronacher32133552008-09-15 14:35:01 +0200175 gettext = getattr(translations, 'ugettext', None)
176 if gettext is None:
177 gettext = translations.gettext
178 ngettext = getattr(translations, 'ungettext', None)
179 if ngettext is None:
180 ngettext = translations.ngettext
Armin Ronacher4da90342010-05-29 17:35:10 +0200181 self._install_callables(gettext, ngettext, newstyle)
Armin Ronacher762079c2008-05-08 23:57:56 +0200182
Armin Ronacher4da90342010-05-29 17:35:10 +0200183 def _install_null(self, newstyle=None):
184 self._install_callables(
185 lambda x: x,
186 lambda s, p, n: (n != 1 and (p,) or (s,))[0],
187 newstyle
188 )
189
190 def _install_callables(self, gettext, ngettext, newstyle=None):
191 if newstyle is not None:
192 self.environment.newstyle_gettext = newstyle
193 if self.environment.newstyle_gettext:
194 gettext = _make_new_gettext(gettext)
195 ngettext = _make_new_ngettext(ngettext)
Armin Ronacher762079c2008-05-08 23:57:56 +0200196 self.environment.globals.update(
Armin Ronacher4da90342010-05-29 17:35:10 +0200197 gettext=gettext,
198 ngettext=ngettext
Armin Ronacher762079c2008-05-08 23:57:56 +0200199 )
200
201 def _uninstall(self, translations):
202 for key in 'gettext', 'ngettext':
203 self.environment.globals.pop(key, None)
204
205 def _extract(self, source, gettext_functions=GETTEXT_FUNCTIONS):
206 if isinstance(source, basestring):
207 source = self.environment.parse(source)
208 return extract_from_ast(source, gettext_functions)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200209
210 def parse(self, parser):
211 """Parse a translatable tag."""
Armin Ronacherbd357722009-08-05 20:25:06 +0200212 lineno = next(parser.stream).lineno
Armin Ronacherb98dad92010-05-29 22:31:17 +0200213 num_called_num = False
Armin Ronacherb5124e62008-04-25 00:36:14 +0200214
Armin Ronacherb5124e62008-04-25 00:36:14 +0200215 # find all the variables referenced. Additionally a variable can be
216 # defined in the body of the trans block too, but this is checked at
217 # a later state.
218 plural_expr = None
219 variables = {}
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100220 while parser.stream.current.type != 'block_end':
Armin Ronacherb5124e62008-04-25 00:36:14 +0200221 if variables:
222 parser.stream.expect('comma')
Armin Ronacher023b5e92008-05-08 11:03:10 +0200223
224 # skip colon for python compatibility
Armin Ronacherfdf95302008-05-11 22:20:51 +0200225 if parser.stream.skip_if('colon'):
Armin Ronacher023b5e92008-05-08 11:03:10 +0200226 break
227
Armin Ronacherb5124e62008-04-25 00:36:14 +0200228 name = parser.stream.expect('name')
229 if name.value in variables:
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200230 parser.fail('translatable variable %r defined twice.' %
231 name.value, name.lineno,
232 exc=TemplateAssertionError)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200233
234 # expressions
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100235 if parser.stream.current.type == 'assign':
Armin Ronacherbd357722009-08-05 20:25:06 +0200236 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200237 variables[name.value] = var = parser.parse_expression()
238 else:
239 variables[name.value] = var = nodes.Name(name.value, 'load')
Armin Ronacherb98dad92010-05-29 22:31:17 +0200240
Armin Ronacherb5124e62008-04-25 00:36:14 +0200241 if plural_expr is None:
242 plural_expr = var
Armin Ronacherb98dad92010-05-29 22:31:17 +0200243 num_called_num = name.value == 'num'
Armin Ronacher023b5e92008-05-08 11:03:10 +0200244
Armin Ronacherb5124e62008-04-25 00:36:14 +0200245 parser.stream.expect('block_end')
246
247 plural = plural_names = None
248 have_plural = False
249 referenced = set()
250
251 # now parse until endtrans or pluralize
252 singular_names, singular = self._parse_block(parser, True)
253 if singular_names:
254 referenced.update(singular_names)
255 if plural_expr is None:
256 plural_expr = nodes.Name(singular_names[0], 'load')
Armin Ronacherb98dad92010-05-29 22:31:17 +0200257 num_called_num = singular_names[0] == 'num'
Armin Ronacherb5124e62008-04-25 00:36:14 +0200258
259 # if we have a pluralize block, we parse that too
260 if parser.stream.current.test('name:pluralize'):
261 have_plural = True
Armin Ronacherbd357722009-08-05 20:25:06 +0200262 next(parser.stream)
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100263 if parser.stream.current.type != 'block_end':
Armin Ronacher4720c362008-09-06 16:15:38 +0200264 name = parser.stream.expect('name')
265 if name.value not in variables:
266 parser.fail('unknown variable %r for pluralization' %
267 name.value, name.lineno,
268 exc=TemplateAssertionError)
269 plural_expr = variables[name.value]
Armin Ronacherb98dad92010-05-29 22:31:17 +0200270 num_called_num = name.value == 'num'
Armin Ronacherb5124e62008-04-25 00:36:14 +0200271 parser.stream.expect('block_end')
272 plural_names, plural = self._parse_block(parser, False)
Armin Ronacherbd357722009-08-05 20:25:06 +0200273 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200274 referenced.update(plural_names)
275 else:
Armin Ronacherbd357722009-08-05 20:25:06 +0200276 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200277
278 # register free names as simple name expressions
279 for var in referenced:
280 if var not in variables:
281 variables[var] = nodes.Name(var, 'load')
282
Armin Ronacherb5124e62008-04-25 00:36:14 +0200283 if not have_plural:
284 plural_expr = None
285 elif plural_expr is None:
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200286 parser.fail('pluralize without variables', lineno)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200287
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200288 node = self._make_node(singular, plural, variables, plural_expr,
Armin Ronacherb98dad92010-05-29 22:31:17 +0200289 bool(referenced), num_called_num)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200290 node.set_lineno(lineno)
291 return node
292
293 def _parse_block(self, parser, allow_pluralize):
294 """Parse until the next block tag with a given name."""
295 referenced = []
296 buf = []
297 while 1:
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100298 if parser.stream.current.type == 'data':
Armin Ronacherb5124e62008-04-25 00:36:14 +0200299 buf.append(parser.stream.current.value.replace('%', '%%'))
Armin Ronacherbd357722009-08-05 20:25:06 +0200300 next(parser.stream)
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100301 elif parser.stream.current.type == 'variable_begin':
Armin Ronacherbd357722009-08-05 20:25:06 +0200302 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200303 name = parser.stream.expect('name').value
304 referenced.append(name)
305 buf.append('%%(%s)s' % name)
306 parser.stream.expect('variable_end')
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100307 elif parser.stream.current.type == 'block_begin':
Armin Ronacherbd357722009-08-05 20:25:06 +0200308 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200309 if parser.stream.current.test('name:endtrans'):
310 break
311 elif parser.stream.current.test('name:pluralize'):
312 if allow_pluralize:
313 break
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200314 parser.fail('a translatable section can have only one '
315 'pluralize section')
316 parser.fail('control structures in translatable sections are '
317 'not allowed')
Armin Ronacherd02fc7d2008-06-14 14:19:47 +0200318 elif parser.stream.eos:
319 parser.fail('unclosed translation block')
Armin Ronacherb5124e62008-04-25 00:36:14 +0200320 else:
321 assert False, 'internal parser error'
322
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200323 return referenced, concat(buf)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200324
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200325 def _make_node(self, singular, plural, variables, plural_expr,
Armin Ronacherb98dad92010-05-29 22:31:17 +0200326 vars_referenced, num_called_num):
Armin Ronacherb5124e62008-04-25 00:36:14 +0200327 """Generates a useful node from the data provided."""
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200328 # no variables referenced? no need to escape for old style
329 # gettext invocations
330 if not vars_referenced and not self.environment.newstyle_gettext:
331 singular = singular.replace('%%', '%')
332 if plural:
333 plural = plural.replace('%%', '%')
334
Armin Ronacherb5124e62008-04-25 00:36:14 +0200335 # singular only:
336 if plural_expr is None:
337 gettext = nodes.Name('gettext', 'load')
338 node = nodes.Call(gettext, [nodes.Const(singular)],
339 [], None, None)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200340
341 # singular and plural
342 else:
343 ngettext = nodes.Name('ngettext', 'load')
344 node = nodes.Call(ngettext, [
345 nodes.Const(singular),
346 nodes.Const(plural),
347 plural_expr
348 ], [], None, None)
Armin Ronacherd84ec462008-04-29 13:43:16 +0200349
Armin Ronacher4da90342010-05-29 17:35:10 +0200350 # in case newstyle gettext is used, the method is powerful
351 # enough to handle the variable expansion and autoescape
352 # handling itself
353 if self.environment.newstyle_gettext:
Armin Ronacherb8892e72010-05-29 17:58:06 +0200354 for key, value in variables.iteritems():
Armin Ronacherb98dad92010-05-29 22:31:17 +0200355 # the function adds that later anyways in case num was
356 # called num, so just skip it.
357 if num_called_num and key == 'num':
358 continue
Armin Ronacherb8892e72010-05-29 17:58:06 +0200359 node.kwargs.append(nodes.Keyword(key, value))
Armin Ronacherd84ec462008-04-29 13:43:16 +0200360
Armin Ronacher4da90342010-05-29 17:35:10 +0200361 # otherwise do that here
362 else:
363 # mark the return value as safe if we are in an
364 # environment with autoescaping turned on
365 node = nodes.MarkSafeIfAutoescape(node)
366 if variables:
Armin Ronacherb8892e72010-05-29 17:58:06 +0200367 node = nodes.Mod(node, nodes.Dict([
368 nodes.Pair(nodes.Const(key), value)
369 for key, value in variables.items()
370 ]))
Armin Ronacherb5124e62008-04-25 00:36:14 +0200371 return nodes.Output([node])
372
373
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200374class ExprStmtExtension(Extension):
375 """Adds a `do` tag to Jinja2 that works like the print statement just
376 that it doesn't print the return value.
377 """
378 tags = set(['do'])
379
380 def parse(self, parser):
Armin Ronacherbd357722009-08-05 20:25:06 +0200381 node = nodes.ExprStmt(lineno=next(parser.stream).lineno)
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200382 node.node = parser.parse_tuple()
383 return node
384
385
Armin Ronacher3da90312008-05-23 16:37:28 +0200386class LoopControlExtension(Extension):
387 """Adds break and continue to the template engine."""
388 tags = set(['break', 'continue'])
389
390 def parse(self, parser):
Armin Ronacherbd357722009-08-05 20:25:06 +0200391 token = next(parser.stream)
Armin Ronacher3da90312008-05-23 16:37:28 +0200392 if token.value == 'break':
393 return nodes.Break(lineno=token.lineno)
394 return nodes.Continue(lineno=token.lineno)
395
396
Armin Ronacher9b4cc9f2010-02-07 03:55:15 +0100397class WithExtension(Extension):
398 """Adds support for a django-like with block."""
399 tags = set(['with'])
400
401 def parse(self, parser):
402 node = nodes.Scope(lineno=next(parser.stream).lineno)
403 assignments = []
404 while parser.stream.current.type != 'block_end':
405 lineno = parser.stream.current.lineno
406 if assignments:
407 parser.stream.expect('comma')
408 target = parser.parse_assign_target()
409 parser.stream.expect('assign')
410 expr = parser.parse_expression()
411 assignments.append(nodes.Assign(target, expr, lineno=lineno))
412 node.body = assignments + \
413 list(parser.parse_statements(('name:endwith',),
414 drop_needle=True))
415 return node
416
417
Armin Ronacher8346bd72010-03-14 19:43:47 +0100418class AutoEscapeExtension(Extension):
419 """Changes auto escape rules for a scope."""
420 tags = set(['autoescape'])
421
422 def parse(self, parser):
423 node = nodes.ScopedEvalContextModifier(lineno=next(parser.stream).lineno)
424 node.options = [
425 nodes.Keyword('autoescape', parser.parse_expression())
426 ]
427 node.body = parser.parse_statements(('name:endautoescape',),
428 drop_needle=True)
429 return nodes.Scope([node])
430
431
Armin Ronacherabd36572008-06-27 08:45:19 +0200432def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,
433 babel_style=True):
434 """Extract localizable strings from the given template node. Per
435 default this function returns matches in babel style that means non string
436 parameters as well as keyword arguments are returned as `None`. This
437 allows Babel to figure out what you really meant if you are using
438 gettext functions that allow keyword arguments for placeholder expansion.
439 If you don't want that behavior set the `babel_style` parameter to `False`
440 which causes only strings to be returned and parameters are always stored
441 in tuples. As a consequence invalid gettext calls (calls without a single
442 string parameter or string parameters after non-string parameters) are
443 skipped.
444
445 This example explains the behavior:
446
447 >>> from jinja2 import Environment
448 >>> env = Environment()
449 >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')
450 >>> list(extract_from_ast(node))
451 [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]
452 >>> list(extract_from_ast(node, babel_style=False))
453 [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]
Armin Ronacherb5124e62008-04-25 00:36:14 +0200454
455 For every string found this function yields a ``(lineno, function,
456 message)`` tuple, where:
457
458 * ``lineno`` is the number of the line on which the string was found,
459 * ``function`` is the name of the ``gettext`` function used (if the
460 string was extracted from embedded Python code), and
461 * ``message`` is the string itself (a ``unicode`` object, or a tuple
462 of ``unicode`` objects for functions with multiple string arguments).
Armin Ronacher531578d2010-02-06 16:34:54 +0100463
464 This extraction function operates on the AST and is because of that unable
465 to extract any comments. For comment support you have to use the babel
466 extraction interface or extract comments yourself.
Armin Ronacherb5124e62008-04-25 00:36:14 +0200467 """
468 for node in node.find_all(nodes.Call):
469 if not isinstance(node.node, nodes.Name) or \
470 node.node.name not in gettext_functions:
471 continue
472
473 strings = []
474 for arg in node.args:
475 if isinstance(arg, nodes.Const) and \
476 isinstance(arg.value, basestring):
477 strings.append(arg.value)
478 else:
479 strings.append(None)
480
Armin Ronacherabd36572008-06-27 08:45:19 +0200481 for arg in node.kwargs:
482 strings.append(None)
483 if node.dyn_args is not None:
484 strings.append(None)
485 if node.dyn_kwargs is not None:
486 strings.append(None)
487
488 if not babel_style:
489 strings = tuple(x for x in strings if x is not None)
490 if not strings:
491 continue
Armin Ronacherb5124e62008-04-25 00:36:14 +0200492 else:
Armin Ronacherabd36572008-06-27 08:45:19 +0200493 if len(strings) == 1:
494 strings = strings[0]
495 else:
496 strings = tuple(strings)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200497 yield node.lineno, node.node.name, strings
498
499
Armin Ronacher531578d2010-02-06 16:34:54 +0100500class _CommentFinder(object):
501 """Helper class to find comments in a token stream. Can only
502 find comments for gettext calls forwards. Once the comment
503 from line 4 is found, a comment for line 1 will not return a
504 usable value.
505 """
506
507 def __init__(self, tokens, comment_tags):
508 self.tokens = tokens
509 self.comment_tags = comment_tags
510 self.offset = 0
511 self.last_lineno = 0
512
513 def find_backwards(self, offset):
514 try:
515 for _, token_type, token_value in \
516 reversed(self.tokens[self.offset:offset]):
517 if token_type in ('comment', 'linecomment'):
518 try:
519 prefix, comment = token_value.split(None, 1)
520 except ValueError:
521 continue
522 if prefix in self.comment_tags:
523 return [comment.rstrip()]
524 return []
525 finally:
526 self.offset = offset
527
528 def find_comments(self, lineno):
529 if not self.comment_tags or self.last_lineno > lineno:
530 return []
531 for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset:]):
532 if token_lineno > lineno:
533 return self.find_backwards(self.offset + idx)
534 return self.find_backwards(len(self.tokens))
535
536
Armin Ronacherb5124e62008-04-25 00:36:14 +0200537def babel_extract(fileobj, keywords, comment_tags, options):
538 """Babel extraction method for Jinja templates.
539
Armin Ronacher531578d2010-02-06 16:34:54 +0100540 .. versionchanged:: 2.3
541 Basic support for translation comments was added. If `comment_tags`
542 is now set to a list of keywords for extraction, the extractor will
543 try to find the best preceeding comment that begins with one of the
544 keywords. For best results, make sure to not have more than one
545 gettext call in one line of code and the matching comment in the
546 same line or the line before.
547
Armin Ronacherb5124e62008-04-25 00:36:14 +0200548 :param fileobj: the file-like object the messages should be extracted from
549 :param keywords: a list of keywords (i.e. function names) that should be
550 recognized as translation functions
551 :param comment_tags: a list of translator tags to search for and include
Armin Ronacher531578d2010-02-06 16:34:54 +0100552 in the results.
Armin Ronacherb5124e62008-04-25 00:36:14 +0200553 :param options: a dictionary of additional options (optional)
554 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
555 (comments will be empty currently)
556 """
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200557 extensions = set()
Armin Ronacherb5124e62008-04-25 00:36:14 +0200558 for extension in options.get('extensions', '').split(','):
559 extension = extension.strip()
560 if not extension:
561 continue
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200562 extensions.add(import_string(extension))
563 if InternationalizationExtension not in extensions:
564 extensions.add(InternationalizationExtension)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200565
566 environment = get_spontaneous_environment(
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200567 options.get('block_start_string', BLOCK_START_STRING),
568 options.get('block_end_string', BLOCK_END_STRING),
569 options.get('variable_start_string', VARIABLE_START_STRING),
570 options.get('variable_end_string', VARIABLE_END_STRING),
571 options.get('comment_start_string', COMMENT_START_STRING),
572 options.get('comment_end_string', COMMENT_END_STRING),
573 options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,
Armin Ronacher59b6bd52009-03-30 21:00:16 +0200574 options.get('line_comment_prefix') or LINE_COMMENT_PREFIX,
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200575 str(options.get('trim_blocks', TRIM_BLOCKS)).lower() in \
576 ('1', 'on', 'yes', 'true'),
577 NEWLINE_SEQUENCE, frozenset(extensions),
Armin Ronacherb5124e62008-04-25 00:36:14 +0200578 # fill with defaults so that environments are shared
Armin Ronacher7259c762008-04-30 13:03:59 +0200579 # with other spontaneus environments. The rest of the
580 # arguments are optimizer, undefined, finalize, autoescape,
Armin Ronacher4d5bdff2008-09-17 16:19:46 +0200581 # loader, cache size, auto reloading setting and the
582 # bytecode cache
583 True, Undefined, None, False, None, 0, False, None
Armin Ronacherb5124e62008-04-25 00:36:14 +0200584 )
585
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200586 source = fileobj.read().decode(options.get('encoding', 'utf-8'))
Armin Ronacherc670b112008-06-29 17:23:04 +0200587 try:
588 node = environment.parse(source)
Armin Ronacher531578d2010-02-06 16:34:54 +0100589 tokens = list(environment.lex(environment.preprocess(source)))
Armin Ronacherc670b112008-06-29 17:23:04 +0200590 except TemplateSyntaxError, e:
591 # skip templates with syntax errors
592 return
Armin Ronacher531578d2010-02-06 16:34:54 +0100593
594 finder = _CommentFinder(tokens, comment_tags)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200595 for lineno, func, message in extract_from_ast(node, keywords):
Armin Ronacher531578d2010-02-06 16:34:54 +0100596 yield lineno, func, message, finder.find_comments(lineno)
Armin Ronachered98cac2008-05-07 08:42:11 +0200597
598
599#: nicer import names
600i18n = InternationalizationExtension
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200601do = ExprStmtExtension
Armin Ronacher3da90312008-05-23 16:37:28 +0200602loopcontrols = LoopControlExtension
Armin Ronacher9b4cc9f2010-02-07 03:55:15 +0100603with_ = WithExtension
Armin Ronacher8346bd72010-03-14 19:43:47 +0100604autoescape = AutoEscapeExtension