[svn] added fist code for cycle and a few more filters in jinja
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 4271d77 · Browse files · 4 files·+165 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+165 added−1 removed
- other:+165 added−1 removed
Provenance
- Commit
fed86c1- Tree
9eaa9c9- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
4271d77
4 files changed+165 added−1 removed
modified:jinja/translators/python.py+11 added
35 unmodified lines36 nodes.Text: self.handle_template_text,37 nodes.NodeList: self.handle_node_list,38 nodes.ForLoop: self.handle_for_loop,39 nodes.IfCondition: self.handle_if_condition,40+ nodes.Cycle: self.handle_cycle,41 nodes.Print: self.handle_print,42 # used python nodes43 ast.Name: self.handle_name,44 ast.AssName: self.handle_name,110 unmodified lines155 buf.append(self.handle_node(node.else_))156 self.indention -= 1157 return '\n'.join(buf)158 159+ def handle_cycle(self, node):160+ """161+ Handle the cycle tag.162+ """163+ buf = []164+ write = lambda x: buf.append(self.indent(x))165+ write('# XXX: add some code here')166+ self.last_pos = node.pos167+ return '\n'.join(buf)168+169 def handle_print(self, node):170 """171 Handle a print statement.172 """286 unmodified linesmodified:jinja/filters.py+131 added-1 removed
6 unmodified lines7 8 :copyright: 2006 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11+from random import choice12+from urllib import urlencode, quote13 14 15+try:16+ _reversed = reversed17+except NameError:18+ # python2.3 compatibility hack for the do_reverse function19+ def _reversed(seq):20+ try:21+ return seq[::-1]22+ except:23+ try:24+ return list(seq)[::-1]25+ except:26+ raise TypeError('argument to _reversed must '27+ 'be a sequence')28+29+30 def stringfilter(f):31 """32 Decorator for filters that just work on unicode objects.33 """160 unmodified lines194 return l195 return wrapped196 197 198+def do_center(value, width=80):199+ """200+ {{ var|center(80) }}201+202+ Centers the value in a field of a given width.203+ """204+ return value.center(width)205+do_center = stringfilter(do_center)206+207+208+def do_title(value):209+ """210+ {{ var|title }}211+212+ Capitalize the first character of all words.213+ """214+ return value.title()215+do_title = stringfilter(do_title)216+217+218+def do_capitalize(value):219+ """220+ {{ var|capitalize }}221+222+ Capitalize the first character of the string.223+ """224+ return value.capitalize()225+do_capitalize = stringfilter(do_capitalize)226+227+228+def do_first():229+ """230+ {{ var|first }}231+232+ Return the frist item of a sequence or None.233+ """234+ def wrapped(env, context, seq):235+ try:236+ return iter(seq).next()237+ except StopIteration:238+ return239+ return wrapped240+241+242+def do_last():243+ """244+ {{ var|last }}245+246+ Return the last item of a sequence.247+ """248+ def wrapped(env, context, seq):249+ try:250+ return iter(_reversed(seq)).next()251+ except (TypeError, StopIteration):252+ return253+ return wrapped254+255+256+def do_random():257+ """258+ {{ var|random }}259+260+ Return a random item from the sequence.261+ """262+ def wrapped(env, context, seq):263+ try:264+ return choice(seq)265+ except:266+ return267+ return wrapped268+269+270+def do_urlencode():271+ """272+ {{ var|urlencode }}273+274+ {{ {'foo': 'bar'}|urlencode }}275+276+ urlencode a string or directory.277+ """278+ def wrapped(env, context, value):279+ if isinstance(value, dict):280+ tmp = {}281+ for key, value in value.iteritems():282+ tmp[env.to_unicode(key)] = env.to_unicode(value)283+ return urlencode(tmp)284+ else:285+ return quote(env.to_unicode(value))286+ return wrapped287+288+289+def do_jsonencode():290+ """291+ {{ var|jsonencode }}292+293+ JSON dump a variable. just works if simplejson is installed.294+ """295+ global simplejson296+ try:297+ simplejson298+ except NameError:299+ import simplejson300+ return lambda e, c, v: simplejson.dumps(v)301+302+303 FILTERS = {304 'replace': do_replace,305 'upper': do_upper,306 'lower': do_lower,6 unmodified lines313 'join': do_join,314 'count': do_count,315 'odd': do_odd,316 'even': do_even,195- 'reversed': do_reversed317+ 'reversed': do_reversed,318+ 'center': do_center,319+ 'title': do_title,320+ 'capitalize': do_capitalize,321+ 'first': do_first,322+ 'last': do_last,323+ 'random': do_random,324+ 'urlencode': do_urlencode,325+ 'jsonencode': do_jsonencode326 }modified:jinja/nodes.py+13 added
75 unmodified lines76 self.else_77 )78 79 80+class Cycle(Node):81+ """82+ A node that represents the cycle statement.83+ """84+85+ def __init__(self, pos, seq):86+ self.pos = pos87+ self.seq = seq88+89+ def __repr__(self):90+ return 'Cycle(%r)' % (self.seq,)91+92+93 class Print(Node):94 """95 A node that represents variable tags and print calls96 """7 unmodified linesmodified:jinja/parser.py+10 added
51 unmodified lines52 53 self.directives = {54 'for': self.handle_for_directive,55 'if': self.handle_if_directive,56+ 'cycle': self.handle_cycle_directive,57 'print': self.handle_print_directive58 }59 60 def handle_for_directive(self, pos, gen):29 unmodified lines90 self.close_remaining_block()91 92 return nodes.IfCondition(pos, ast.tests[0][0], body, else_)93 94+ def handle_cycle_directive(self, pos, gen):95+ """96+ Handle {% cycle foo, bar, baz %}.97+ """98+ ast = self.parse_python(pos, gen, '_cycle((%s))')99+ # ast is something like Discard(CallFunc(Name('_cycle'), ...))100+ # skip that.101+ return nodes.Cycle(pos, ast.expr.args[0])102+103 def handle_print_directive(self, pos, gen):104 """105 Handle {{ foo }} and {% print foo %}.106 """123 unmodified lines