# HG changeset patch # User cmlenz # Date 1163182843 0 # Node ID 07c5978ad95f43fe9ab2c68d8a0caf8c08b58dec # Parent 72937f87d5192cdd3f0fb8217621b2edb854047d inline branch: adapted to the immutble `Attrs` change, and various other fixes. diff --git a/genshi/template/core.py b/genshi/template/core.py --- a/genshi/template/core.py +++ b/genshi/template/core.py @@ -178,6 +178,11 @@ expr = ' "%s"' % self.expr.source return '<%s%s>' % (self.__class__.__name__, expr) + def optimize(self, stream, directives): + if not directives: + return stream + return directives[0].optimize(stream, directives[1:]) + def tagname(self): """Return the local tag name of the directive as it is used in templates. @@ -231,7 +236,7 @@ self.filters = [self._flatten, self._eval] - self.stream = self._parse(encoding) + self.stream = self._optimize(self._parse(encoding)) def __repr__(self): return '<%s "%s">' % (self.__class__.__name__, self.filename) @@ -289,6 +294,14 @@ return _interpolate(text, [cls._FULL_EXPR_RE, cls._SHORT_EXPR_RE]) _interpolate = classmethod(_interpolate) + def _optimize(self, stream): + for kind, data, pos in stream: + if kind is SUB: + directives, substream = data + directives[0].optimize(substream, directives[1:]) + else: + yield kind, data pos + def compile(self): """Compile the template to a Python module, and return the module object.