Armin Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | jinja2.ext |
| 4 | ~~~~~~~~~~ |
| 5 | |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 6 | 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 Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 9 | |
| 10 | :copyright: Copyright 2008 by Armin Ronacher. |
| 11 | :license: BSD. |
| 12 | """ |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 13 | from collections import deque |
Armin Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 14 | from jinja2 import nodes |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 15 | from jinja2.environment import get_spontaneous_environment |
Armin Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 16 | from jinja2.runtime import Undefined, concat |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 17 | from jinja2.parser import statement_end_tokens |
Benjamin Wiegand | a315274 | 2008-04-28 18:07:52 +0200 | [diff] [blame] | 18 | from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError |
Armin Ronacher | d134231 | 2008-04-28 12:20:12 +0200 | [diff] [blame] | 19 | from jinja2.utils import import_string, Markup |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 20 | |
| 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. |
| 25 | GETTEXT_FUNCTIONS = ('_', 'gettext', 'ngettext') |
Armin Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class Extension(object): |
Armin Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame^] | 29 | """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 Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 36 | |
| 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 Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame^] | 43 | 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 Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 50 | def parse(self, parser): |
| 51 | """Called if one of the tags matched.""" |
| 52 | |
| 53 | |
| 54 | class CacheExtension(Extension): |
Armin Ronacher | 2b60fe5 | 2008-04-21 08:23:59 +0200 | [diff] [blame] | 55 | """An example extension that adds cacheable blocks.""" |
Armin Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 56 | tags = set(['cache']) |
| 57 | |
Armin Ronacher | 203bfcb | 2008-04-24 21:54:44 +0200 | [diff] [blame] | 58 | 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 Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 65 | def parse(self, parser): |
| 66 | lineno = parser.stream.next().lineno |
| 67 | args = [parser.parse_expression()] |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 68 | if parser.stream.current.type is 'comma': |
| 69 | parser.stream.next() |
Armin Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 70 | args.append(parser.parse_expression()) |
| 71 | body = parser.parse_statements(('name:endcache',), drop_needle=True) |
| 72 | return nodes.CallBlock( |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 73 | nodes.Call(nodes.Name('cache_support', 'load'), args, [], None, None), |
Armin Ronacher | 0553093 | 2008-04-20 13:27:49 +0200 | [diff] [blame] | 74 | [], [], body |
| 75 | ) |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 76 | |
| 77 | |
| 78 | class 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 Ronacher | 2feed1d | 2008-04-26 16:26:52 +0200 | [diff] [blame] | 206 | return referenced, concat(buf) |
Armin Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 207 | |
| 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 Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 215 | |
| 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 Ronacher | d84ec46 | 2008-04-29 13:43:16 +0200 | [diff] [blame] | 224 | |
| 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 Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 232 | return nodes.Output([node]) |
| 233 | |
| 234 | |
| 235 | def 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 | |
| 267 | def 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 Ronacher | 7259c76 | 2008-04-30 13:03:59 +0200 | [diff] [blame^] | 305 | # 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 Ronacher | b5124e6 | 2008-04-25 00:36:14 +0200 | [diff] [blame] | 309 | ) |
| 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, [] |