# HG changeset patch # User cmlenz # Date 1153436796 0 # Node ID 01d36818bb3daa93410a65d113d6fbc1911ed3be # Parent a71a58df6bf5a12f7f97478c3985c873fafe9814 More performance improvements... this time for whitespace normalization and template loops. diff --git a/markup/filters.py b/markup/filters.py --- 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): diff --git a/markup/template.py b/markup/template.py --- 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