changeset 23:d88358f719fa trunk

Separate match and eval filters from the include and user-supplied filters.
author cmlenz
date Tue, 20 Jun 2006 17:31:32 +0000
parents 2483fe549959
children 5a4d45703319
files examples/includes/run.py markup/path.py markup/template.py
diffstat 3 files changed, 11 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/examples/includes/run.py
+++ b/examples/includes/run.py
@@ -1,7 +1,6 @@
 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 
-from datetime import datetime, timedelta
 import os
 import sys
 import timing
--- a/markup/path.py
+++ b/markup/path.py
@@ -176,8 +176,6 @@
         stack = [0] # stack of cursors into the location path
 
         def _test(kind, data, pos):
-            #print '\nTracker %r test [%s] %r' % (self, kind, data)
-
             if not stack:
                 return False
 
@@ -191,7 +189,6 @@
             matched = False
             closure, node_test, predicates = self.steps[stack[-1]]
 
-            #print '  Testing against %r' % node_test
             matched = node_test(kind, data, pos)
             if matched and predicates:
                 for predicate in predicates:
@@ -201,10 +198,8 @@
 
             if matched:
                 if stack[-1] == len(self.steps) - 1:
-                    #print '  Last step %r... returned %r' % (node_test, matched)
                     return matched
 
-                #print '  Matched intermediate step %r... proceed to next step %r' % (node_test, self.steps[stack[-1] + 1])
                 stack[-1] += 1
 
             elif kind is Stream.START and not closure:
--- a/markup/template.py
+++ b/markup/template.py
@@ -587,7 +587,7 @@
         else:
             self.filepath = '<string>'
 
-        self.filters = [self._eval, self._match]
+        self.filters = []
         self.parse()
 
     def __repr__(self):
@@ -692,7 +692,7 @@
                             yield result
                     else:
                         yield Stream.TEXT, group.replace('$$', '$'), \
-                              (lineno, offset)
+                              (filename, lineno, offset)
         return _interpolate(text)
     _interpolate = classmethod(_interpolate)
 
@@ -703,7 +703,8 @@
         if not hasattr(ctxt, '_match_templates'):
             ctxt._match_templates = []
 
-        return Stream(self._flatten(self.stream, ctxt))
+        stream = self._match(self._eval(self.stream, ctxt), ctxt)
+        return Stream(self._flatten(stream, ctxt))
 
     def _eval(self, stream, ctxt=None):
         for kind, data, pos in stream:
@@ -726,7 +727,7 @@
                         value = filter(lambda x: x is not None, values)
                         if not value:
                             continue
-                    new_attrib.append((name, ''.join(value)))
+                    new_attrib.append((name, u''.join(value)))
                 yield kind, (tag, Attributes(new_attrib)), pos
 
             elif kind is Template.EXPR:
@@ -738,7 +739,7 @@
                 # succeeds, and the string will be chopped up into individual
                 # characters
                 if isinstance(result, basestring):
-                    yield Stream.TEXT, result, pos
+                    yield Stream.TEXT, unicode(result), pos
                 else:
                     # Test if the expression evaluated to an iterable, in which
                     # case we yield the individual items
@@ -752,10 +753,9 @@
             else:
                 yield kind, data, pos
 
-    def _flatten(self, stream, ctxt=None, apply_filters=True):
-        if apply_filters:
-            for filter_ in self.filters:
-                stream = filter_(iter(stream), ctxt)
+    def _flatten(self, stream, ctxt=None):
+        for filter_ in self.filters:
+            stream = filter_(iter(stream), ctxt)
         try:
             for kind, data, pos in stream:
                 if kind is Template.SUB:
@@ -766,6 +766,7 @@
                     directives.reverse()
                     for directive in directives:
                         substream = directive(iter(substream), ctxt)
+                    substream = self._match(self._eval(substream, ctxt), ctxt)
                     for event in self._flatten(substream, ctxt):
                         yield event
                         continue
@@ -808,10 +809,9 @@
                         # enable the path to keep track of the stream state
                         test(*event)
 
-                    content = list(self._flatten(content, ctxt, False))
+                    content = list(self._flatten(content, ctxt))
 
                     def _apply(stream, ctxt):
-                        stream = list(stream)
                         ctxt.push(select=lambda path: Stream(stream).select(path))
                         for event in template:
                             yield event
Copyright (C) 2012-2017 Edgewall Software