[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
Provenance
- Commit
3b65b8a- Tree
dad1103- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5887538
6 files changed+61 added−12 removed
modified:jinja/translators/python.py+3 added-3 removed
357 unmodified lines358 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 lines535 self.last_cycle_id = 0536 537 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 linesmodified:jinja/filters.py+13 added-8 removed
31 unmodified lines32 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 decorator43 44 7 unmodified lines52 """53 if count is None:54 return s.replace(old, new)55 return s.replace(old, new, count)56+do_replace = stringfilter(do_replace)57 58 59 def do_upper(s):60 """1 unmodified line62 63 Return a copy of s converted to uppercase.64 """65 return s.upper()66+do_upper = stringfilter(do_upper)67 68 69 def do_lower(s):70 """1 unmodified line72 73 Return a copy of s converted to lowercase.74 """75 return s.lower()76+do_lower = stringfilter(do_lower)77 78 79 def do_escape(s, attribute=False):80 """5 unmodified lines86 s = s.replace("&", "&").replace("<", "<").replace(">", ">")87 if attribute:88 s = s.replace('"', """)89 return s90+escape = stringfilter(do_escape)91 92 93 def do_addslashes(s):94 """1 unmodified line96 97 Adds slashes to s.98 """99 return s.encode('utf-8').encode('string-escape').decode('utf-8')100+do_addslashes = stringfilter(do_addslashes)101 102 103 def do_capitalize(s):104 """2 unmodified lines107 Return a copy of the string s with only its first character108 capitalized.109 """110 return s.capitalize()111+do_capitalize = stringfilter(do_capitalize)112 113 114 def do_title(s):115 """2 unmodified lines118 Return a titlecased version of s, i.e. words start with uppercase119 characters, all remaining cased characters have lowercase.120 """121 return s.title()122+do_title = stringfilter(do_title)123 124 125 def do_default(default_value=u''):126 """2 unmodified lines129 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_value133+do_default = stringfilter(do_default)134 135 136 def do_join(d=u''):137 """15 unmodified lines153 154 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 linesmodified:jinja/lexer.py+3 added
1 # -*- coding: utf-8 -*-2 """3 jinja.lexer4 ~~~~~~~~~~~5+6+ :copyright: 2006 by Armin Ronacher.7+ :license: BSD, see LICENSE for more details.8 """9 import re10 from jinja.datastructure import TokenStream11 from jinja.exceptions import TemplateSyntaxError190 unmodified linesmodified:jinja/nodes.py+4 added-1 removed
17 unmodified lines18 """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())27 28 29 class Node(ast.Node):163 unmodified linesadded:jinja/template.py+28 added
1+# -*- coding: utf-8 -*-2+"""3+ jinja.template4+ ~~~~~~~~~~~~~~5+6+ Template class.7+8+ :copyright: 2006 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+11+"""12+from jinja.datastructure import Context13+14+15+class Template(object):16+ """17+ Represents a finished template.18+ """19+20+ def __init__(self, environment, generate_func):21+ self.environment = environment22+ self.generate_func = generate_func23+24+ 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 lines10 def test_parser(x):11 from jinja.parser import Parser12 from jinja.translators.python import translate13 print translate(e, Parser(e, x).parse())14+15+16+def 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'])