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