Mercurial > genshi > mirror
comparison markup/template.py @ 35:35b9e9318fb1 trunk
Simplify template processing model by removing dynamically generated `SUB` events.
author | cmlenz |
---|---|
date | Sun, 02 Jul 2006 21:23:52 +0000 |
parents | 2ab5fa60575d |
children | ed370ebfa794 |
comparison
equal
deleted
inserted
replaced
34:3421dd98f015 | 35:35b9e9318fb1 |
---|---|
718 if ctxt is None: | 718 if ctxt is None: |
719 ctxt = Context() | 719 ctxt = Context() |
720 if not hasattr(ctxt, '_match_templates'): | 720 if not hasattr(ctxt, '_match_templates'): |
721 ctxt._match_templates = [] | 721 ctxt._match_templates = [] |
722 | 722 |
723 stream = self._match(self._eval(self.stream, ctxt), ctxt) | 723 stream = self._flatten(self._match(self._eval(self.stream, ctxt), ctxt), |
724 return Stream(self._flatten(stream, ctxt)) | 724 ctxt) |
725 for filter_ in self.filters: | |
726 stream = filter_(iter(stream), ctxt) | |
727 return Stream(stream) | |
725 | 728 |
726 def _eval(self, stream, ctxt=None): | 729 def _eval(self, stream, ctxt=None): |
727 """Internal stream filter that evaluates any expressions in `START` and | 730 """Internal stream filter that evaluates any expressions in `START` and |
728 `TEXT` events. | 731 `TEXT` events. |
729 """ | 732 """ |
762 yield Stream.TEXT, unicode(result), pos | 765 yield Stream.TEXT, unicode(result), pos |
763 else: | 766 else: |
764 # Test if the expression evaluated to an iterable, in which | 767 # Test if the expression evaluated to an iterable, in which |
765 # case we yield the individual items | 768 # case we yield the individual items |
766 try: | 769 try: |
767 yield (Template.SUB, ([], iter(result)), pos) | 770 for event in self._match(self._eval(iter(result), ctxt), |
771 ctxt): | |
772 yield event | |
768 except TypeError: | 773 except TypeError: |
769 # Neither a string nor an iterable, so just pass it | 774 # Neither a string nor an iterable, so just pass it |
770 # through | 775 # through |
771 yield Stream.TEXT, unicode(result), pos | 776 yield Stream.TEXT, unicode(result), pos |
772 | 777 |
773 else: | 778 else: |
774 yield kind, data, pos | 779 yield kind, data, pos |
775 | 780 |
776 def _flatten(self, stream, ctxt=None): | 781 def _flatten(self, stream, ctxt=None): |
777 """Internal stream filter that expands `SUB` events in the stream.""" | 782 """Internal stream filter that expands `SUB` events in the stream.""" |
778 for filter_ in self.filters: | |
779 stream = filter_(iter(stream), ctxt) | |
780 try: | 783 try: |
781 for kind, data, pos in stream: | 784 for kind, data, pos in stream: |
782 if kind is Template.SUB: | 785 if kind is Template.SUB: |
783 # This event is a list of directives and a list of | 786 # This event is a list of directives and a list of nested |
784 # nested events to which those directives should be | 787 # events to which those directives should be applied |
785 # applied | |
786 directives, substream = data | 788 directives, substream = data |
787 for directive in directives: | 789 for directive in directives: |
788 substream = directive(iter(substream), ctxt) | 790 substream = directive(iter(substream), ctxt) |
789 substream = self._match(self._eval(substream, ctxt), ctxt) | 791 substream = self._match(self._eval(substream, ctxt), ctxt) |
790 for event in self._flatten(substream, ctxt): | 792 for event in self._flatten(substream, ctxt): |
832 # enable the path to keep track of the stream state | 834 # enable the path to keep track of the stream state |
833 test(*event) | 835 test(*event) |
834 | 836 |
835 content = list(self._flatten(content, ctxt)) | 837 content = list(self._flatten(content, ctxt)) |
836 | 838 |
837 def _apply(stream, ctxt): | 839 ctxt.push(select=lambda path: Stream(content).select(path)) |
838 ctxt.push(select=lambda path: Stream(stream).select(path)) | 840 for event in self._match(self._eval(iter(template), ctxt), |
839 for event in template: | 841 ctxt): |
840 yield event | 842 yield event |
841 ctxt.pop() | 843 ctxt.pop() |
842 | 844 |
843 yield (Template.SUB, | |
844 ([lambda stream, ctxt: _apply(content, ctxt)], | |
845 []), content[0][-1]) | |
846 break | 845 break |
847 else: | 846 else: |
848 yield kind, data, pos | 847 yield kind, data, pos |
849 | 848 |
850 | 849 |