blob: a88c088dba3c241887e85f785b7bc83553939d0b [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"""
13from jinja2 import nodes
Armin Ronachera65f1eb2013-05-19 11:18:19 +010014from jinja2.defaults import BLOCK_START_STRING, \
15 BLOCK_END_STRING, VARIABLE_START_STRING, VARIABLE_END_STRING, \
16 COMMENT_START_STRING, COMMENT_END_STRING, LINE_STATEMENT_PREFIX, \
17 LINE_COMMENT_PREFIX, TRIM_BLOCKS, NEWLINE_SEQUENCE, \
18 KEEP_TRAILING_NEWLINE, LSTRIP_BLOCKS
Armin Ronacher4f77a302010-07-01 12:15:39 +020019from jinja2.environment import Environment
Daniel Neuhäuserd0708db2013-05-18 12:52:40 +020020from jinja2.runtime import concat
Benjamin Wieganda3152742008-04-28 18:07:52 +020021from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError
Thomas Waldmann7d295622013-05-18 00:06:22 +020022from jinja2.utils import contextfunction, import_string, Markup
Armin Ronachere9098672013-05-19 14:16:13 +010023from jinja2._compat import next, with_metaclass, string_types, iteritems
Armin Ronacherb5124e62008-04-25 00:36:14 +020024
25
26# the only real useful gettext functions for a Jinja template. Note
27# that ugettext must be assigned to gettext as Jinja doesn't support
28# non unicode strings.
29GETTEXT_FUNCTIONS = ('_', 'gettext', 'ngettext')
Armin Ronacher05530932008-04-20 13:27:49 +020030
31
Armin Ronacher023b5e92008-05-08 11:03:10 +020032class ExtensionRegistry(type):
Armin Ronacher5c047ea2008-05-23 22:26:45 +020033 """Gives the extension an unique identifier."""
Armin Ronacher023b5e92008-05-08 11:03:10 +020034
35 def __new__(cls, name, bases, d):
36 rv = type.__new__(cls, name, bases, d)
37 rv.identifier = rv.__module__ + '.' + rv.__name__
38 return rv
39
40
Armin Ronachere9098672013-05-19 14:16:13 +010041class Extension(with_metaclass(ExtensionRegistry, object)):
Armin Ronacher7259c762008-04-30 13:03:59 +020042 """Extensions can be used to add extra functionality to the Jinja template
Armin Ronacher9d42abf2008-05-14 18:10:41 +020043 system at the parser level. Custom extensions are bound to an environment
44 but may not store environment specific data on `self`. The reason for
45 this is that an extension can be bound to another environment (for
46 overlays) by creating a copy and reassigning the `environment` attribute.
Armin Ronacher762079c2008-05-08 23:57:56 +020047
48 As extensions are created by the environment they cannot accept any
49 arguments for configuration. One may want to work around that by using
50 a factory function, but that is not possible as extensions are identified
51 by their import name. The correct way to configure the extension is
52 storing the configuration values on the environment. Because this way the
53 environment ends up acting as central configuration storage the
54 attributes may clash which is why extensions have to ensure that the names
55 they choose for configuration are not too generic. ``prefix`` for example
56 is a terrible name, ``fragment_cache_prefix`` on the other hand is a good
57 name as includes the name of the extension (fragment cache).
Armin Ronacher7259c762008-04-30 13:03:59 +020058 """
Armin Ronacher023b5e92008-05-08 11:03:10 +020059 __metaclass__ = ExtensionRegistry
Armin Ronacher05530932008-04-20 13:27:49 +020060
61 #: if this extension parses this is the list of tags it's listening to.
62 tags = set()
63
Armin Ronacher5b3f4dc2010-04-12 14:04:14 +020064 #: the priority of that extension. This is especially useful for
65 #: extensions that preprocess values. A lower value means higher
66 #: priority.
67 #:
68 #: .. versionadded:: 2.4
69 priority = 100
70
Armin Ronacher05530932008-04-20 13:27:49 +020071 def __init__(self, environment):
72 self.environment = environment
73
Armin Ronacher7259c762008-04-30 13:03:59 +020074 def bind(self, environment):
75 """Create a copy of this extension bound to another environment."""
76 rv = object.__new__(self.__class__)
77 rv.__dict__.update(self.__dict__)
78 rv.environment = environment
79 return rv
80
Armin Ronacher9ad96e72008-06-13 22:44:01 +020081 def preprocess(self, source, name, filename=None):
82 """This method is called before the actual lexing and can be used to
83 preprocess the source. The `filename` is optional. The return value
84 must be the preprocessed source.
85 """
86 return source
87
88 def filter_stream(self, stream):
89 """It's passed a :class:`~jinja2.lexer.TokenStream` that can be used
90 to filter tokens returned. This method has to return an iterable of
91 :class:`~jinja2.lexer.Token`\s, but it doesn't have to return a
92 :class:`~jinja2.lexer.TokenStream`.
Armin Ronacherd02fc7d2008-06-14 14:19:47 +020093
94 In the `ext` folder of the Jinja2 source distribution there is a file
95 called `inlinegettext.py` which implements a filter that utilizes this
96 method.
Armin Ronacher9ad96e72008-06-13 22:44:01 +020097 """
98 return stream
99
Armin Ronacher05530932008-04-20 13:27:49 +0200100 def parse(self, parser):
Armin Ronacher023b5e92008-05-08 11:03:10 +0200101 """If any of the :attr:`tags` matched this method is called with the
102 parser as first argument. The token the parser stream is pointing at
103 is the name token that matched. This method has to return one or a
104 list of multiple nodes.
105 """
Armin Ronacher27069d72008-05-11 19:48:12 +0200106 raise NotImplementedError()
Armin Ronacher023b5e92008-05-08 11:03:10 +0200107
108 def attr(self, name, lineno=None):
109 """Return an attribute node for the current extension. This is useful
Armin Ronacher53278a32011-01-24 01:16:00 +0100110 to pass constants on extensions to generated template code.
111
112 ::
Armin Ronacher023b5e92008-05-08 11:03:10 +0200113
Armin Ronacher69e12db2008-05-12 09:00:03 +0200114 self.attr('_my_attribute', lineno=lineno)
Armin Ronacher023b5e92008-05-08 11:03:10 +0200115 """
116 return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)
Armin Ronacher05530932008-04-20 13:27:49 +0200117
Armin Ronacher27069d72008-05-11 19:48:12 +0200118 def call_method(self, name, args=None, kwargs=None, dyn_args=None,
119 dyn_kwargs=None, lineno=None):
Armin Ronacher69e12db2008-05-12 09:00:03 +0200120 """Call a method of the extension. This is a shortcut for
121 :meth:`attr` + :class:`jinja2.nodes.Call`.
122 """
Armin Ronacher27069d72008-05-11 19:48:12 +0200123 if args is None:
124 args = []
125 if kwargs is None:
126 kwargs = []
127 return nodes.Call(self.attr(name, lineno=lineno), args, kwargs,
128 dyn_args, dyn_kwargs, lineno=lineno)
129
Armin Ronacher05530932008-04-20 13:27:49 +0200130
Armin Ronacher5c047ea2008-05-23 22:26:45 +0200131@contextfunction
Armin Ronacher4da90342010-05-29 17:35:10 +0200132def _gettext_alias(__context, *args, **kwargs):
Armin Ronacherb8892e72010-05-29 17:58:06 +0200133 return __context.call(__context.resolve('gettext'), *args, **kwargs)
Armin Ronacher4da90342010-05-29 17:35:10 +0200134
135
136def _make_new_gettext(func):
137 @contextfunction
138 def gettext(__context, __string, **variables):
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200139 rv = __context.call(func, __string)
Armin Ronacher4da90342010-05-29 17:35:10 +0200140 if __context.eval_ctx.autoescape:
141 rv = Markup(rv)
142 return rv % variables
143 return gettext
144
145
146def _make_new_ngettext(func):
147 @contextfunction
Armin Ronacherb98dad92010-05-29 22:31:17 +0200148 def ngettext(__context, __singular, __plural, __num, **variables):
149 variables.setdefault('num', __num)
150 rv = __context.call(func, __singular, __plural, __num)
Armin Ronacher4da90342010-05-29 17:35:10 +0200151 if __context.eval_ctx.autoescape:
152 rv = Markup(rv)
153 return rv % variables
154 return ngettext
Armin Ronacher5c047ea2008-05-23 22:26:45 +0200155
156
Armin Ronachered98cac2008-05-07 08:42:11 +0200157class InternationalizationExtension(Extension):
Armin Ronacher762079c2008-05-08 23:57:56 +0200158 """This extension adds gettext support to Jinja2."""
Armin Ronacherb5124e62008-04-25 00:36:14 +0200159 tags = set(['trans'])
160
Armin Ronacher4720c362008-09-06 16:15:38 +0200161 # TODO: the i18n extension is currently reevaluating values in a few
162 # situations. Take this example:
163 # {% trans count=something() %}{{ count }} foo{% pluralize
164 # %}{{ count }} fooss{% endtrans %}
165 # something is called twice here. One time for the gettext value and
166 # the other time for the n-parameter of the ngettext function.
167
Armin Ronacherb5124e62008-04-25 00:36:14 +0200168 def __init__(self, environment):
169 Extension.__init__(self, environment)
Armin Ronacher5c047ea2008-05-23 22:26:45 +0200170 environment.globals['_'] = _gettext_alias
Armin Ronacher762079c2008-05-08 23:57:56 +0200171 environment.extend(
172 install_gettext_translations=self._install,
173 install_null_translations=self._install_null,
Armin Ronacher4da90342010-05-29 17:35:10 +0200174 install_gettext_callables=self._install_callables,
Armin Ronacher762079c2008-05-08 23:57:56 +0200175 uninstall_gettext_translations=self._uninstall,
Armin Ronacher4da90342010-05-29 17:35:10 +0200176 extract_translations=self._extract,
177 newstyle_gettext=False
Armin Ronacher762079c2008-05-08 23:57:56 +0200178 )
179
Armin Ronacher4da90342010-05-29 17:35:10 +0200180 def _install(self, translations, newstyle=None):
Armin Ronacher32133552008-09-15 14:35:01 +0200181 gettext = getattr(translations, 'ugettext', None)
182 if gettext is None:
183 gettext = translations.gettext
184 ngettext = getattr(translations, 'ungettext', None)
185 if ngettext is None:
186 ngettext = translations.ngettext
Armin Ronacher4da90342010-05-29 17:35:10 +0200187 self._install_callables(gettext, ngettext, newstyle)
Armin Ronacher762079c2008-05-08 23:57:56 +0200188
Armin Ronacher4da90342010-05-29 17:35:10 +0200189 def _install_null(self, newstyle=None):
190 self._install_callables(
191 lambda x: x,
192 lambda s, p, n: (n != 1 and (p,) or (s,))[0],
193 newstyle
194 )
195
196 def _install_callables(self, gettext, ngettext, newstyle=None):
197 if newstyle is not None:
198 self.environment.newstyle_gettext = newstyle
199 if self.environment.newstyle_gettext:
200 gettext = _make_new_gettext(gettext)
201 ngettext = _make_new_ngettext(ngettext)
Armin Ronacher762079c2008-05-08 23:57:56 +0200202 self.environment.globals.update(
Armin Ronacher4da90342010-05-29 17:35:10 +0200203 gettext=gettext,
204 ngettext=ngettext
Armin Ronacher762079c2008-05-08 23:57:56 +0200205 )
206
207 def _uninstall(self, translations):
208 for key in 'gettext', 'ngettext':
209 self.environment.globals.pop(key, None)
210
211 def _extract(self, source, gettext_functions=GETTEXT_FUNCTIONS):
Armin Ronachere9098672013-05-19 14:16:13 +0100212 if isinstance(source, string_types):
Armin Ronacher762079c2008-05-08 23:57:56 +0200213 source = self.environment.parse(source)
214 return extract_from_ast(source, gettext_functions)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200215
216 def parse(self, parser):
217 """Parse a translatable tag."""
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100218 lineno = next(parser.stream).lineno
Armin Ronacherb98dad92010-05-29 22:31:17 +0200219 num_called_num = False
Armin Ronacherb5124e62008-04-25 00:36:14 +0200220
Armin Ronacherb5124e62008-04-25 00:36:14 +0200221 # find all the variables referenced. Additionally a variable can be
222 # defined in the body of the trans block too, but this is checked at
223 # a later state.
224 plural_expr = None
Florian Apolloner79c84752012-01-18 17:47:54 +0100225 plural_expr_assignment = None
Armin Ronacherb5124e62008-04-25 00:36:14 +0200226 variables = {}
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100227 while parser.stream.current.type != 'block_end':
Armin Ronacherb5124e62008-04-25 00:36:14 +0200228 if variables:
229 parser.stream.expect('comma')
Armin Ronacher023b5e92008-05-08 11:03:10 +0200230
231 # skip colon for python compatibility
Armin Ronacherfdf95302008-05-11 22:20:51 +0200232 if parser.stream.skip_if('colon'):
Armin Ronacher023b5e92008-05-08 11:03:10 +0200233 break
234
Armin Ronacherb5124e62008-04-25 00:36:14 +0200235 name = parser.stream.expect('name')
236 if name.value in variables:
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200237 parser.fail('translatable variable %r defined twice.' %
238 name.value, name.lineno,
239 exc=TemplateAssertionError)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200240
241 # expressions
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100242 if parser.stream.current.type == 'assign':
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100243 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200244 variables[name.value] = var = parser.parse_expression()
245 else:
246 variables[name.value] = var = nodes.Name(name.value, 'load')
Armin Ronacherb98dad92010-05-29 22:31:17 +0200247
Armin Ronacherb5124e62008-04-25 00:36:14 +0200248 if plural_expr is None:
Florian Apolloner5a25a472012-01-18 17:08:48 +0100249 if isinstance(var, nodes.Call):
Florian Apolloner79c84752012-01-18 17:47:54 +0100250 plural_expr = nodes.Name('_trans', 'load')
251 variables[name.value] = plural_expr
252 plural_expr_assignment = nodes.Assign(
253 nodes.Name('_trans', 'store'), var)
Florian Apolloner5a25a472012-01-18 17:08:48 +0100254 else:
255 plural_expr = var
Armin Ronacherb98dad92010-05-29 22:31:17 +0200256 num_called_num = name.value == 'num'
Armin Ronacher023b5e92008-05-08 11:03:10 +0200257
Armin Ronacherb5124e62008-04-25 00:36:14 +0200258 parser.stream.expect('block_end')
259
260 plural = plural_names = None
261 have_plural = False
262 referenced = set()
263
264 # now parse until endtrans or pluralize
265 singular_names, singular = self._parse_block(parser, True)
266 if singular_names:
267 referenced.update(singular_names)
268 if plural_expr is None:
269 plural_expr = nodes.Name(singular_names[0], 'load')
Armin Ronacherb98dad92010-05-29 22:31:17 +0200270 num_called_num = singular_names[0] == 'num'
Armin Ronacherb5124e62008-04-25 00:36:14 +0200271
272 # if we have a pluralize block, we parse that too
273 if parser.stream.current.test('name:pluralize'):
274 have_plural = True
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100275 next(parser.stream)
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100276 if parser.stream.current.type != 'block_end':
Armin Ronacher4720c362008-09-06 16:15:38 +0200277 name = parser.stream.expect('name')
278 if name.value not in variables:
279 parser.fail('unknown variable %r for pluralization' %
280 name.value, name.lineno,
281 exc=TemplateAssertionError)
282 plural_expr = variables[name.value]
Armin Ronacherb98dad92010-05-29 22:31:17 +0200283 num_called_num = name.value == 'num'
Armin Ronacherb5124e62008-04-25 00:36:14 +0200284 parser.stream.expect('block_end')
285 plural_names, plural = self._parse_block(parser, False)
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100286 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200287 referenced.update(plural_names)
288 else:
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100289 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200290
291 # register free names as simple name expressions
292 for var in referenced:
293 if var not in variables:
294 variables[var] = nodes.Name(var, 'load')
295
Armin Ronacherb5124e62008-04-25 00:36:14 +0200296 if not have_plural:
297 plural_expr = None
298 elif plural_expr is None:
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200299 parser.fail('pluralize without variables', lineno)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200300
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200301 node = self._make_node(singular, plural, variables, plural_expr,
Armin Ronacher4f77a302010-07-01 12:15:39 +0200302 bool(referenced),
303 num_called_num and have_plural)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200304 node.set_lineno(lineno)
Florian Apolloner79c84752012-01-18 17:47:54 +0100305 if plural_expr_assignment is not None:
306 return [plural_expr_assignment, node]
307 else:
308 return node
Armin Ronacherb5124e62008-04-25 00:36:14 +0200309
310 def _parse_block(self, parser, allow_pluralize):
311 """Parse until the next block tag with a given name."""
312 referenced = []
313 buf = []
314 while 1:
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100315 if parser.stream.current.type == 'data':
Armin Ronacherb5124e62008-04-25 00:36:14 +0200316 buf.append(parser.stream.current.value.replace('%', '%%'))
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100317 next(parser.stream)
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100318 elif parser.stream.current.type == 'variable_begin':
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100319 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200320 name = parser.stream.expect('name').value
321 referenced.append(name)
322 buf.append('%%(%s)s' % name)
323 parser.stream.expect('variable_end')
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100324 elif parser.stream.current.type == 'block_begin':
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100325 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200326 if parser.stream.current.test('name:endtrans'):
327 break
328 elif parser.stream.current.test('name:pluralize'):
329 if allow_pluralize:
330 break
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200331 parser.fail('a translatable section can have only one '
332 'pluralize section')
333 parser.fail('control structures in translatable sections are '
334 'not allowed')
Armin Ronacherd02fc7d2008-06-14 14:19:47 +0200335 elif parser.stream.eos:
336 parser.fail('unclosed translation block')
Armin Ronacherb5124e62008-04-25 00:36:14 +0200337 else:
338 assert False, 'internal parser error'
339
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200340 return referenced, concat(buf)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200341
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200342 def _make_node(self, singular, plural, variables, plural_expr,
Armin Ronacherb98dad92010-05-29 22:31:17 +0200343 vars_referenced, num_called_num):
Armin Ronacherb5124e62008-04-25 00:36:14 +0200344 """Generates a useful node from the data provided."""
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200345 # no variables referenced? no need to escape for old style
Armin Ronacher4cccc222010-07-06 11:37:45 +0200346 # gettext invocations only if there are vars.
Armin Ronacherffaa2e72010-05-29 20:57:16 +0200347 if not vars_referenced and not self.environment.newstyle_gettext:
348 singular = singular.replace('%%', '%')
349 if plural:
350 plural = plural.replace('%%', '%')
351
Armin Ronacherb5124e62008-04-25 00:36:14 +0200352 # singular only:
353 if plural_expr is None:
354 gettext = nodes.Name('gettext', 'load')
355 node = nodes.Call(gettext, [nodes.Const(singular)],
356 [], None, None)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200357
358 # singular and plural
359 else:
360 ngettext = nodes.Name('ngettext', 'load')
361 node = nodes.Call(ngettext, [
362 nodes.Const(singular),
363 nodes.Const(plural),
364 plural_expr
365 ], [], None, None)
Armin Ronacherd84ec462008-04-29 13:43:16 +0200366
Armin Ronacher4da90342010-05-29 17:35:10 +0200367 # in case newstyle gettext is used, the method is powerful
368 # enough to handle the variable expansion and autoescape
369 # handling itself
370 if self.environment.newstyle_gettext:
Armin Ronachere9098672013-05-19 14:16:13 +0100371 for key, value in iteritems(variables):
Armin Ronacherb98dad92010-05-29 22:31:17 +0200372 # the function adds that later anyways in case num was
373 # called num, so just skip it.
374 if num_called_num and key == 'num':
375 continue
Armin Ronacherb8892e72010-05-29 17:58:06 +0200376 node.kwargs.append(nodes.Keyword(key, value))
Armin Ronacherd84ec462008-04-29 13:43:16 +0200377
Armin Ronacher4da90342010-05-29 17:35:10 +0200378 # otherwise do that here
379 else:
380 # mark the return value as safe if we are in an
381 # environment with autoescaping turned on
382 node = nodes.MarkSafeIfAutoescape(node)
383 if variables:
Armin Ronacherb8892e72010-05-29 17:58:06 +0200384 node = nodes.Mod(node, nodes.Dict([
385 nodes.Pair(nodes.Const(key), value)
386 for key, value in variables.items()
387 ]))
Armin Ronacherb5124e62008-04-25 00:36:14 +0200388 return nodes.Output([node])
389
390
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200391class ExprStmtExtension(Extension):
392 """Adds a `do` tag to Jinja2 that works like the print statement just
393 that it doesn't print the return value.
394 """
395 tags = set(['do'])
396
397 def parse(self, parser):
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100398 node = nodes.ExprStmt(lineno=next(parser.stream).lineno)
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200399 node.node = parser.parse_tuple()
400 return node
401
402
Armin Ronacher3da90312008-05-23 16:37:28 +0200403class LoopControlExtension(Extension):
404 """Adds break and continue to the template engine."""
405 tags = set(['break', 'continue'])
406
407 def parse(self, parser):
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100408 token = next(parser.stream)
Armin Ronacher3da90312008-05-23 16:37:28 +0200409 if token.value == 'break':
410 return nodes.Break(lineno=token.lineno)
411 return nodes.Continue(lineno=token.lineno)
412
413
Armin Ronacher9b4cc9f2010-02-07 03:55:15 +0100414class WithExtension(Extension):
415 """Adds support for a django-like with block."""
416 tags = set(['with'])
417
418 def parse(self, parser):
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100419 node = nodes.Scope(lineno=next(parser.stream).lineno)
Armin Ronacher9b4cc9f2010-02-07 03:55:15 +0100420 assignments = []
421 while parser.stream.current.type != 'block_end':
422 lineno = parser.stream.current.lineno
423 if assignments:
424 parser.stream.expect('comma')
425 target = parser.parse_assign_target()
426 parser.stream.expect('assign')
427 expr = parser.parse_expression()
428 assignments.append(nodes.Assign(target, expr, lineno=lineno))
429 node.body = assignments + \
430 list(parser.parse_statements(('name:endwith',),
431 drop_needle=True))
432 return node
433
434
Armin Ronacher8346bd72010-03-14 19:43:47 +0100435class AutoEscapeExtension(Extension):
436 """Changes auto escape rules for a scope."""
437 tags = set(['autoescape'])
438
439 def parse(self, parser):
Armin Ronacherc87d4cf2013-05-19 13:46:22 +0100440 node = nodes.ScopedEvalContextModifier(lineno=next(parser.stream).lineno)
Armin Ronacher8346bd72010-03-14 19:43:47 +0100441 node.options = [
442 nodes.Keyword('autoescape', parser.parse_expression())
443 ]
444 node.body = parser.parse_statements(('name:endautoescape',),
445 drop_needle=True)
446 return nodes.Scope([node])
447
448
Armin Ronacherabd36572008-06-27 08:45:19 +0200449def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,
450 babel_style=True):
451 """Extract localizable strings from the given template node. Per
452 default this function returns matches in babel style that means non string
453 parameters as well as keyword arguments are returned as `None`. This
454 allows Babel to figure out what you really meant if you are using
455 gettext functions that allow keyword arguments for placeholder expansion.
456 If you don't want that behavior set the `babel_style` parameter to `False`
457 which causes only strings to be returned and parameters are always stored
458 in tuples. As a consequence invalid gettext calls (calls without a single
459 string parameter or string parameters after non-string parameters) are
460 skipped.
461
462 This example explains the behavior:
463
464 >>> from jinja2 import Environment
465 >>> env = Environment()
466 >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')
467 >>> list(extract_from_ast(node))
468 [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]
469 >>> list(extract_from_ast(node, babel_style=False))
470 [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]
Armin Ronacherb5124e62008-04-25 00:36:14 +0200471
472 For every string found this function yields a ``(lineno, function,
473 message)`` tuple, where:
474
475 * ``lineno`` is the number of the line on which the string was found,
476 * ``function`` is the name of the ``gettext`` function used (if the
477 string was extracted from embedded Python code), and
478 * ``message`` is the string itself (a ``unicode`` object, or a tuple
479 of ``unicode`` objects for functions with multiple string arguments).
Armin Ronacher531578d2010-02-06 16:34:54 +0100480
481 This extraction function operates on the AST and is because of that unable
482 to extract any comments. For comment support you have to use the babel
483 extraction interface or extract comments yourself.
Armin Ronacherb5124e62008-04-25 00:36:14 +0200484 """
485 for node in node.find_all(nodes.Call):
486 if not isinstance(node.node, nodes.Name) or \
487 node.node.name not in gettext_functions:
488 continue
489
490 strings = []
491 for arg in node.args:
492 if isinstance(arg, nodes.Const) and \
Armin Ronachere9098672013-05-19 14:16:13 +0100493 isinstance(arg.value, string_types):
Armin Ronacherb5124e62008-04-25 00:36:14 +0200494 strings.append(arg.value)
495 else:
496 strings.append(None)
497
Armin Ronacherabd36572008-06-27 08:45:19 +0200498 for arg in node.kwargs:
499 strings.append(None)
500 if node.dyn_args is not None:
501 strings.append(None)
502 if node.dyn_kwargs is not None:
503 strings.append(None)
504
505 if not babel_style:
506 strings = tuple(x for x in strings if x is not None)
507 if not strings:
508 continue
Armin Ronacherb5124e62008-04-25 00:36:14 +0200509 else:
Armin Ronacherabd36572008-06-27 08:45:19 +0200510 if len(strings) == 1:
511 strings = strings[0]
512 else:
513 strings = tuple(strings)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200514 yield node.lineno, node.node.name, strings
515
516
Armin Ronacher531578d2010-02-06 16:34:54 +0100517class _CommentFinder(object):
518 """Helper class to find comments in a token stream. Can only
519 find comments for gettext calls forwards. Once the comment
520 from line 4 is found, a comment for line 1 will not return a
521 usable value.
522 """
523
524 def __init__(self, tokens, comment_tags):
525 self.tokens = tokens
526 self.comment_tags = comment_tags
527 self.offset = 0
528 self.last_lineno = 0
529
530 def find_backwards(self, offset):
531 try:
532 for _, token_type, token_value in \
533 reversed(self.tokens[self.offset:offset]):
534 if token_type in ('comment', 'linecomment'):
535 try:
536 prefix, comment = token_value.split(None, 1)
537 except ValueError:
538 continue
539 if prefix in self.comment_tags:
540 return [comment.rstrip()]
541 return []
542 finally:
543 self.offset = offset
544
545 def find_comments(self, lineno):
546 if not self.comment_tags or self.last_lineno > lineno:
547 return []
548 for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset:]):
549 if token_lineno > lineno:
550 return self.find_backwards(self.offset + idx)
551 return self.find_backwards(len(self.tokens))
552
553
Armin Ronacherb5124e62008-04-25 00:36:14 +0200554def babel_extract(fileobj, keywords, comment_tags, options):
555 """Babel extraction method for Jinja templates.
556
Armin Ronacher531578d2010-02-06 16:34:54 +0100557 .. versionchanged:: 2.3
558 Basic support for translation comments was added. If `comment_tags`
559 is now set to a list of keywords for extraction, the extractor will
560 try to find the best preceeding comment that begins with one of the
561 keywords. For best results, make sure to not have more than one
562 gettext call in one line of code and the matching comment in the
563 same line or the line before.
564
Armin Ronacher4f77a302010-07-01 12:15:39 +0200565 .. versionchanged:: 2.5.1
566 The `newstyle_gettext` flag can be set to `True` to enable newstyle
567 gettext calls.
568
Armin Ronacher11619152011-12-15 11:50:27 +0100569 .. versionchanged:: 2.7
570 A `silent` option can now be provided. If set to `False` template
571 syntax errors are propagated instead of being ignored.
572
Armin Ronacherb5124e62008-04-25 00:36:14 +0200573 :param fileobj: the file-like object the messages should be extracted from
574 :param keywords: a list of keywords (i.e. function names) that should be
575 recognized as translation functions
576 :param comment_tags: a list of translator tags to search for and include
Armin Ronacher531578d2010-02-06 16:34:54 +0100577 in the results.
Armin Ronacherb5124e62008-04-25 00:36:14 +0200578 :param options: a dictionary of additional options (optional)
579 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
580 (comments will be empty currently)
581 """
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200582 extensions = set()
Armin Ronacherb5124e62008-04-25 00:36:14 +0200583 for extension in options.get('extensions', '').split(','):
584 extension = extension.strip()
585 if not extension:
586 continue
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200587 extensions.add(import_string(extension))
588 if InternationalizationExtension not in extensions:
589 extensions.add(InternationalizationExtension)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200590
Armin Ronacher4f77a302010-07-01 12:15:39 +0200591 def getbool(options, key, default=False):
Armin Ronacher11619152011-12-15 11:50:27 +0100592 return options.get(key, str(default)).lower() in \
593 ('1', 'on', 'yes', 'true')
Armin Ronacher4f77a302010-07-01 12:15:39 +0200594
Armin Ronacher11619152011-12-15 11:50:27 +0100595 silent = getbool(options, 'silent', True)
Armin Ronacher4f77a302010-07-01 12:15:39 +0200596 environment = Environment(
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200597 options.get('block_start_string', BLOCK_START_STRING),
598 options.get('block_end_string', BLOCK_END_STRING),
599 options.get('variable_start_string', VARIABLE_START_STRING),
600 options.get('variable_end_string', VARIABLE_END_STRING),
601 options.get('comment_start_string', COMMENT_START_STRING),
602 options.get('comment_end_string', COMMENT_END_STRING),
603 options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,
Armin Ronacher59b6bd52009-03-30 21:00:16 +0200604 options.get('line_comment_prefix') or LINE_COMMENT_PREFIX,
Armin Ronacher4f77a302010-07-01 12:15:39 +0200605 getbool(options, 'trim_blocks', TRIM_BLOCKS),
Armin Ronachera65f1eb2013-05-19 11:18:19 +0100606 getbool(options, 'lstrip_blocks', LSTRIP_BLOCKS),
W. Trevor King7e912c62013-01-11 08:23:24 -0500607 NEWLINE_SEQUENCE,
608 getbool(options, 'keep_trailing_newline', KEEP_TRAILING_NEWLINE),
609 frozenset(extensions),
Armin Ronacher4f77a302010-07-01 12:15:39 +0200610 cache_size=0,
611 auto_reload=False
Armin Ronacherb5124e62008-04-25 00:36:14 +0200612 )
613
Armin Ronacher4f77a302010-07-01 12:15:39 +0200614 if getbool(options, 'newstyle_gettext'):
615 environment.newstyle_gettext = True
616
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200617 source = fileobj.read().decode(options.get('encoding', 'utf-8'))
Armin Ronacherc670b112008-06-29 17:23:04 +0200618 try:
619 node = environment.parse(source)
Armin Ronacher531578d2010-02-06 16:34:54 +0100620 tokens = list(environment.lex(environment.preprocess(source)))
Thomas Waldmanne0003552013-05-17 23:52:14 +0200621 except TemplateSyntaxError as e:
Armin Ronacher11619152011-12-15 11:50:27 +0100622 if not silent:
623 raise
Armin Ronacherc670b112008-06-29 17:23:04 +0200624 # skip templates with syntax errors
625 return
Armin Ronacher531578d2010-02-06 16:34:54 +0100626
627 finder = _CommentFinder(tokens, comment_tags)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200628 for lineno, func, message in extract_from_ast(node, keywords):
Armin Ronacher531578d2010-02-06 16:34:54 +0100629 yield lineno, func, message, finder.find_comments(lineno)
Armin Ronachered98cac2008-05-07 08:42:11 +0200630
631
632#: nicer import names
633i18n = InternationalizationExtension
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200634do = ExprStmtExtension
Armin Ronacher3da90312008-05-23 16:37:28 +0200635loopcontrols = LoopControlExtension
Armin Ronacher9b4cc9f2010-02-07 03:55:15 +0100636with_ = WithExtension
Armin Ronacher8346bd72010-03-14 19:43:47 +0100637autoescape = AutoEscapeExtension