[svn] last changes from yesterday so that i can continue working in school on it
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 20eacb7 · Browse files · 5 files·+76 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+76 added−5 removed
- other:+76 added−5 removed
Provenance
- Commit
19863ef- Tree
785020e- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
20eacb7
5 files changed+76 added−5 removed
modified:jinja/translators/python.py+14 added-2 removed
84 unmodified lines85 nodes.IfCondition: self.handle_if_condition,86 nodes.Cycle: self.handle_cycle,87 nodes.Print: self.handle_print,88 nodes.Macro: self.handle_macro,89+ nodes.Set: self.handle_set,90 nodes.Filter: self.handle_filter,91 nodes.Block: self.handle_block,92 nodes.Include: self.handle_include,93 # used python nodes42 unmodified lines136 return Template(environment,137 compile(translator.translate(), filename, 'exec'))138 process = staticmethod(process)139 139- # -- private methods140+ # -- private helper methods141 142 def indent(self, text):143 """144 Indent the current text.1 unmodified line146 return (' ' * (self.indention * 4)) + text147 148 def filter(self, s, filter_nodes):149 """149- Apply a filter on an object.150+ Apply a filter on an object that already is a python expression.151+ Used to avoid redundant code in bitor and the filter directive.152 """153 filters = []154 for n in filter_nodes:155 if n.__class__ is ast.CallFunc:59 unmodified lines215 block.replace(node.blocks[block.name])216 node = tmpl217 218 lines = [219+ 'from __future__ import division\n'220 'from jinja.datastructure import Undefined, LoopContext, CycleContext\n\n'221 'def generate(context, write):\n'222 ' # BOOTSTRAPPING CODE\n'223 ' environment = context.environment\n'153 unmodified lines377 buf.append(self.indent('context[%r] = macro' % node.name))378 379 return '\n'.join(buf)380 381+ def handle_set(self, node):382+ """383+ Handle variable assignments.384+ """385+ return self.indent('context[%r] = %s' % (386+ node.name,387+ self.handle_node(node.expr)388+ ))389+390 def handle_filter(self, node):391 """392 Handle filter sections.393 """327 unmodified linesmodified:jinja/datastructure.py+4 added-1 removed
164 unmodified lines165 Used by `Environment.iterate`.166 """167 168 jinja_allowed_attributes = ['index', 'index0', 'length', 'parent',169- 'even', 'odd', 'revindex0', 'revindex']169+ 'even', 'odd', 'revindex0', 'revindex',170+ 'first', 'last']171 172 def __init__(self, seq, parent, loop_function):173 self.loop_function = loop_function174 self.parent = parent18 unmodified lines193 revindex = property(lambda s: s._stack[-1]['length'] - s._stack[-1]['index'])194 length = property(lambda s: s._stack[-1]['length'])195 even = property(lambda s: s._stack[-1]['index'] % 2 == 0)196 odd = property(lambda s: s._stack[-1]['index'] % 2 == 1)197+ first = property(lambda s: s._stack[-1]['index'] == 0)198+ last = property(lambda s: s._stack[-1]['index'] == s._stack[-1]['length'] - 1)199 200 def __iter__(self):201 s = self._stack[-1]202 for idx, item in enumerate(s['seq']):93 unmodified linesmodified:jinja/nodes.py+20 added
201 unmodified lines202 self.body203 )204 205 206+class Set(Node):207+ """208+ Allow defining own variables.209+ """210+211+ def __init__(self, lineno, name, expr):212+ self.lineno = lineno213+ self.name = name214+ self.expr = expr215+216+ def get_items(self):217+ return [self.name, self.expr]218+219+ def __repr__(self):220+ return 'Set(%r, %r)' % (221+ self.name,222+ self.expr223+ )224+225+226 class Filter(Node):227 """228 Node for filter sections.229 """73 unmodified linesmodified:jinja/parser.py+16 added-2 removed
48 unmodified lines49 self.directives = {50 'for': self.handle_for_directive,51 'if': self.handle_if_directive,52 'cycle': self.handle_cycle_directive,53+ 'set': self.handle_set_directive,54 'filter': self.handle_filter_directive,55 'print': self.handle_print_directive,56 'macro': self.handle_macro_directive,57 'block': self.handle_block_directive,67 unmodified lines125 # ast is something like Discard(CallFunc(Name('_cycle'), ...))126 # skip that.127 return nodes.Cycle(lineno, ast.expr.args[0])128 129+ def handle_set_directive(self, lineno, gen):130+ """131+ Handle {% set foo = 'value of foo' %}.132+ """133+ try:134+ name = gen.next()135+ if name[1] != 'name' or gen.next()[1:] != ('operator', '='):136+ raise ValueError()137+ except (StopIteration, ValueError):138+ raise TemplateSyntaxError('invalid syntax for set', lineno)139+ ast = self.parse_python(lineno, gen, '(%s)')140+ return nodes.Set(lineno, name[1], ast.expr)141+142 def handle_filter_directive(self, lineno, gen):143 """144 Handle {% filter foo|bar %} directives.145 """18 unmodified lines164 return nodes.Print(lineno, arguments[0])165 166 def handle_macro_directive(self, lineno, gen):167 """154- Handle {% macro foo(bar, baz) %}.168+ Handle {% macro foo bar, baz %}.169 """170 try:171 macro_name = gen.next()172 except StopIteration:56 unmodified lines229 if len(tokens) != 1 or tokens[0][1] != 'string':230 raise TemplateSyntaxError('include requires a string', lineno)231 return nodes.Include(lineno, tokens[0][2][1:-1])232 219- def parse_python(self, lineno, gen, template='%s'):233+ def parse_python(self, lineno, gen, template):234 """235 Convert the passed generator into a flat string representing236 python sourcecode and return an ast node or raise a237 TemplateSyntaxError.114 unmodified linesadded:jdebug.py+22 added
1+# -*- coding: utf-8 -*-2+"""3+ jdebug4+ ~~~~~~5+6+ Helper module to simplify jinja debugging. Use7+8+ :copyright: 2006 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+from jinja import Environment12+from jinja.parser import Parser13+from jinja.translators.python import PythonTranslator14+15+16+__all__ = ['e', 't', 'p']17+18+e = Environment()19+t = e.from_string20+21+def p(x):22+ print PythonTranslator(e, Parser(e, x).parse()).translate()