Mercurial > genshi > mirror
comparison genshi/template.py @ 261:da3137c427a2 stable-0.3.x
Ported [321:323] to 0.3.x stable branch.
author | cmlenz |
---|---|
date | Fri, 22 Sep 2006 12:07:23 +0000 |
parents | 82f2bc77ad0c |
children | eb0a369ebef6 186d18554183 |
comparison
equal
deleted
inserted
replaced
257:5c8125e03b72 | 261:da3137c427a2 |
---|---|
11 # individuals. For the exact contribution history, see the revision | 11 # individuals. For the exact contribution history, see the revision |
12 # history and logs, available at http://genshi.edgewall.org/log/. | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
13 | 13 |
14 """Implementation of the template engine.""" | 14 """Implementation of the template engine.""" |
15 | 15 |
16 from itertools import chain | |
16 try: | 17 try: |
17 from collections import deque | 18 from collections import deque |
18 except ImportError: | 19 except ImportError: |
19 class deque(list): | 20 class deque(list): |
20 def appendleft(self, x): self.insert(0, x) | 21 def appendleft(self, x): self.insert(0, x) |
847 stream = self.stream | 848 stream = self.stream |
848 for filter_ in self.filters: | 849 for filter_ in self.filters: |
849 stream = filter_(iter(stream), ctxt) | 850 stream = filter_(iter(stream), ctxt) |
850 return Stream(stream) | 851 return Stream(stream) |
851 | 852 |
852 def _eval(self, stream, ctxt=None): | 853 def _eval(self, stream, ctxt): |
853 """Internal stream filter that evaluates any expressions in `START` and | 854 """Internal stream filter that evaluates any expressions in `START` and |
854 `TEXT` events. | 855 `TEXT` events. |
855 """ | 856 """ |
856 filters = (self._flatten, self._eval) | 857 filters = (self._flatten, self._eval) |
857 | 858 |
903 yield event | 904 yield event |
904 | 905 |
905 else: | 906 else: |
906 yield kind, data, pos | 907 yield kind, data, pos |
907 | 908 |
908 def _flatten(self, stream, ctxt=None): | 909 def _flatten(self, stream, ctxt): |
909 """Internal stream filter that expands `SUB` events in the stream.""" | 910 """Internal stream filter that expands `SUB` events in the stream.""" |
910 for kind, data, pos in stream: | 911 for kind, data, pos in stream: |
911 if kind is SUB: | 912 if kind is SUB: |
912 # This event is a list of directives and a list of nested | 913 # This event is a list of directives and a list of nested |
913 # events to which those directives should be applied | 914 # events to which those directives should be applied |
1047 else: | 1048 else: |
1048 stream.append((kind, data, pos)) | 1049 stream.append((kind, data, pos)) |
1049 | 1050 |
1050 return stream | 1051 return stream |
1051 | 1052 |
1052 def _match(self, stream, ctxt=None, match_templates=None): | 1053 def _match(self, stream, ctxt, match_templates=None): |
1053 """Internal stream filter that applies any defined match templates | 1054 """Internal stream filter that applies any defined match templates |
1054 to the stream. | 1055 to the stream. |
1055 """ | 1056 """ |
1056 if match_templates is None: | 1057 if match_templates is None: |
1057 match_templates = ctxt._match_templates | 1058 match_templates = ctxt._match_templates |
1090 for test in [mt[0] for mt in match_templates[idx + 1:]]: | 1091 for test in [mt[0] for mt in match_templates[idx + 1:]]: |
1091 test(kind, data, pos, nsprefix, ctxt) | 1092 test(kind, data, pos, nsprefix, ctxt) |
1092 | 1093 |
1093 # Consume and store all events until an end event | 1094 # Consume and store all events until an end event |
1094 # corresponding to this start event is encountered | 1095 # corresponding to this start event is encountered |
1095 content = [(kind, data, pos)] | 1096 content = chain([(kind, data, pos)], |
1096 content += list(self._match(_strip(stream), ctxt)) + tail | 1097 self._match(_strip(stream), ctxt), |
1098 tail) | |
1099 for filter_ in self.filters[3:]: | |
1100 content = filter_(content, ctxt) | |
1101 content = list(content) | |
1097 | 1102 |
1098 kind, data, pos = tail[0] | 1103 kind, data, pos = tail[0] |
1099 for test in [mt[0] for mt in match_templates]: | 1104 for test in [mt[0] for mt in match_templates]: |
1100 test(kind, data, pos, nsprefix, ctxt) | 1105 test(kind, data, pos, nsprefix, ctxt) |
1101 | 1106 |