blob: 84d656d18a196984669f8300686755c07288570a [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
10 :copyright: Copyright 2008 by Armin Ronacher.
11 :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 Ronacherb5124e62008-04-25 00:36:14 +020015from jinja2.environment import get_spontaneous_environment
Armin Ronacher2feed1d2008-04-26 16:26:52 +020016from jinja2.runtime import Undefined, concat
Armin Ronacherb5124e62008-04-25 00:36:14 +020017from jinja2.parser import statement_end_tokens
Benjamin Wieganda3152742008-04-28 18:07:52 +020018from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError
Armin Ronacherd1342312008-04-28 12:20:12 +020019from jinja2.utils import import_string, Markup
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
28class Extension(object):
Armin Ronacher7259c762008-04-30 13:03:59 +020029 """Extensions can be used to add extra functionality to the Jinja template
30 system at the parser level. This is a supported but currently
31 undocumented interface. Custom extensions are bound to an environment but
32 may not store environment specific data on `self`. The reason for this is
33 that an extension can be bound to another environment (for overlays) by
34 creating a copy and reassigning the `environment` attribute.
35 """
Armin Ronacher05530932008-04-20 13:27:49 +020036
37 #: if this extension parses this is the list of tags it's listening to.
38 tags = set()
39
40 def __init__(self, environment):
41 self.environment = environment
42
Armin Ronacher7259c762008-04-30 13:03:59 +020043 def bind(self, environment):
44 """Create a copy of this extension bound to another environment."""
45 rv = object.__new__(self.__class__)
46 rv.__dict__.update(self.__dict__)
47 rv.environment = environment
48 return rv
49
Armin Ronacher05530932008-04-20 13:27:49 +020050 def parse(self, parser):
51 """Called if one of the tags matched."""
52
53
54class CacheExtension(Extension):
Armin Ronacher2b60fe52008-04-21 08:23:59 +020055 """An example extension that adds cacheable blocks."""
Armin Ronacher05530932008-04-20 13:27:49 +020056 tags = set(['cache'])
57
Armin Ronacher203bfcb2008-04-24 21:54:44 +020058 def __init__(self, environment):
59 Extension.__init__(self, environment)
60 def dummy_cache_support(name, timeout=None, caller=None):
61 if caller is not None:
62 return caller()
63 environment.globals['cache_support'] = dummy_cache_support
64
Armin Ronacher05530932008-04-20 13:27:49 +020065 def parse(self, parser):
66 lineno = parser.stream.next().lineno
67 args = [parser.parse_expression()]
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020068 if parser.stream.current.type is 'comma':
69 parser.stream.next()
Armin Ronacher05530932008-04-20 13:27:49 +020070 args.append(parser.parse_expression())
71 body = parser.parse_statements(('name:endcache',), drop_needle=True)
72 return nodes.CallBlock(
Armin Ronacher4f7d2d52008-04-22 10:40:26 +020073 nodes.Call(nodes.Name('cache_support', 'load'), args, [], None, None),
Armin Ronacher05530932008-04-20 13:27:49 +020074 [], [], body
75 )
Armin Ronacherb5124e62008-04-25 00:36:14 +020076
77
78class TransExtension(Extension):
79 """This extension adds gettext support to Jinja."""
80 tags = set(['trans'])
81
82 def __init__(self, environment):
83 Extension.__init__(self, environment)
84 environment.globals.update({
85 '_': lambda x: x,
86 'gettext': lambda x: x,
87 'ngettext': lambda s, p, n: (s, p)[n != 1]
88 })
89
90 def parse(self, parser):
91 """Parse a translatable tag."""
92 lineno = parser.stream.next().lineno
93
94 # skip colon for python compatibility
95 if parser.stream.current.type is 'colon':
96 parser.stream.next()
97
98 # find all the variables referenced. Additionally a variable can be
99 # defined in the body of the trans block too, but this is checked at
100 # a later state.
101 plural_expr = None
102 variables = {}
103 while parser.stream.current.type is not 'block_end':
104 if variables:
105 parser.stream.expect('comma')
106 name = parser.stream.expect('name')
107 if name.value in variables:
108 raise TemplateAssertionError('translatable variable %r defined '
109 'twice.' % name.value, name.lineno,
110 parser.filename)
111
112 # expressions
113 if parser.stream.current.type is 'assign':
114 parser.stream.next()
115 variables[name.value] = var = parser.parse_expression()
116 else:
117 variables[name.value] = var = nodes.Name(name.value, 'load')
118 if plural_expr is None:
119 plural_expr = var
120 parser.stream.expect('block_end')
121
122 plural = plural_names = None
123 have_plural = False
124 referenced = set()
125
126 # now parse until endtrans or pluralize
127 singular_names, singular = self._parse_block(parser, True)
128 if singular_names:
129 referenced.update(singular_names)
130 if plural_expr is None:
131 plural_expr = nodes.Name(singular_names[0], 'load')
132
133 # if we have a pluralize block, we parse that too
134 if parser.stream.current.test('name:pluralize'):
135 have_plural = True
136 parser.stream.next()
137 if parser.stream.current.type is not 'block_end':
138 plural_expr = parser.parse_expression()
139 parser.stream.expect('block_end')
140 plural_names, plural = self._parse_block(parser, False)
141 parser.stream.next()
142 referenced.update(plural_names)
143 else:
144 parser.stream.next()
145
146 # register free names as simple name expressions
147 for var in referenced:
148 if var not in variables:
149 variables[var] = nodes.Name(var, 'load')
150
151 # no variables referenced? no need to escape
152 if not referenced:
153 singular = singular.replace('%%', '%')
154 if plural:
155 plural = plural.replace('%%', '%')
156
157 if not have_plural:
158 plural_expr = None
159 elif plural_expr is None:
160 raise TemplateAssertionError('pluralize without variables',
161 lineno, parser.filename)
162
163 if variables:
164 variables = nodes.Dict([nodes.Pair(nodes.Const(x, lineno=lineno), y)
165 for x, y in variables.items()])
166 else:
167 variables = None
168
169 node = self._make_node(singular, plural, variables, plural_expr)
170 node.set_lineno(lineno)
171 return node
172
173 def _parse_block(self, parser, allow_pluralize):
174 """Parse until the next block tag with a given name."""
175 referenced = []
176 buf = []
177 while 1:
178 if parser.stream.current.type is 'data':
179 buf.append(parser.stream.current.value.replace('%', '%%'))
180 parser.stream.next()
181 elif parser.stream.current.type is 'variable_begin':
182 parser.stream.next()
183 name = parser.stream.expect('name').value
184 referenced.append(name)
185 buf.append('%%(%s)s' % name)
186 parser.stream.expect('variable_end')
187 elif parser.stream.current.type is 'block_begin':
188 parser.stream.next()
189 if parser.stream.current.test('name:endtrans'):
190 break
191 elif parser.stream.current.test('name:pluralize'):
192 if allow_pluralize:
193 break
194 raise TemplateSyntaxError('a translatable section can '
195 'have only one pluralize '
196 'section',
197 parser.stream.current.lineno,
198 parser.filename)
199 raise TemplateSyntaxError('control structures in translatable'
200 ' sections are not allowed.',
201 parser.stream.current.lineno,
202 parser.filename)
203 else:
204 assert False, 'internal parser error'
205
Armin Ronacher2feed1d2008-04-26 16:26:52 +0200206 return referenced, concat(buf)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200207
208 def _make_node(self, singular, plural, variables, plural_expr):
209 """Generates a useful node from the data provided."""
210 # singular only:
211 if plural_expr is None:
212 gettext = nodes.Name('gettext', 'load')
213 node = nodes.Call(gettext, [nodes.Const(singular)],
214 [], None, None)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200215
216 # singular and plural
217 else:
218 ngettext = nodes.Name('ngettext', 'load')
219 node = nodes.Call(ngettext, [
220 nodes.Const(singular),
221 nodes.Const(plural),
222 plural_expr
223 ], [], None, None)
Armin Ronacherd84ec462008-04-29 13:43:16 +0200224
225 # mark the return value as safe if we are in an
226 # environment with autoescaping turned on
227 if self.environment.autoescape:
228 node = nodes.MarkSafe(node)
229
230 if variables:
231 node = nodes.Mod(node, variables)
Armin Ronacherb5124e62008-04-25 00:36:14 +0200232 return nodes.Output([node])
233
234
235def extract_from_ast(node, gettext_functions=GETTEXT_FUNCTIONS):
236 """Extract localizable strings from the given template node.
237
238 For every string found this function yields a ``(lineno, function,
239 message)`` tuple, where:
240
241 * ``lineno`` is the number of the line on which the string was found,
242 * ``function`` is the name of the ``gettext`` function used (if the
243 string was extracted from embedded Python code), and
244 * ``message`` is the string itself (a ``unicode`` object, or a tuple
245 of ``unicode`` objects for functions with multiple string arguments).
246 """
247 for node in node.find_all(nodes.Call):
248 if not isinstance(node.node, nodes.Name) or \
249 node.node.name not in gettext_functions:
250 continue
251
252 strings = []
253 for arg in node.args:
254 if isinstance(arg, nodes.Const) and \
255 isinstance(arg.value, basestring):
256 strings.append(arg.value)
257 else:
258 strings.append(None)
259
260 if len(strings) == 1:
261 strings = strings[0]
262 else:
263 strings = tuple(strings)
264 yield node.lineno, node.node.name, strings
265
266
267def babel_extract(fileobj, keywords, comment_tags, options):
268 """Babel extraction method for Jinja templates.
269
270 :param fileobj: the file-like object the messages should be extracted from
271 :param keywords: a list of keywords (i.e. function names) that should be
272 recognized as translation functions
273 :param comment_tags: a list of translator tags to search for and include
274 in the results. (Unused)
275 :param options: a dictionary of additional options (optional)
276 :return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
277 (comments will be empty currently)
278 """
279 encoding = options.get('encoding', 'utf-8')
280
281 have_trans_extension = False
282 extensions = []
283 for extension in options.get('extensions', '').split(','):
284 extension = extension.strip()
285 if not extension:
286 continue
287 extension = import_string(extension)
288 if extension is TransExtension:
289 have_trans_extension = True
290 extensions.append(extension)
291 if not have_trans_extension:
292 extensions.append(TransExtension)
293
294 environment = get_spontaneous_environment(
295 options.get('block_start_string', '{%'),
296 options.get('block_end_string', '%}'),
297 options.get('variable_start_string', '{{'),
298 options.get('variable_end_string', '}}'),
299 options.get('comment_start_string', '{#'),
300 options.get('comment_end_string', '#}'),
301 options.get('line_statement_prefix') or None,
302 options.get('trim_blocks', '').lower() in ('1', 'on', 'yes', 'true'),
303 tuple(extensions),
304 # fill with defaults so that environments are shared
Armin Ronacher7259c762008-04-30 13:03:59 +0200305 # with other spontaneus environments. The rest of the
306 # arguments are optimizer, undefined, finalize, autoescape,
307 # loader, cache size and auto reloading setting
308 True, Undefined, None, False, None, 0, False
Armin Ronacherb5124e62008-04-25 00:36:14 +0200309 )
310
311 node = environment.parse(fileobj.read().decode(encoding))
312 for lineno, func, message in extract_from_ast(node, keywords):
313 yield lineno, func, message, []