Mercurial > genshi > mirror
comparison genshi/template.py @ 248:82f2bc77ad0c trunk
* Add `MANIFEST.in` so that the generated HTML documentation is included in the source distribution.
* Remove duplicate `_match` implementation.
* Allow shorthand expressions to start with an underscore.
author | cmlenz |
---|---|
date | Sun, 17 Sep 2006 10:16:15 +0000 |
parents | 71062601458a |
children | da3a27589559 |
comparison
equal
deleted
inserted
replaced
247:cf4a125b44a1 | 248:82f2bc77ad0c |
---|---|
781 data) and directives or expressions. | 781 data) and directives or expressions. |
782 """ | 782 """ |
783 raise NotImplementedError | 783 raise NotImplementedError |
784 | 784 |
785 _FULL_EXPR_RE = re.compile(r'(?<!\$)\$\{(.+?)\}', re.DOTALL) | 785 _FULL_EXPR_RE = re.compile(r'(?<!\$)\$\{(.+?)\}', re.DOTALL) |
786 _SHORT_EXPR_RE = re.compile(r'(?<!\$)\$([a-zA-Z][a-zA-Z0-9_\.]*)') | 786 _SHORT_EXPR_RE = re.compile(r'(?<!\$)\$([a-zA-Z_][a-zA-Z0-9_\.]*)') |
787 | 787 |
788 def _interpolate(cls, text, filename=None, lineno=-1, offset=-1): | 788 def _interpolate(cls, text, filename=None, lineno=-1, offset=-1): |
789 """Parse the given string and extract expressions. | 789 """Parse the given string and extract expressions. |
790 | 790 |
791 This method returns a list containing both literal text and `Expression` | 791 This method returns a list containing both literal text and `Expression` |
916 for event in self._flatten(substream, ctxt): | 916 for event in self._flatten(substream, ctxt): |
917 yield event | 917 yield event |
918 else: | 918 else: |
919 yield kind, data, pos | 919 yield kind, data, pos |
920 | 920 |
921 def _match(self, stream, ctxt=None, match_templates=None): | |
922 """Internal stream filter that applies any defined match templates | |
923 to the stream. | |
924 """ | |
925 if match_templates is None: | |
926 match_templates = ctxt._match_templates | |
927 nsprefix = {} # mapping of namespace prefixes to URIs | |
928 | |
929 tail = [] | |
930 def _strip(stream): | |
931 depth = 1 | |
932 while 1: | |
933 kind, data, pos = stream.next() | |
934 if kind is START: | |
935 depth += 1 | |
936 elif kind is END: | |
937 depth -= 1 | |
938 if depth > 0: | |
939 yield kind, data, pos | |
940 else: | |
941 tail[:] = [(kind, data, pos)] | |
942 break | |
943 | |
944 for kind, data, pos in stream: | |
945 | |
946 # We (currently) only care about start and end events for matching | |
947 # We might care about namespace events in the future, though | |
948 if not match_templates or kind not in (START, END): | |
949 yield kind, data, pos | |
950 continue | |
951 | |
952 for idx, (test, path, template, directives) in \ | |
953 enumerate(match_templates): | |
954 | |
955 if test(kind, data, pos, nsprefix, ctxt) is True: | |
956 | |
957 # Let the remaining match templates know about the event so | |
958 # they get a chance to update their internal state | |
959 for test in [mt[0] for mt in match_templates[idx + 1:]]: | |
960 test(kind, data, pos, nsprefix, ctxt) | |
961 | |
962 # Consume and store all events until an end event | |
963 # corresponding to this start event is encountered | |
964 content = [(kind, data, pos)] | |
965 content += list(self._match(_strip(stream), ctxt)) + tail | |
966 | |
967 kind, data, pos = tail[0] | |
968 for test in [mt[0] for mt in match_templates]: | |
969 test(kind, data, pos, nsprefix, ctxt) | |
970 | |
971 # Make the select() function available in the body of the | |
972 # match template | |
973 def select(path): | |
974 return Stream(content).select(path) | |
975 ctxt.push(dict(select=select)) | |
976 | |
977 # Recursively process the output | |
978 template = _apply_directives(template, ctxt, directives) | |
979 for event in self._match(self._eval(self._flatten(template, | |
980 ctxt), | |
981 ctxt), ctxt, | |
982 match_templates[:idx] + | |
983 match_templates[idx + 1:]): | |
984 yield event | |
985 | |
986 ctxt.pop() | |
987 break | |
988 | |
989 else: # no matches | |
990 yield kind, data, pos | |
991 | |
992 | 921 |
993 EXPR = Template.EXPR | 922 EXPR = Template.EXPR |
994 SUB = Template.SUB | 923 SUB = Template.SUB |
995 | 924 |
996 | 925 |