blob: 64d5525adf436a8c33ac53681144262b75630995 [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
60 def __init__(self, environment):
61 self.environment = environment
62
Armin Ronacher7259c762008-04-30 13:03:59 +020063 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
Armin Ronacher9ad96e72008-06-13 22:44:01 +020070 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`.
Armin Ronacherd02fc7d2008-06-14 14:19:47 +020082
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.
Armin Ronacher9ad96e72008-06-13 22:44:01 +020086 """
87 return stream
88
Armin Ronacher05530932008-04-20 13:27:49 +020089 def parse(self, parser):
Armin Ronacher023b5e92008-05-08 11:03:10 +020090 """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 """
Armin Ronacher27069d72008-05-11 19:48:12 +020095 raise NotImplementedError()
Armin Ronacher023b5e92008-05-08 11:03:10 +020096
97 def attr(self, name, lineno=None):
98 """Return an attribute node for the current extension. This is useful
Armin Ronacher69e12db2008-05-12 09:00:03 +020099 to pass constants on extensions to generated template code::
Armin Ronacher023b5e92008-05-08 11:03:10 +0200100
Armin Ronacher69e12db2008-05-12 09:00:03 +0200101 self.attr('_my_attribute', lineno=lineno)
Armin Ronacher023b5e92008-05-08 11:03:10 +0200102 """
103 return nodes.ExtensionAttribute(self.identifier, name, lineno=lineno)
Armin Ronacher05530932008-04-20 13:27:49 +0200104
Armin Ronacher27069d72008-05-11 19:48:12 +0200105 def call_method(self, name, args=None, kwargs=None, dyn_args=None,
106 dyn_kwargs=None, lineno=None):
Armin Ronacher69e12db2008-05-12 09:00:03 +0200107 """Call a method of the extension. This is a shortcut for
108 :meth:`attr` + :class:`jinja2.nodes.Call`.
109 """
Armin Ronacher27069d72008-05-11 19:48:12 +0200110 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
Armin Ronacher05530932008-04-20 13:27:49 +0200117
Armin Ronacher5c047ea2008-05-23 22:26:45 +0200118@contextfunction
119def _gettext_alias(context, string):
120 return context.resolve('gettext')(string)
121
122
Armin Ronachered98cac2008-05-07 08:42:11 +0200123class InternationalizationExtension(Extension):
Armin Ronacher762079c2008-05-08 23:57:56 +0200124 """This extension adds gettext support to Jinja2."""
Armin Ronacherb5124e62008-04-25 00:36:14 +0200125 tags = set(['trans'])
126
Armin Ronacher4720c362008-09-06 16:15:38 +0200127 # TODO: the i18n extension is currently reevaluating values in a few
128 # situations. Take this example:
129 # {% trans count=something() %}{{ count }} foo{% pluralize
130 # %}{{ count }} fooss{% endtrans %}
131 # something is called twice here. One time for the gettext value and
132 # the other time for the n-parameter of the ngettext function.
133
Armin Ronacherb5124e62008-04-25 00:36:14 +0200134 def __init__(self, environment):
135 Extension.__init__(self, environment)
Armin Ronacher5c047ea2008-05-23 22:26:45 +0200136 environment.globals['_'] = _gettext_alias
Armin Ronacher762079c2008-05-08 23:57:56 +0200137 environment.extend(
138 install_gettext_translations=self._install,
139 install_null_translations=self._install_null,
140 uninstall_gettext_translations=self._uninstall,
141 extract_translations=self._extract
142 )
143
144 def _install(self, translations):
Armin Ronacher32133552008-09-15 14:35:01 +0200145 gettext = getattr(translations, 'ugettext', None)
146 if gettext is None:
147 gettext = translations.gettext
148 ngettext = getattr(translations, 'ungettext', None)
149 if ngettext is None:
150 ngettext = translations.ngettext
151 self.environment.globals.update(gettext=gettext, ngettext=ngettext)
Armin Ronacher762079c2008-05-08 23:57:56 +0200152
153 def _install_null(self):
154 self.environment.globals.update(
155 gettext=lambda x: x,
156 ngettext=lambda s, p, n: (n != 1 and (p,) or (s,))[0]
157 )
158
159 def _uninstall(self, translations):
160 for key in 'gettext', 'ngettext':
161 self.environment.globals.pop(key, None)
162
163 def _extract(self, source, gettext_functions=GETTEXT_FUNCTIONS):
164 if isinstance(source, basestring):
165 source = self.environment.parse(source)
166 return extract_from_ast(source, gettext_functions)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200167
168 def parse(self, parser):
169 """Parse a translatable tag."""
Armin Ronacherbd357722009-08-05 20:25:06 +0200170 lineno = next(parser.stream).lineno
Armin Ronacherb5124e62008-04-25 00:36:14 +0200171
Armin Ronacherb5124e62008-04-25 00:36:14 +0200172 # find all the variables referenced. Additionally a variable can be
173 # defined in the body of the trans block too, but this is checked at
174 # a later state.
175 plural_expr = None
176 variables = {}
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100177 while parser.stream.current.type != 'block_end':
Armin Ronacherb5124e62008-04-25 00:36:14 +0200178 if variables:
179 parser.stream.expect('comma')
Armin Ronacher023b5e92008-05-08 11:03:10 +0200180
181 # skip colon for python compatibility
Armin Ronacherfdf95302008-05-11 22:20:51 +0200182 if parser.stream.skip_if('colon'):
Armin Ronacher023b5e92008-05-08 11:03:10 +0200183 break
184
Armin Ronacherb5124e62008-04-25 00:36:14 +0200185 name = parser.stream.expect('name')
186 if name.value in variables:
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200187 parser.fail('translatable variable %r defined twice.' %
188 name.value, name.lineno,
189 exc=TemplateAssertionError)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200190
191 # expressions
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100192 if parser.stream.current.type == 'assign':
Armin Ronacherbd357722009-08-05 20:25:06 +0200193 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200194 variables[name.value] = var = parser.parse_expression()
195 else:
196 variables[name.value] = var = nodes.Name(name.value, 'load')
197 if plural_expr is None:
198 plural_expr = var
Armin Ronacher023b5e92008-05-08 11:03:10 +0200199
Armin Ronacherb5124e62008-04-25 00:36:14 +0200200 parser.stream.expect('block_end')
201
202 plural = plural_names = None
203 have_plural = False
204 referenced = set()
205
206 # now parse until endtrans or pluralize
207 singular_names, singular = self._parse_block(parser, True)
208 if singular_names:
209 referenced.update(singular_names)
210 if plural_expr is None:
211 plural_expr = nodes.Name(singular_names[0], 'load')
212
213 # if we have a pluralize block, we parse that too
214 if parser.stream.current.test('name:pluralize'):
215 have_plural = True
Armin Ronacherbd357722009-08-05 20:25:06 +0200216 next(parser.stream)
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100217 if parser.stream.current.type != 'block_end':
Armin Ronacher4720c362008-09-06 16:15:38 +0200218 name = parser.stream.expect('name')
219 if name.value not in variables:
220 parser.fail('unknown variable %r for pluralization' %
221 name.value, name.lineno,
222 exc=TemplateAssertionError)
223 plural_expr = variables[name.value]
Armin Ronacherb5124e62008-04-25 00:36:14 +0200224 parser.stream.expect('block_end')
225 plural_names, plural = self._parse_block(parser, False)
Armin Ronacherbd357722009-08-05 20:25:06 +0200226 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200227 referenced.update(plural_names)
228 else:
Armin Ronacherbd357722009-08-05 20:25:06 +0200229 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200230
231 # register free names as simple name expressions
232 for var in referenced:
233 if var not in variables:
234 variables[var] = nodes.Name(var, 'load')
235
236 # no variables referenced? no need to escape
237 if not referenced:
238 singular = singular.replace('%%', '%')
239 if plural:
240 plural = plural.replace('%%', '%')
241
242 if not have_plural:
243 plural_expr = None
244 elif plural_expr is None:
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200245 parser.fail('pluralize without variables', lineno)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200246
247 if variables:
248 variables = nodes.Dict([nodes.Pair(nodes.Const(x, lineno=lineno), y)
249 for x, y in variables.items()])
250 else:
251 variables = None
252
253 node = self._make_node(singular, plural, variables, plural_expr)
254 node.set_lineno(lineno)
255 return node
256
257 def _parse_block(self, parser, allow_pluralize):
258 """Parse until the next block tag with a given name."""
259 referenced = []
260 buf = []
261 while 1:
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100262 if parser.stream.current.type == 'data':
Armin Ronacherb5124e62008-04-25 00:36:14 +0200263 buf.append(parser.stream.current.value.replace('%', '%%'))
Armin Ronacherbd357722009-08-05 20:25:06 +0200264 next(parser.stream)
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100265 elif parser.stream.current.type == 'variable_begin':
Armin Ronacherbd357722009-08-05 20:25:06 +0200266 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200267 name = parser.stream.expect('name').value
268 referenced.append(name)
269 buf.append('%%(%s)s' % name)
270 parser.stream.expect('variable_end')
Armin Ronacher7647d1c2009-01-05 12:16:46 +0100271 elif parser.stream.current.type == 'block_begin':
Armin Ronacherbd357722009-08-05 20:25:06 +0200272 next(parser.stream)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200273 if parser.stream.current.test('name:endtrans'):
274 break
275 elif parser.stream.current.test('name:pluralize'):
276 if allow_pluralize:
277 break
Armin Ronacher7f15ef82008-05-16 09:11:39 +0200278 parser.fail('a translatable section can have only one '
279 'pluralize section')
280 parser.fail('control structures in translatable sections are '
281 'not allowed')
Armin Ronacherd02fc7d2008-06-14 14:19:47 +0200282 elif parser.stream.eos:
283 parser.fail('unclosed translation block')
Armin Ronacherb5124e62008-04-25 00:36:14 +0200284 else:
285 assert False, 'internal parser error'
286
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200287 return referenced, concat(buf)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200288
289 def _make_node(self, singular, plural, variables, plural_expr):
290 """Generates a useful node from the data provided."""
291 # singular only:
292 if plural_expr is None:
293 gettext = nodes.Name('gettext', 'load')
294 node = nodes.Call(gettext, [nodes.Const(singular)],
295 [], None, None)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200296
297 # singular and plural
298 else:
299 ngettext = nodes.Name('ngettext', 'load')
300 node = nodes.Call(ngettext, [
301 nodes.Const(singular),
302 nodes.Const(plural),
303 plural_expr
304 ], [], None, None)
Armin Ronacherd84ec462008-04-29 13:43:16 +0200305
306 # mark the return value as safe if we are in an
307 # environment with autoescaping turned on
308 if self.environment.autoescape:
309 node = nodes.MarkSafe(node)
310
311 if variables:
312 node = nodes.Mod(node, variables)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200313 return nodes.Output([node])
314
315
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200316class ExprStmtExtension(Extension):
317 """Adds a `do` tag to Jinja2 that works like the print statement just
318 that it doesn't print the return value.
319 """
320 tags = set(['do'])
321
322 def parse(self, parser):
Armin Ronacherbd357722009-08-05 20:25:06 +0200323 node = nodes.ExprStmt(lineno=next(parser.stream).lineno)
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200324 node.node = parser.parse_tuple()
325 return node
326
327
Armin Ronacher3da90312008-05-23 16:37:28 +0200328class LoopControlExtension(Extension):
329 """Adds break and continue to the template engine."""
330 tags = set(['break', 'continue'])
331
332 def parse(self, parser):
Armin Ronacherbd357722009-08-05 20:25:06 +0200333 token = next(parser.stream)
Armin Ronacher3da90312008-05-23 16:37:28 +0200334 if token.value == 'break':
335 return nodes.Break(lineno=token.lineno)
336 return nodes.Continue(lineno=token.lineno)
337
338
Armin Ronacher9b4cc9f2010-02-07 03:55:15 +0100339class WithExtension(Extension):
340 """Adds support for a django-like with block."""
341 tags = set(['with'])
342
343 def parse(self, parser):
344 node = nodes.Scope(lineno=next(parser.stream).lineno)
345 assignments = []
346 while parser.stream.current.type != 'block_end':
347 lineno = parser.stream.current.lineno
348 if assignments:
349 parser.stream.expect('comma')
350 target = parser.parse_assign_target()
351 parser.stream.expect('assign')
352 expr = parser.parse_expression()
353 assignments.append(nodes.Assign(target, expr, lineno=lineno))
354 node.body = assignments + \
355 list(parser.parse_statements(('name:endwith',),
356 drop_needle=True))
357 return node
358
359
Armin Ronacher8346bd72010-03-14 19:43:47 +0100360class AutoEscapeExtension(Extension):
361 """Changes auto escape rules for a scope."""
362 tags = set(['autoescape'])
363
364 def parse(self, parser):
365 node = nodes.ScopedEvalContextModifier(lineno=next(parser.stream).lineno)
366 node.options = [
367 nodes.Keyword('autoescape', parser.parse_expression())
368 ]
369 node.body = parser.parse_statements(('name:endautoescape',),
370 drop_needle=True)
371 return nodes.Scope([node])
372
373
Armin Ronacherabd36572008-06-27 08:45:19 +0200374def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS,
375 babel_style=True):
376 """Extract localizable strings from the given template node. Per
377 default this function returns matches in babel style that means non string
378 parameters as well as keyword arguments are returned as `None`. This
379 allows Babel to figure out what you really meant if you are using
380 gettext functions that allow keyword arguments for placeholder expansion.
381 If you don't want that behavior set the `babel_style` parameter to `False`
382 which causes only strings to be returned and parameters are always stored
383 in tuples. As a consequence invalid gettext calls (calls without a single
384 string parameter or string parameters after non-string parameters) are
385 skipped.
386
387 This example explains the behavior:
388
389 >>> from jinja2 import Environment
390 >>> env = Environment()
391 >>> node = env.parse('{{ (_("foo"), _(), ngettext("foo", "bar", 42)) }}')
392 >>> list(extract_from_ast(node))
393 [(1, '_', 'foo'), (1, '_', ()), (1, 'ngettext', ('foo', 'bar', None))]
394 >>> list(extract_from_ast(node, babel_style=False))
395 [(1, '_', ('foo',)), (1, 'ngettext', ('foo', 'bar'))]
Armin Ronacherb5124e62008-04-25 00:36:14 +0200396
397 For every string found this function yields a ``(lineno, function,
398 message)`` tuple, where:
399
400 * ``lineno`` is the number of the line on which the string was found,
401 * ``function`` is the name of the ``gettext`` function used (if the
402 string was extracted from embedded Python code), and
403 * ``message`` is the string itself (a ``unicode`` object, or a tuple
404 of ``unicode`` objects for functions with multiple string arguments).
Armin Ronacher531578d2010-02-06 16:34:54 +0100405
406 This extraction function operates on the AST and is because of that unable
407 to extract any comments. For comment support you have to use the babel
408 extraction interface or extract comments yourself.
Armin Ronacherb5124e62008-04-25 00:36:14 +0200409 """
410 for node in node.find_all(nodes.Call):
411 if not isinstance(node.node, nodes.Name) or \
412 node.node.name not in gettext_functions:
413 continue
414
415 strings = []
416 for arg in node.args:
417 if isinstance(arg, nodes.Const) and \
418 isinstance(arg.value, basestring):
419 strings.append(arg.value)
420 else:
421 strings.append(None)
422
Armin Ronacherabd36572008-06-27 08:45:19 +0200423 for arg in node.kwargs:
424 strings.append(None)
425 if node.dyn_args is not None:
426 strings.append(None)
427 if node.dyn_kwargs is not None:
428 strings.append(None)
429
430 if not babel_style:
431 strings = tuple(x for x in strings if x is not None)
432 if not strings:
433 continue
Armin Ronacherb5124e62008-04-25 00:36:14 +0200434 else:
Armin Ronacherabd36572008-06-27 08:45:19 +0200435 if len(strings) == 1:
436 strings = strings[0]
437 else:
438 strings = tuple(strings)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200439 yield node.lineno, node.node.name, strings
440
441
Armin Ronacher531578d2010-02-06 16:34:54 +0100442class _CommentFinder(object):
443 """Helper class to find comments in a token stream. Can only
444 find comments for gettext calls forwards. Once the comment
445 from line 4 is found, a comment for line 1 will not return a
446 usable value.
447 """
448
449 def __init__(self, tokens, comment_tags):
450 self.tokens = tokens
451 self.comment_tags = comment_tags
452 self.offset = 0
453 self.last_lineno = 0
454
455 def find_backwards(self, offset):
456 try:
457 for _, token_type, token_value in \
458 reversed(self.tokens[self.offset:offset]):
459 if token_type in ('comment', 'linecomment'):
460 try:
461 prefix, comment = token_value.split(None, 1)
462 except ValueError:
463 continue
464 if prefix in self.comment_tags:
465 return [comment.rstrip()]
466 return []
467 finally:
468 self.offset = offset
469
470 def find_comments(self, lineno):
471 if not self.comment_tags or self.last_lineno > lineno:
472 return []
473 for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset:]):
474 if token_lineno > lineno:
475 return self.find_backwards(self.offset + idx)
476 return self.find_backwards(len(self.tokens))
477
478
Armin Ronacherb5124e62008-04-25 00:36:14 +0200479def babel_extract(fileobj, keywords, comment_tags, options):
480 """Babel extraction method for Jinja templates.
481
Armin Ronacher531578d2010-02-06 16:34:54 +0100482 .. versionchanged:: 2.3
483 Basic support for translation comments was added. If `comment_tags`
484 is now set to a list of keywords for extraction, the extractor will
485 try to find the best preceeding comment that begins with one of the
486 keywords. For best results, make sure to not have more than one
487 gettext call in one line of code and the matching comment in the
488 same line or the line before.
489
Armin Ronacherb5124e62008-04-25 00:36:14 +0200490 :param fileobj: the file-like object the messages should be extracted from
491 :param keywords: a list of keywords (i.e. function names) that should be
492 recognized as translation functions
493 :param comment_tags: a list of translator tags to search for and include
Armin Ronacher531578d2010-02-06 16:34:54 +0100494 in the results.
Armin Ronacherb5124e62008-04-25 00:36:14 +0200495 :param options: a dictionary of additional options (optional)
496 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
497 (comments will be empty currently)
498 """
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200499 extensions = set()
Armin Ronacherb5124e62008-04-25 00:36:14 +0200500 for extension in options.get('extensions', '').split(','):
501 extension = extension.strip()
502 if not extension:
503 continue
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200504 extensions.add(import_string(extension))
505 if InternationalizationExtension not in extensions:
506 extensions.add(InternationalizationExtension)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200507
508 environment = get_spontaneous_environment(
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200509 options.get('block_start_string', BLOCK_START_STRING),
510 options.get('block_end_string', BLOCK_END_STRING),
511 options.get('variable_start_string', VARIABLE_START_STRING),
512 options.get('variable_end_string', VARIABLE_END_STRING),
513 options.get('comment_start_string', COMMENT_START_STRING),
514 options.get('comment_end_string', COMMENT_END_STRING),
515 options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,
Armin Ronacher59b6bd52009-03-30 21:00:16 +0200516 options.get('line_comment_prefix') or LINE_COMMENT_PREFIX,
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200517 str(options.get('trim_blocks', TRIM_BLOCKS)).lower() in \
518 ('1', 'on', 'yes', 'true'),
519 NEWLINE_SEQUENCE, frozenset(extensions),
Armin Ronacherb5124e62008-04-25 00:36:14 +0200520 # fill with defaults so that environments are shared
Armin Ronacher7259c762008-04-30 13:03:59 +0200521 # with other spontaneus environments. The rest of the
522 # arguments are optimizer, undefined, finalize, autoescape,
Armin Ronacher4d5bdff2008-09-17 16:19:46 +0200523 # loader, cache size, auto reloading setting and the
524 # bytecode cache
525 True, Undefined, None, False, None, 0, False, None
Armin Ronacherb5124e62008-04-25 00:36:14 +0200526 )
527
Armin Ronacher4f5008f2008-05-23 23:36:07 +0200528 source = fileobj.read().decode(options.get('encoding', 'utf-8'))
Armin Ronacherc670b112008-06-29 17:23:04 +0200529 try:
530 node = environment.parse(source)
Armin Ronacher531578d2010-02-06 16:34:54 +0100531 tokens = list(environment.lex(environment.preprocess(source)))
Armin Ronacherc670b112008-06-29 17:23:04 +0200532 except TemplateSyntaxError, e:
533 # skip templates with syntax errors
534 return
Armin Ronacher531578d2010-02-06 16:34:54 +0100535
536 finder = _CommentFinder(tokens, comment_tags)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200537 for lineno, func, message in extract_from_ast(node, keywords):
Armin Ronacher531578d2010-02-06 16:34:54 +0100538 yield lineno, func, message, finder.find_comments(lineno)
Armin Ronachered98cac2008-05-07 08:42:11 +0200539
540
541#: nicer import names
542i18n = InternationalizationExtension
Armin Ronacher5d2733f2008-05-15 23:26:52 +0200543do = ExprStmtExtension
Armin Ronacher3da90312008-05-23 16:37:28 +0200544loopcontrols = LoopControlExtension
Armin Ronacher9b4cc9f2010-02-07 03:55:15 +0100545with_ = WithExtension
Armin Ronacher8346bd72010-03-14 19:43:47 +0100546autoescape = AutoEscapeExtension