Generates a useful node from the data provided.
(
self,
singular: str,
plural: t.Optional[str],
context: t.Optional[str],
variables: t.Dict[str, nodes.Expr],
plural_expr: t.Optional[nodes.Expr],
vars_referenced: bool,
num_called_num: bool,
)
| 514 | return referenced, concat(buf) |
| 515 | |
| 516 | def _make_node( |
| 517 | self, |
| 518 | singular: str, |
| 519 | plural: t.Optional[str], |
| 520 | context: t.Optional[str], |
| 521 | variables: t.Dict[str, nodes.Expr], |
| 522 | plural_expr: t.Optional[nodes.Expr], |
| 523 | vars_referenced: bool, |
| 524 | num_called_num: bool, |
| 525 | ) -> nodes.Output: |
| 526 | """Generates a useful node from the data provided.""" |
| 527 | newstyle = self.environment.newstyle_gettext # type: ignore |
| 528 | node: nodes.Expr |
| 529 | |
| 530 | # no variables referenced? no need to escape for old style |
| 531 | # gettext invocations only if there are vars. |
| 532 | if not vars_referenced and not newstyle: |
| 533 | singular = singular.replace("%%", "%") |
| 534 | if plural: |
| 535 | plural = plural.replace("%%", "%") |
| 536 | |
| 537 | func_name = "gettext" |
| 538 | func_args: t.List[nodes.Expr] = [nodes.Const(singular)] |
| 539 | |
| 540 | if context is not None: |
| 541 | func_args.insert(0, nodes.Const(context)) |
| 542 | func_name = f"p{func_name}" |
| 543 | |
| 544 | if plural_expr is not None: |
| 545 | func_name = f"n{func_name}" |
| 546 | func_args.extend((nodes.Const(plural), plural_expr)) |
| 547 | |
| 548 | node = nodes.Call(nodes.Name(func_name, "load"), func_args, [], None, None) |
| 549 | |
| 550 | # in case newstyle gettext is used, the method is powerful |
| 551 | # enough to handle the variable expansion and autoescape |
| 552 | # handling itself |
| 553 | if newstyle: |
| 554 | for key, value in variables.items(): |
| 555 | # the function adds that later anyways in case num was |
| 556 | # called num, so just skip it. |
| 557 | if num_called_num and key == "num": |
| 558 | continue |
| 559 | node.kwargs.append(nodes.Keyword(key, value)) |
| 560 | |
| 561 | # otherwise do that here |
| 562 | else: |
| 563 | # mark the return value as safe if we are in an |
| 564 | # environment with autoescaping turned on |
| 565 | node = nodes.MarkSafeIfAutoescape(node) |
| 566 | if variables: |
| 567 | node = nodes.Mod( |
| 568 | node, |
| 569 | nodes.Dict( |
| 570 | [ |
| 571 | nodes.Pair(nodes.Const(key), value) |
| 572 | for key, value in variables.items() |
| 573 | ] |