Show More
@@ -47,12 +47,16 b' class engine(object):' | |||||
47 | self.filters = filters |
|
47 | self.filters = filters | |
48 | self.defaults = defaults |
|
48 | self.defaults = defaults | |
49 | self.cache = {} |
|
49 | self.cache = {} | |
|
50 | self.parsecache = {} | |||
50 |
|
51 | |||
51 | def process(self, t, map): |
|
52 | def process(self, t, map): | |
52 | '''Perform expansion. t is name of map element to expand. map contains |
|
53 | '''Perform expansion. t is name of map element to expand. map contains | |
53 | added elements for use during expansion. Is a generator.''' |
|
54 | added elements for use during expansion. Is a generator.''' | |
54 | tmpl = self.loader(t) |
|
55 | if t not in self.parsecache: | |
55 | iters = [self._process(tmpl, map)] |
|
56 | tmpl = self.loader(t) | |
|
57 | self.parsecache[t] = self._parse(tmpl) | |||
|
58 | parsed = self.parsecache[t] | |||
|
59 | iters = [self._process(parsed, map)] | |||
56 | while iters: |
|
60 | while iters: | |
57 | try: |
|
61 | try: | |
58 | item = iters[0].next() |
|
62 | item = iters[0].next() | |
@@ -100,7 +104,44 b' class engine(object):' | |||||
100 | self.cache[expr] = apply |
|
104 | self.cache[expr] = apply | |
101 | return self.cache[expr](get) |
|
105 | return self.cache[expr](get) | |
102 |
|
106 | |||
103 |
def _p |
|
107 | def _parse(self, tmpl): | |
|
108 | '''preparse a template''' | |||
|
109 | ||||
|
110 | parsed = [] | |||
|
111 | pos, stop = 0, len(tmpl) | |||
|
112 | while pos < stop: | |||
|
113 | n = tmpl.find('{', pos) | |||
|
114 | if n < 0: | |||
|
115 | parsed.append(('', tmpl[pos:stop])) | |||
|
116 | break | |||
|
117 | if n > 0 and tmpl[n - 1] == '\\': | |||
|
118 | # escaped | |||
|
119 | parsed.append(('', tmpl[pos:n + 1])) | |||
|
120 | pos = n + 1 | |||
|
121 | continue | |||
|
122 | if n > pos: | |||
|
123 | parsed.append(('', tmpl[pos:n])) | |||
|
124 | ||||
|
125 | pos = n | |||
|
126 | n = tmpl.find('}', pos) | |||
|
127 | if n < 0: | |||
|
128 | # no closing | |||
|
129 | parsed.append(('', tmpl[pos:stop])) | |||
|
130 | break | |||
|
131 | ||||
|
132 | expr = tmpl[pos + 1:n] | |||
|
133 | pos = n + 1 | |||
|
134 | ||||
|
135 | if '%' in expr: | |||
|
136 | parsed.append(('%', expr)) | |||
|
137 | elif '|' in expr: | |||
|
138 | parsed.append(('|', expr)) | |||
|
139 | else: | |||
|
140 | parsed.append(('g', expr)) | |||
|
141 | ||||
|
142 | return parsed | |||
|
143 | ||||
|
144 | def _process(self, parsed, map): | |||
104 | '''Render a template. Returns a generator.''' |
|
145 | '''Render a template. Returns a generator.''' | |
105 |
|
146 | |||
106 | def get(key): |
|
147 | def get(key): | |
@@ -111,36 +152,15 b' class engine(object):' | |||||
111 | v = v(**map) |
|
152 | v = v(**map) | |
112 | return v |
|
153 | return v | |
113 |
|
154 | |||
114 | pos, stop = 0, len(tmpl) |
|
155 | for t, e in parsed: | |
115 |
|
|
156 | if not t: | |
116 | n = tmpl.find('{', pos) |
|
157 | yield e | |
117 |
if |
|
158 | elif t is '|': | |
118 |
yield |
|
159 | yield self._filter(e, get, map) | |
119 |
|
|
160 | elif t is '%': | |
120 | if n > 0 and tmpl[n - 1] == '\\': |
|
161 | yield self._format(e, get, map) | |
121 | # escaped |
|
162 | elif t is 'g': | |
122 |
yield |
|
163 | yield get(e) | |
123 | pos = n + 1 |
|
|||
124 | continue |
|
|||
125 | if n > pos: |
|
|||
126 | yield tmpl[pos:n] |
|
|||
127 |
|
||||
128 | pos = n |
|
|||
129 | n = tmpl.find('}', pos) |
|
|||
130 | if n < 0: |
|
|||
131 | # no closing |
|
|||
132 | yield tmpl[pos:stop] |
|
|||
133 | break |
|
|||
134 |
|
||||
135 | expr = tmpl[pos + 1:n] |
|
|||
136 | pos = n + 1 |
|
|||
137 |
|
||||
138 | if '%' in expr: |
|
|||
139 | yield self._format(expr, get, map) |
|
|||
140 | elif '|' in expr: |
|
|||
141 | yield self._filter(expr, get, map) |
|
|||
142 | else: |
|
|||
143 | yield get(expr) |
|
|||
144 |
|
164 | |||
145 | engines = {'default': engine} |
|
165 | engines = {'default': engine} | |
146 |
|
166 |
General Comments 0
You need to be logged in to leave comments.
Login now