Parse a translatable tag.
(self, parser)
| 215 | return extract_from_ast(source, gettext_functions) |
| 216 | |
| 217 | def parse(self, parser): |
| 218 | """Parse a translatable tag.""" |
| 219 | lineno = next(parser.stream).lineno |
| 220 | num_called_num = False |
| 221 | |
| 222 | # find all the variables referenced. Additionally a variable can be |
| 223 | # defined in the body of the trans block too, but this is checked at |
| 224 | # a later state. |
| 225 | plural_expr = None |
| 226 | plural_expr_assignment = None |
| 227 | variables = {} |
| 228 | trimmed = None |
| 229 | while parser.stream.current.type != 'block_end': |
| 230 | if variables: |
| 231 | parser.stream.expect('comma') |
| 232 | |
| 233 | # skip colon for python compatibility |
| 234 | if parser.stream.skip_if('colon'): |
| 235 | break |
| 236 | |
| 237 | name = parser.stream.expect('name') |
| 238 | if name.value in variables: |
| 239 | parser.fail('translatable variable %r defined twice.' % |
| 240 | name.value, name.lineno, |
| 241 | exc=TemplateAssertionError) |
| 242 | |
| 243 | # expressions |
| 244 | if parser.stream.current.type == 'assign': |
| 245 | next(parser.stream) |
| 246 | variables[name.value] = var = parser.parse_expression() |
| 247 | elif trimmed is None and name.value in ('trimmed', 'notrimmed'): |
| 248 | trimmed = name.value == 'trimmed' |
| 249 | continue |
| 250 | else: |
| 251 | variables[name.value] = var = nodes.Name(name.value, 'load') |
| 252 | |
| 253 | if plural_expr is None: |
| 254 | if isinstance(var, nodes.Call): |
| 255 | plural_expr = nodes.Name('_trans', 'load') |
| 256 | variables[name.value] = plural_expr |
| 257 | plural_expr_assignment = nodes.Assign( |
| 258 | nodes.Name('_trans', 'store'), var) |
| 259 | else: |
| 260 | plural_expr = var |
| 261 | num_called_num = name.value == 'num' |
| 262 | |
| 263 | parser.stream.expect('block_end') |
| 264 | |
| 265 | plural = None |
| 266 | have_plural = False |
| 267 | referenced = set() |
| 268 | |
| 269 | # now parse until endtrans or pluralize |
| 270 | singular_names, singular = self._parse_block(parser, True) |
| 271 | if singular_names: |
| 272 | referenced.update(singular_names) |
| 273 | if plural_expr is None: |
| 274 | plural_expr = nodes.Name(singular_names[0], 'load') |
nothing calls this directly
no test coverage detected