changeset 92:01d36818bb3d trunk

More performance improvements... this time for whitespace normalization and template loops.
author cmlenz
date Thu, 20 Jul 2006 23:06:36 +0000
parents a71a58df6bf5
children 08d77c7725e2
files markup/filters.py markup/template.py
diffstat 2 files changed, 22 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/markup/filters.py
+++ b/markup/filters.py
@@ -13,13 +13,14 @@
 
 """Implementation of a number of stream filters."""
 
+from itertools import chain
 try:
     frozenset
 except NameError:
     from sets import ImmutableSet as frozenset
 import re
 
-from markup.core import Attributes, Markup, Namespace
+from markup.core import Attributes, Markup, Namespace, escape
 from markup.core import END, END_NS, START, START_NS, TEXT
 from markup.path import Path
 
@@ -117,23 +118,23 @@
         mjoin = Markup('').join
 
         textbuf = []
-        for kind, data, pos in stream:
+        for kind, data, pos in chain(stream, [(None, None, None)]):
             if kind is TEXT:
                 textbuf.append(data)
             else:
                 if textbuf:
-                    text = mjoin(textbuf, escape_quotes=False)
-                    text = trim_trailing_space('', text)
-                    text = collapse_lines('\n', text)
-                    yield TEXT, Markup(text), pos
-                    del textbuf[:]
+                    if len(textbuf) > 1:
+                        output = Markup(collapse_lines('\n',
+                            trim_trailing_space('',
+                                mjoin(textbuf, escape_quotes=False))))
+                        del textbuf[:]
+                        yield TEXT, output, pos
+                    else:
+                        output = escape(collapse_lines('\n',
+                            trim_trailing_space('',
+                                textbuf.pop())), quotes=False)
+                        yield TEXT, output, pos
                 yield kind, data, pos
-        else:
-            if textbuf:
-                text = mjoin(textbuf, escape_quotes=False)
-                text = trim_trailing_space('', text)
-                text = collapse_lines('\n', text)
-                yield TEXT, Markup(text), pos
 
 
 class HTMLSanitizer(object):
--- a/markup/template.py
+++ b/markup/template.py
@@ -367,12 +367,14 @@
         iterable = self.expr.evaluate(ctxt)
         if iterable is not None:
             stream = list(stream)
+            scope = {}
+            targets = self.targets
             for item in iter(iterable):
-                if len(self.targets) == 1:
-                    item = [item]
-                scope = {}
-                for idx, name in enumerate(self.targets):
-                    scope[name] = item[idx]
+                if len(targets) == 1:
+                    scope[targets[0]] = item
+                else:
+                    for idx, name in enumerate(targets):
+                        scope[name] = item[idx]
                 ctxt.push(**scope)
                 for event in _apply_directives(stream, ctxt, directives):
                     yield event
@@ -881,7 +883,6 @@
                     substream = filter_(substream, ctxt)
                 for event in substream:
                     yield event
-                    continue
             else:
                 yield kind, data, pos
 
@@ -896,7 +897,7 @@
 
             # We (currently) only care about start and end events for matching
             # We might care about namespace events in the future, though
-            if kind not in (START, END):
+            if not match_templates or kind not in (START, END):
                 yield kind, data, pos
                 continue
 
Copyright (C) 2012-2017 Edgewall Software