annotate genshi/template/inline.py @ 828:eb8aa8690480 experimental-inline

inline branch: template object can be compiled, and remembers the generated module.
author cmlenz
date Fri, 13 Mar 2009 16:06:42 +0000
parents 8ebccfa9a9fe
children 09cc3627654c
rev   line source
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
2 #
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2006 Edgewall Software
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
4 # All rights reserved.
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
5 #
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
9 #
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
13
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
14 import imp
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
15
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
16 from genshi.core import Attrs, Stream, _ensure, START, END, TEXT
821
abb1f1d2f4f3 inline-branch: Fix for `for` directive.
cmlenz
parents: 820
diff changeset
17 from genshi.template.astutil import _ast
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
18 from genshi.template.base import EXEC, EXPR, SUB
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
19 from genshi.template.directives import *
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
20
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
21
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
22 class CodeWriter(object):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
23
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
24 def __init__(self):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
25 self.indent = 0
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
26
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
27 def __call__(self, line='', *args):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
28 if not line:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
29 return ''
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
30 if args:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
31 line %= args
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
32 return ' ' * self.indent + line
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
33
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
34 def shift(self):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
35 self.indent += 4
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
36
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
37 def unshift(self):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
38 self.indent -= 4
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
39
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
40
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
41 def _expand(obj, pos):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
42 if obj is not None:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
43 # First check for a string, otherwise the iterable test below
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
44 # succeeds, and the string will be chopped up into individual
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
45 # characters
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
46 if isinstance(obj, basestring):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
47 yield TEXT, obj, pos
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
48 elif isinstance(obj, (int, float, long)):
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
49 yield TEXT, unicode(obj), pos
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
50 elif hasattr(obj, '__iter__'):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
51 for event in _ensure(obj):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
52 yield event
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
53 else:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
54 yield TEXT, unicode(obj), pos
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
55
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
56
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
57 def _expand_text(obj):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
58 if obj is not None:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
59 if isinstance(obj, basestring):
356
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
60 return [obj]
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
61 elif isinstance(obj, (int, float, long)):
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
62 return [unicode(result)]
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
63 elif hasattr(obj, '__iter__'):
356
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
64 return [e[1] for e in _ensure(obj) if e[0] is TEXT]
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
65 else:
356
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
66 return [unicode(obj)]
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
67 return []
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
68
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
69
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
70 def _assign(ast):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
71 buf = []
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
72 def _build(node, indices):
821
abb1f1d2f4f3 inline-branch: Fix for `for` directive.
cmlenz
parents: 820
diff changeset
73 if isinstance(node, _ast.Tuple):
abb1f1d2f4f3 inline-branch: Fix for `for` directive.
cmlenz
parents: 820
diff changeset
74 for idx, elt in enumerate(node.elts):
abb1f1d2f4f3 inline-branch: Fix for `for` directive.
cmlenz
parents: 820
diff changeset
75 _build(elt, indices + (idx,))
abb1f1d2f4f3 inline-branch: Fix for `for` directive.
cmlenz
parents: 820
diff changeset
76 elif isinstance(node, _ast.Name):
abb1f1d2f4f3 inline-branch: Fix for `for` directive.
cmlenz
parents: 820
diff changeset
77 buf.append('%r: v%s' % (node.id, ''.join(['[%s]' % i for i in indices])))
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
78 _build(ast, ())
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
79 return '{%s}' % ', '.join(buf)
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
80
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
81
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
82 def inline(template):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
83 w = CodeWriter()
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
84
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
85 yield w('from genshi.core import Attrs, QName')
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
86 yield w('from genshi.core import START, START_CDATA, START_NS, END, '
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
87 'END_CDATA, END_NS, DOCTYPE, TEXT')
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
88 yield w('from genshi.path import Path')
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
89 yield w('from genshi.template.eval import Expression, Suite')
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
90 yield w('from genshi.template.inline import _expand, _expand_text')
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
91 yield w()
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
92
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
93 def _declare_vars(stream):
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
94 for kind, data, pos in stream:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
95
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
96 if kind is START:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
97 tagname, attrs = data
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
98 yield 'Q', tagname, tagname
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
99
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
100 sattrs = Attrs([(n, v) for n, v in attrs
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
101 if isinstance(v, basestring)])
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
102 for name, val in [(n, v) for n, v in attrs
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
103 if not isinstance(v, basestring)]:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
104 yield 'Q', name, name
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
105 for subkind, subdata, subpos in val:
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
106 if subkind is EXPR:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
107 yield 'E', subdata, subdata
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
108
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
109 yield 'A', tuple(sattrs), sattrs
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
110
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
111 elif kind is EXPR:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
112 yield 'E', data, data
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
113
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
114 elif kind is EXEC:
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
115 yield 'S', data, data
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
116
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
117 elif kind is SUB:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
118 directives, substream = data
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
119 for directive in directives:
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
120
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
121 if directive.expr:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
122 yield 'E', directive.expr, directive.expr
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
123
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
124 elif hasattr(directive, 'vars'):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
125 for _, expr in directive.vars:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
126 yield 'E', expr, expr
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
127
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
128 elif hasattr(directive, 'path') and directive.path:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
129 yield 'P', directive.path, directive.path
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
130
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
131 for line in _declare_vars(substream):
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
132 yield line
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
133
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
134 def _declare_functions(stream, names):
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
135 for kind, data, pos in stream:
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
136 if kind is SUB:
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
137 directives, substream = data
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
138 for idx, directive in enumerate(directives):
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
139 if isinstance(directive, DefDirective):
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
140 names.append(directive.name)
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
141 yield w('def %s:', directive.signature)
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
142 w.shift()
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
143 args = ['%r: %s' % (name, name) for name
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
144 in directive.args]
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
145 yield w('push({%s})', ', '.join(args))
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
146 for line in _apply(directives[idx + 1:], substream):
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
147 yield line
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
148 yield w('pop()')
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
149 w.unshift()
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
150
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
151 # Recursively apply directives
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
152 def _apply(directives, stream):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
153 if not directives:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
154 for line in _generate(stream):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
155 yield line
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
156 return
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
157
398
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
158 d = directives[0]
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
159 rest = directives[1:]
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
160
398
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
161 if isinstance(d, DefDirective):
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
162 return # already added
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
163
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
164 yield w()
398
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
165 yield w('# Applying %r', d)
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
166
398
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
167 if isinstance(d, ForDirective):
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
168 yield w('for v in e[%d].evaluate(ctxt):', index['E'][d.expr])
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
169 w.shift()
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
170 yield w('push(%s)', _assign(d.target))
398
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
171 for line in _apply(rest, stream):
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
172 yield line
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
173 yield w('pop()')
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
174 w.unshift()
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
175
398
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
176 elif isinstance(d, IfDirective):
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
177 yield w('if e[%d].evaluate(ctxt):', index['E'][d.expr])
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
178 w.shift()
398
49aa525b8f83 inline branch: Merged [480:486/trunk].
cmlenz
parents: 359
diff changeset
179 for line in _apply(rest, stream):
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
180 yield line
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
181 w.unshift()
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
182
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
183 else:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
184 raise NotImplementedError
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
185
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
186 yield w()
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
187
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
188 # Generate code for the given template stream
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
189 def _generate(stream):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
190 for kind, data, pos in stream:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
191
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
192 if kind is EXPR:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
193 yield w('for evt in _expand(e[%d].evaluate(ctxt), (f, %d, %d)): yield evt',
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
194 index['E'][data], *pos[1:])
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
195
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
196 elif kind is EXEC:
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
197 yield w('s[%d].execute(ctxt)', index['S'][data])
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
198
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
199 elif kind is START:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
200 tagname, attrs = data
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
201 qn = index['Q'][tagname]
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
202
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
203 sattrs = Attrs([(n, v) for n, v in attrs
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
204 if isinstance(v, basestring)])
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
205 at = index['A'][tuple(sattrs)]
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
206 if filter(None, [not isinstance(v, basestring) for n,v in attrs]):
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
207 yield w('at = [(an, "".join(av)) for an, av in ([')
356
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
208 w.shift()
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
209 for name, value in [(n, v) for n, v in attrs
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
210 if not isinstance(v, basestring)]:
356
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
211 values = []
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
212 for subkind, subdata, subpos in value:
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
213 if subkind is EXPR:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
214 values.append('_expand_text(e[%d].evaluate(ctxt))' %
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
215 index['E'][subdata])
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
216 elif subkind is TEXT:
356
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
217 values.append('[%r]' % subdata)
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
218 yield w('(q[%d], [v for v in %s if v is not None]),' % (
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
219 index['Q'][name], ' + '.join(values)
356
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
220 ))
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
221 w.unshift()
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
222 yield w(']) if av]')
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
223 yield w('yield START, (q[%d], a[%d] | at), (f, %d, %d)', qn, at,
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
224 *pos[1:])
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
225 else:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
226 yield w('yield START, (q[%d], a[%d]), (f, %d, %d)', qn, at, *pos[1:])
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
227
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
228 elif kind is END:
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
229 yield w('yield END, q[%d], (f, %d, %d)', index['Q'][data], *pos[1:])
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
230
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
231 elif kind is SUB:
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
232 directives, substream = data
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
233 for line in _apply(directives, substream):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
234 yield line
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
235
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
236 else:
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
237 yield w('yield %s, %r, (f, %d, %d)', kind, data, *pos[1:])
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
238
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
239 yield w('_F = %r', template.filename)
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
240 yield w()
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
241
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
242 yield '# Create qnames, attributes, expressions, and suite objects'
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
243 index, counter, values = {}, {}, {}
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
244 for prefix, key, value in _declare_vars(template.stream):
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
245 if not prefix in counter:
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
246 counter[prefix] = 0
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
247 if key not in index.get(prefix, ()):
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
248 index.setdefault(prefix, {})[key] = counter[prefix]
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
249 counter[prefix] += 1
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
250 values.setdefault(prefix, []).append(value)
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
251 for prefix in sorted(values.keys()):
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
252 yield w('_%s = (', prefix)
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
253 for value in values[prefix]:
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
254 yield w(' ' + repr(value) + ',')
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
255 yield w(')')
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
256 yield w()
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
257
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
258 yield w('def generate(ctxt, %s):',
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
259 ', '.join(['f=_F'] + ['%s=_%s' % (n.lower(), n) for n in index]))
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
260 w.shift()
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
261 yield w('push = ctxt.push; pop = ctxt.pop')
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
262 yield w()
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
263
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
264 # Define macro functions
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
265 defs = []
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
266 for line in _declare_functions(template.stream, names=defs):
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
267 yield line
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
268 if defs:
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
269 yield w()
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
270 yield w('push({%s})', ', '.join('%r: %s' % (n, n) for n in defs))
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
271 yield w()
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
272
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
273 ei, pi = [0], [0]
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
274 for line in _generate(template.stream):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
275 yield line
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
276
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
277
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
278 if __name__ == '__main__':
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
279 import timeit
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
280 from genshi.template import Context, MarkupTemplate
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
281
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
282 text = """<html xmlns="http://www.w3.org/1999/xhtml"
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
283 xmlns:py="http://genshi.edgewall.org/"
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
284 lang="en">
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
285 <?python
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
286 def foo(x):
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
287 return x*x
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
288 ?>
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
289 <body>
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
290 <h1 py:def="sayhi(name='world')" py:strip="">
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
291 Hello, $name!
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
292 </h1>
342
3d29249e0524 inline branch: support for the def directive.
cmlenz
parents: 341
diff changeset
293 ${sayhi()}
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
294 <ul py:if="items">
356
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
295 <li py:for="idx, item in enumerate(items)"
ada22378f12c inline branch: minor tweaks, removed currently unsupported directives
cmlenz
parents: 355
diff changeset
296 class="${idx % 2 and 'odd' or 'even'}">
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
297 <span py:replace="item + 1">NUM</span>
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
298 </li>
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
299 </ul>
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
300 </body>
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
301 </html>"""
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
302
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
303 ctxt = Context(hello='world', items=range(10))
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
304 tmpl = MarkupTemplate(text)
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
305
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
306 print 'Generated source:'
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
307 for idx, line in enumerate(inline(tmpl)):
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
308 print '%3d %s' % (idx + 1, line)
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
309
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
310 print
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
311 print 'Interpreted template:'
826
8ebccfa9a9fe inline branch: Add code block support to the template inliner, and some tweaks/cleanup.
cmlenz
parents: 821
diff changeset
312 print tmpl.generate(ctxt).render('html')
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
313
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
314 print
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
315 print 'Executed module:'
828
eb8aa8690480 inline branch: template object can be compiled, and remembers the generated module.
cmlenz
parents: 826
diff changeset
316 tmpl.compile()
eb8aa8690480 inline branch: template object can be compiled, and remembers the generated module.
cmlenz
parents: 826
diff changeset
317 print tmpl.generate(ctxt).render('html')
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
318
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
319 print
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
320 print
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
321 t = timeit.Timer('list(tmpl.generate(**data))', '''
828
eb8aa8690480 inline branch: template object can be compiled, and remembers the generated module.
cmlenz
parents: 826
diff changeset
322 from genshi.template import MarkupTemplate
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
323 data = dict(hello='world', items=range(10))
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
324 tmpl = MarkupTemplate("""%s""")''' % text)
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
325 print 'Interpreted: %.2f msec/pass' % (1000 * t.timeit(number=1000) / 1000)
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
326 print
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
327
828
eb8aa8690480 inline branch: template object can be compiled, and remembers the generated module.
cmlenz
parents: 826
diff changeset
328 t = timeit.Timer('list(tmpl.generate(**data))', '''
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
329 from genshi.core import Stream
828
eb8aa8690480 inline branch: template object can be compiled, and remembers the generated module.
cmlenz
parents: 826
diff changeset
330 from genshi.template import MarkupTemplate
348
72937f87d519 inline branch: adapted to the immutble `Attrs` change, and various other fixes.
cmlenz
parents: 342
diff changeset
331 data = dict(hello='world', items=range(10))
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
332 tmpl = MarkupTemplate("""%s""")
828
eb8aa8690480 inline branch: template object can be compiled, and remembers the generated module.
cmlenz
parents: 826
diff changeset
333 tmpl.compile()''' % text)
359
f53be1b18b82 inline branch: use array to predeclare qnames, attrs, and expressions.
cmlenz
parents: 356
diff changeset
334 print 'Compiled: %.2f msec/pass' % (1000 * t.timeit(number=1000) / 1000)
339
83c3d04bba97 First code for the `inline` branch? only bits and pieces working right now.
cmlenz
parents:
diff changeset
335 print
Copyright (C) 2012-2017 Edgewall Software