From: Armin Ronacher Date: Tue, 27 Feb 2007 19:21:45 +0000 (+0100) Subject: [svn] added minimal template evaluator X-Git-Tag: 2.0rc1~487 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3b65b8a950b9c0724ecb56c0e53d330c13dd201f;p=jinja2.git [svn] added minimal template evaluator --HG-- branch : trunk --- diff --git a/jinja/filters.py b/jinja/filters.py index 083f90a..a7bb9d3 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -33,11 +33,11 @@ def stringfilter(f): """ def decorator(*args): def wrapped(env, context, value): - args = list(args) - for idx, var in enumerate(args): + nargs = list(args) + for idx, var in enumerate(nargs): if isinstance(var, str): - args[idx] = env.to_unicode(var) - return f(env.to_unicode(value), *args) + nargs[idx] = env.to_unicode(var) + return f(env.to_unicode(value), *nargs) return wrapped return decorator @@ -53,6 +53,7 @@ def do_replace(s, old, new, count=None): if count is None: return s.replace(old, new) return s.replace(old, new, count) +do_replace = stringfilter(do_replace) def do_upper(s): @@ -62,6 +63,7 @@ def do_upper(s): Return a copy of s converted to uppercase. """ return s.upper() +do_upper = stringfilter(do_upper) def do_lower(s): @@ -71,6 +73,7 @@ def do_lower(s): Return a copy of s converted to lowercase. """ return s.lower() +do_lower = stringfilter(do_lower) def do_escape(s, attribute=False): @@ -84,6 +87,7 @@ def do_escape(s, attribute=False): if attribute: s = s.replace('"', """) return s +escape = stringfilter(do_escape) def do_addslashes(s): @@ -93,6 +97,7 @@ def do_addslashes(s): Adds slashes to s. """ return s.encode('utf-8').encode('string-escape').decode('utf-8') +do_addslashes = stringfilter(do_addslashes) def do_capitalize(s): @@ -103,6 +108,7 @@ def do_capitalize(s): capitalized. """ return s.capitalize() +do_capitalize = stringfilter(do_capitalize) def do_title(s): @@ -113,6 +119,7 @@ def do_title(s): characters, all remaining cased characters have lowercase. """ return s.title() +do_title = stringfilter(do_title) def do_default(default_value=u''): @@ -123,6 +130,7 @@ def do_default(default_value=u''): which is '' per default. """ return lambda e, c, v: v or default_value +do_default = stringfilter(do_default) def do_join(d=u''): @@ -146,10 +154,7 @@ def do_count(): Return the length of var. In case if getting an integer or float it will convert it into a string an return the length of the new string. - If the object doesn't provide a __len__ function it will return - zero.st(value) - l.reverse() - return + If the object doesn't provide a __len__ function it will return zero """ def wrapped(env, context, value): try: diff --git a/jinja/lexer.py b/jinja/lexer.py index 55a4497..5f2f72b 100644 --- a/jinja/lexer.py +++ b/jinja/lexer.py @@ -2,6 +2,9 @@ """ jinja.lexer ~~~~~~~~~~~ + + :copyright: 2006 by Armin Ronacher. + :license: BSD, see LICENSE for more details. """ import re from jinja.datastructure import TokenStream diff --git a/jinja/nodes.py b/jinja/nodes.py index 7d75c58..2109b67 100644 --- a/jinja/nodes.py +++ b/jinja/nodes.py @@ -19,7 +19,10 @@ def inc_lineno(offset, tree): todo = [tree] while todo: node = todo.pop() - node.lineno = (node.lineno or 0) + offset + if node.lineno: + node.lineno += offset - 1 + else: + node.lineno = offset todo.extend(node.getChildNodes()) diff --git a/jinja/template.py b/jinja/template.py new file mode 100644 index 0000000..ad969d3 --- /dev/null +++ b/jinja/template.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +""" + jinja.template + ~~~~~~~~~~~~~~ + + Template class. + + :copyright: 2006 by Armin Ronacher. + :license: BSD, see LICENSE for more details. + +""" +from jinja.datastructure import Context + + +class Template(object): + """ + Represents a finished template. + """ + + def __init__(self, environment, generate_func): + self.environment = environment + self.generate_func = generate_func + + def render(self, *args, **kwargs): + result = [] + ctx = Context(self.environment, *args, **kwargs) + self.generate_func(ctx, result.append) + return u''.join(result) diff --git a/jinja/translators/python.py b/jinja/translators/python.py index b8f52eb..278f873 100644 --- a/jinja/translators/python.py +++ b/jinja/translators/python.py @@ -359,8 +359,8 @@ class PythonTranslator(object): args or '' )) elif n.__class__ is ast.Name: - filters.append('environment.prepare_filter(%s)' % - self.handle_node(n)) + filters.append('environment.prepare_filter(%r)' % + n.name) else: raise TemplateSyntaxError('invalid filter. filter must be a ' 'hardcoded function name from the ' @@ -536,7 +536,7 @@ class PythonTranslator(object): def translate(self): return ( - 'from jinja.datastructures import Undefined, LoopContext, CycleContext\n' + 'from jinja.datastructure import Undefined, LoopContext, CycleContext\n' 'def generate(context, write, write_var=None):\n' ' environment = context.environment\n' ' if write_var is None:\n' diff --git a/test.py b/test.py index 8ce99bc..d535256 100644 --- a/test.py +++ b/test.py @@ -11,3 +11,13 @@ def test_parser(x): from jinja.parser import Parser from jinja.translators.python import translate print translate(e, Parser(e, x).parse()) + + +def load_template(x): + from jinja.template import Template + from jinja.parser import Parser + from jinja.translators.python import translate + code = translate(e, Parser(e, x).parse()) + ns = {} + exec code in ns + return Template(e, ns['generate'])