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