Mercurial > genshi > mirror
comparison genshi/template/markup.py @ 591:bb8141898891 stable-0.4.x
Ported [704] to 0.4.x branch.
author | cmlenz |
---|---|
date | Fri, 10 Aug 2007 09:52:13 +0000 |
parents | dc8646e2e918 |
children |
comparison
equal
deleted
inserted
replaced
589:3cb55344d62f | 591:bb8141898891 |
---|---|
81 def _parse(self, source, encoding): | 81 def _parse(self, source, encoding): |
82 streams = [[]] # stacked lists of events of the "compiled" template | 82 streams = [[]] # stacked lists of events of the "compiled" template |
83 dirmap = {} # temporary mapping of directives to elements | 83 dirmap = {} # temporary mapping of directives to elements |
84 ns_prefix = {} | 84 ns_prefix = {} |
85 depth = 0 | 85 depth = 0 |
86 in_fallback = 0 | 86 fallbacks = [] |
87 include_href = None | 87 includes = [] |
88 | 88 |
89 if not isinstance(source, Stream): | 89 if not isinstance(source, Stream): |
90 source = XMLParser(source, filename=self.filename, | 90 source = XMLParser(source, filename=self.filename, |
91 encoding=encoding) | 91 encoding=encoding) |
92 | 92 |
152 include_href = new_attrs.get('href') | 152 include_href = new_attrs.get('href') |
153 if not include_href: | 153 if not include_href: |
154 raise TemplateSyntaxError('Include misses required ' | 154 raise TemplateSyntaxError('Include misses required ' |
155 'attribute "href"', | 155 'attribute "href"', |
156 self.filepath, *pos[1:]) | 156 self.filepath, *pos[1:]) |
157 includes.append(include_href) | |
157 streams.append([]) | 158 streams.append([]) |
158 elif tag.localname == 'fallback': | 159 elif tag.localname == 'fallback': |
159 in_fallback += 1 | 160 streams.append([]) |
161 fallbacks.append(streams[-1]) | |
160 | 162 |
161 else: | 163 else: |
162 stream.append((kind, (tag, new_attrs), pos)) | 164 stream.append((kind, (tag, new_attrs), pos)) |
163 | 165 |
164 depth += 1 | 166 depth += 1 |
165 | 167 |
166 elif kind is END: | 168 elif kind is END: |
167 depth -= 1 | 169 depth -= 1 |
168 | 170 |
169 if in_fallback and data == self.XINCLUDE_NAMESPACE['fallback']: | 171 if fallbacks and data == self.XINCLUDE_NAMESPACE['fallback']: |
170 in_fallback -= 1 | 172 assert streams.pop() is fallbacks[-1] |
171 elif data == self.XINCLUDE_NAMESPACE['include']: | 173 elif data == self.XINCLUDE_NAMESPACE['include']: |
172 fallback = streams.pop() | 174 fallback = None |
175 if len(fallbacks) == len(includes): | |
176 fallback = fallbacks.pop() | |
177 streams.pop() # discard anything between the include tags | |
178 # and the fallback element | |
173 stream = streams[-1] | 179 stream = streams[-1] |
174 stream.append((INCLUDE, (include_href, fallback), pos)) | 180 stream.append((INCLUDE, (includes.pop(), fallback), pos)) |
175 else: | 181 else: |
176 stream.append((kind, data, pos)) | 182 stream.append((kind, data, pos)) |
177 | 183 |
178 # If there have have directive attributes with the corresponding | 184 # If there have have directive attributes with the corresponding |
179 # start tag, move the events inbetween into a "subprogram" | 185 # start tag, move the events inbetween into a "subprogram" |
223 assert len(streams) == 1 | 229 assert len(streams) == 1 |
224 return streams[0] | 230 return streams[0] |
225 | 231 |
226 def _prepare(self, stream): | 232 def _prepare(self, stream): |
227 for kind, data, pos in Template._prepare(self, stream): | 233 for kind, data, pos in Template._prepare(self, stream): |
228 if kind is INCLUDE: | 234 if kind is INCLUDE and data[1]: |
229 data = data[0], list(self._prepare(data[1])) | 235 data = data[0], list(self._prepare(data[1])) |
230 yield kind, data, pos | 236 yield kind, data, pos |
231 | 237 |
232 def _exec(self, stream, ctxt): | 238 def _exec(self, stream, ctxt): |
233 """Internal stream filter that executes code in ``<?python ?>`` | 239 """Internal stream filter that executes code in ``<?python ?>`` |