|
16 | 16 | from mako import filters |
17 | 17 | from mako import parsetree |
18 | 18 | from mako import util |
| 19 | +from mako.filters import DEFAULT_ESCAPE_PREFIX |
19 | 20 | from mako.pygen import PythonPrinter |
20 | 21 |
|
21 | 22 |
|
|
26 | 27 | # context itself |
27 | 28 | TOPLEVEL_DECLARED = {"UNDEFINED", "STOP_RENDERING"} |
28 | 29 | RESERVED_NAMES = {"context", "loop"}.union(TOPLEVEL_DECLARED) |
| 30 | +DEFAULT_ESCAPED_N = "%sn" % DEFAULT_ESCAPE_PREFIX |
29 | 31 |
|
30 | 32 |
|
31 | 33 | def compile( # noqa |
@@ -522,6 +524,7 @@ def write_variable_declares(self, identifiers, toplevel=False, limit=None): |
522 | 524 | self.printer.writeline("loop = __M_loop = runtime.LoopStack()") |
523 | 525 |
|
524 | 526 | for ident in to_write: |
| 527 | + ident = ident.replace(DEFAULT_ESCAPE_PREFIX, "") |
525 | 528 | if ident in comp_idents: |
526 | 529 | comp = comp_idents[ident] |
527 | 530 | if comp.is_block: |
@@ -785,25 +788,48 @@ def locate_encode(name): |
785 | 788 | else: |
786 | 789 | return filters.DEFAULT_ESCAPES.get(name, name) |
787 | 790 |
|
788 | | - if "n" not in args: |
| 791 | + filter_args = set() |
| 792 | + if DEFAULT_ESCAPED_N not in args: |
789 | 793 | if is_expression: |
790 | 794 | if self.compiler.pagetag: |
791 | 795 | args = self.compiler.pagetag.filter_args.args + args |
792 | | - if self.compiler.default_filters and "n" not in args: |
| 796 | + filter_args = set(self.compiler.pagetag.filter_args.args) |
| 797 | + if ( |
| 798 | + self.compiler.default_filters |
| 799 | + and DEFAULT_ESCAPED_N not in args |
| 800 | + ): |
793 | 801 | args = self.compiler.default_filters + args |
794 | 802 | for e in args: |
795 | | - # if filter given as a function, get just the identifier portion |
796 | | - if e == "n": |
| 803 | + if e == DEFAULT_ESCAPED_N: |
797 | 804 | continue |
| 805 | + |
| 806 | + if e.startswith(DEFAULT_ESCAPE_PREFIX): |
| 807 | + render_e = e.replace(DEFAULT_ESCAPE_PREFIX, "") |
| 808 | + is_default_filter = True |
| 809 | + else: |
| 810 | + render_e = e |
| 811 | + is_default_filter = False |
| 812 | + |
| 813 | + # if filter given as a function, get just the identifier portion |
798 | 814 | m = re.match(r"(.+?)(\(.*\))", e) |
799 | 815 | if m: |
800 | | - ident, fargs = m.group(1, 2) |
801 | | - f = locate_encode(ident) |
802 | | - e = f + fargs |
| 816 | + if not is_default_filter: |
| 817 | + ident, fargs = m.group(1, 2) |
| 818 | + f = locate_encode(ident) |
| 819 | + render_e = f + fargs |
| 820 | + target = "%s(%s)" % (render_e, target) |
| 821 | + elif is_default_filter and e not in filter_args: |
| 822 | + target = "%s(%s) if %s is not UNDEFINED else %s(%s)" % ( |
| 823 | + render_e, |
| 824 | + target, |
| 825 | + render_e, |
| 826 | + locate_encode(render_e), |
| 827 | + target, |
| 828 | + ) |
803 | 829 | else: |
804 | | - e = locate_encode(e) |
| 830 | + e = locate_encode(render_e) |
805 | 831 | assert e is not None |
806 | | - target = "%s(%s)" % (e, target) |
| 832 | + target = "%s(%s)" % (e, target) |
807 | 833 | return target |
808 | 834 |
|
809 | 835 | def visitExpression(self, node): |
|
0 commit comments