Skip to content
Myr

[svn] added minimal template evaluator

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 5887538 · Browse files · 6 files+61 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+61 added−12 removed

  • other:+61 added−12 removed

6 files+61 added−12 removed

modified:jinja/translators/python.py+3 added-3 removed
357 unmodified lines
358 n.node.name,359 args or ''360 ))361 elif n.__class__ is ast.Name:362 filters.append('environment.prepare_filter(%s)' %363 self.handle_node(n))362 filters.append('environment.prepare_filter(%r)' %363 n.name)364 else:365 raise TemplateSyntaxError('invalid filter. filter must be a '366 'hardcoded function name from the '367 'filter namespace',
167 unmodified lines
535 self.last_cycle_id = 0536537 def translate(self):538 return (539 'from jinja.datastructures import Undefined, LoopContext, CycleContext\n'539 'from jinja.datastructure import Undefined, LoopContext, CycleContext\n'540 'def generate(context, write, write_var=None):\n'541 ' environment = context.environment\n'542 ' if write_var is None:\n'543 ' write_var = lambda x: write(environment.finish_var(x))\n'
8 unmodified lines
modified:jinja/filters.py+13 added-8 removed
31 unmodified lines
32 Decorator for filters that just work on unicode objects.33 """34 def decorator(*args):35 def wrapped(env, context, value):36 args = list(args)37 for idx, var in enumerate(args):36 nargs = list(args)37 for idx, var in enumerate(nargs):38 if isinstance(var, str):39 args[idx] = env.to_unicode(var)40 return f(env.to_unicode(value), *args)39 nargs[idx] = env.to_unicode(var)40 return f(env.to_unicode(value), *nargs)41 return wrapped42 return decorator4344
7 unmodified lines
52 """53 if count is None:54 return s.replace(old, new)55 return s.replace(old, new, count)56do_replace = stringfilter(do_replace)575859def do_upper(s):60 """
1 unmodified line
6263 Return a copy of s converted to uppercase.64 """65 return s.upper()66do_upper = stringfilter(do_upper)676869def do_lower(s):70 """
1 unmodified line
7273 Return a copy of s converted to lowercase.74 """75 return s.lower()76do_lower = stringfilter(do_lower)777879def do_escape(s, attribute=False):80 """
5 unmodified lines
86 s = s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")87 if attribute:88 s = s.replace('"', "&quot;")89 return s90escape = stringfilter(do_escape)919293def do_addslashes(s):94 """
1 unmodified line
9697 Adds slashes to s.98 """99 return s.encode('utf-8').encode('string-escape').decode('utf-8')100do_addslashes = stringfilter(do_addslashes)101102103def do_capitalize(s):104 """
2 unmodified lines
107 Return a copy of the string s with only its first character108 capitalized.109 """110 return s.capitalize()111do_capitalize = stringfilter(do_capitalize)112113114def do_title(s):115 """
2 unmodified lines
118 Return a titlecased version of s, i.e. words start with uppercase119 characters, all remaining cased characters have lowercase.120 """121 return s.title()122do_title = stringfilter(do_title)123124125def do_default(default_value=u''):126 """
2 unmodified lines
129 In case of s isn't set or True default will return default_value130 which is '' per default.131 """132 return lambda e, c, v: v or default_value133do_default = stringfilter(do_default)134135136def do_join(d=u''):137 """
15 unmodified lines
153154 Return the length of var. In case if getting an integer or float155 it will convert it into a string an return the length of the new156 string.149 If the object doesn't provide a __len__ function it will return150 zero.st(value)151 l.reverse()152 return 157 If the object doesn't provide a __len__ function it will return zero158 """159 def wrapped(env, context, value):160 try:161 if type(value) in (int, float, long):
170 unmodified lines
modified:jinja/lexer.py+3 added
1# -*- coding: utf-8 -*-2"""3 jinja.lexer4 ~~~~~~~~~~~56 :copyright: 2006 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""9import re10from jinja.datastructure import TokenStream11from jinja.exceptions import TemplateSyntaxError
190 unmodified lines
modified:jinja/nodes.py+4 added-1 removed
17 unmodified lines
18 """19 todo = [tree]20 while todo:21 node = todo.pop()22 if node.lineno:23 node.lineno += offset - 124 else:22 node.lineno = (node.lineno or 0) + offset25 node.lineno = offset26 todo.extend(node.getChildNodes())272829class Node(ast.Node):
163 unmodified lines
added:jinja/template.py+28 added
1# -*- coding: utf-8 -*-2"""3 jinja.template4 ~~~~~~~~~~~~~~56 Template class.78 :copyright: 2006 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.1011"""12from jinja.datastructure import Context131415class Template(object):16 """17 Represents a finished template.18 """1920 def __init__(self, environment, generate_func):21 self.environment = environment22 self.generate_func = generate_func2324 def render(self, *args, **kwargs):25 result = []26 ctx = Context(self.environment, *args, **kwargs)27 self.generate_func(ctx, result.append)28 return u''.join(result)
modified:test.py+10 added
9 unmodified lines
10def test_parser(x):11 from jinja.parser import Parser12 from jinja.translators.python import translate13 print translate(e, Parser(e, x).parse())141516def load_template(x):17 from jinja.template import Template18 from jinja.parser import Parser19 from jinja.translators.python import translate20 code = translate(e, Parser(e, x).parse())21 ns = {}22 exec code in ns23 return Template(e, ns['generate'])