From decb73ba3d9d9810646aa852af6537c2aa498c1b Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Mon, 5 Mar 2007 19:13:57 +0100 Subject: [PATCH] [svn] fixes in jinja i18n and finished macro calling --HG-- branch : trunk --- jinja/parser.py | 63 ++++++++++++++++++++----------------- jinja/translators/python.py | 14 +++++++-- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/jinja/parser.py b/jinja/parser.py index 1bcd0b2..98553bc 100644 --- a/jinja/parser.py +++ b/jinja/parser.py @@ -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 = [] diff --git a/jinja/translators/python.py b/jinja/translators/python.py index fd81783..ce8f1df 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -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)) -- 2.26.2