]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
[svn] fixes in jinja i18n and finished macro calling
authorArmin Ronacher <armin.ronacher@active-4.com>
Mon, 5 Mar 2007 18:13:57 +0000 (19:13 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Mon, 5 Mar 2007 18:13:57 +0000 (19:13 +0100)
--HG--
branch : trunk

jinja/parser.py
jinja/translators/python.py

index 1bcd0b2707840d7c2d1fdab5b42467df697a7cc0..98553bcf6ebc55ce5699143aa8256218ee5a388f 100644 (file)
@@ -258,35 +258,40 @@ class Parser(object):
         flineno = lineno
         try:
             # check for string translations
-            lineno, token, data = gen.next()
-            if token == 'string':
-                # check that there are not any more elements
-                try:
-                    gen.next()
-                except StopIteration:
-                    #XXX: what about escapes?
-                    return nodes.Trans(lineno, data[1:-1], None, None, None)
-                raise TemplateSyntaxError('string based translations '
-                                          'require at most one argument.',
-                                          lineno)
-
-            # create a new generator with the popped item as first one
-            def wrapgen(oldgen):
-                yield lineno, token, data
-                for item in oldgen:
-                    yield item
-            gen = wrapgen(gen)
-
-            # block based translations
-            first_var = None
-            replacements = {}
-            for arg in self.parse_python(lineno, gen, '_trans(%s)').expr.args:
-                if arg.__class__ is not ast.Keyword:
-                    raise TemplateSyntaxError('translation tags need explicit '
-                                              'names for values.', lineno)
-                if first_var is None:
-                    first_var = arg.name
-                replacements[arg.name] = arg.expr
+            try:
+                lineno, token, data = gen.next()
+            except StopIteration:
+                # no dynamic replacements
+                replacements = {}
+                first_var = None
+            else:
+                if token == 'string':
+                    # check that there are not any more elements
+                    try:
+                        gen.next()
+                    except StopIteration:
+                        #XXX: what about escapes?
+                        return nodes.Trans(lineno, data[1:-1], None, None, None)
+                    raise TemplateSyntaxError('string based translations '
+                                              'require at most one argument.',
+                                              lineno)
+                # create a new generator with the popped item as first one
+                def wrapgen(oldgen):
+                    yield lineno, token, data
+                    for item in oldgen:
+                        yield item
+                gen = wrapgen(gen)
+
+                # block based translations
+                first_var = None
+                replacements = {}
+                for arg in self.parse_python(lineno, gen, '_trans(%s)').expr.args:
+                    if arg.__class__ is not ast.Keyword:
+                        raise TemplateSyntaxError('translation tags need explicit '
+                                                  'names for values.', lineno)
+                    if first_var is None:
+                        first_var = arg.name
+                    replacements[arg.name] = arg.expr
 
             # look for endtrans/pluralize
             buf = singular = []
index fd817837130dcfc1f9f5a6d90adca945b889f3d4..ce8f1dff4adae965d3572aef64ab8cfb9577b702 100644 (file)
@@ -375,16 +375,24 @@ class PythonTranslator(Translator):
         Handle macro declarations.
         """
         buf = []
+        write = lambda x: buf.append(self.indent(x))
 
         args = []
+        defaults = []
         for name, n in node.arguments:
+            args.append('context[\'%s\']' % name)
             if n is None:
-                args.append('%s=Undefined' % name)
+                defaults.append('Undefined')
             else:
-                args.append('%s=%s' % (name, self.handle_node(n)))
-        buf.append(self.indent('def macro(%s):' % ', '.join(args)))
+                defaults.append(self.handle_node(n))
+
+        write('def macro(*args):')
         self.indention += 1
+        write('%s = (args + %s[len(args):])' % (_to_tuple(args), _to_tuple(defaults)))
+        write('macrobuffer = []')
+        write('write = macrobuffer.append')
         buf.append(self.handle_node(node.body))
+        write('return u\'\'.join(macrobuffer)')
         self.indention -= 1
         buf.append(self.indent('context[%r] = macro' % node.name))