# HG changeset patch # User cmlenz # Date 1157412458 0 # Node ID d8b195b22a443b1c1f6e95f5d6cf44bd63a0e2fd # Parent 0a01371cecbc421023fb61f574e77518f43c6ec6 Fix `py:match` directive which would screw up in some scenarios due to incorrect handling of the substream. Closes #49. diff --git a/markup/path.py b/markup/path.py --- a/markup/path.py +++ b/markup/path.py @@ -193,9 +193,8 @@ # Both the node test and the predicates matched if matched: if last_step: - if ignore_context or kind is not START \ - or axis is ATTRIBUTE or axis is SELF \ - or len(stack) > 2: + if not ctxtnode or kind is not START \ + or axis is ATTRIBUTE or axis is SELF: retval = matched elif not ctxtnode or axis is SELF \ or axis is DESCENDANT_OR_SELF: @@ -221,7 +220,8 @@ # We're done with this step if it's the last step or the # axis isn't "self" - if last_step or axis is not SELF: + if last_step or (axis is not SELF and + axis is not DESCENDANT_OR_SELF): break if kind is START and axis is not DESCENDANT \ diff --git a/markup/template.py b/markup/template.py --- a/markup/template.py +++ b/markup/template.py @@ -996,6 +996,21 @@ if match_templates is None: match_templates = ctxt._match_templates + tail = [] + def _strip(stream): + depth = 1 + while 1: + kind, data, pos = stream.next() + if kind is START: + depth += 1 + elif kind is END: + depth -= 1 + if depth > 0: + yield kind, data, pos + else: + tail[:] = [(kind, data, pos)] + break + for kind, data, pos in stream: # We (currently) only care about start and end events for matching @@ -1017,15 +1032,11 @@ # Consume and store all events until an end event # corresponding to this start event is encountered content = [(kind, data, pos)] - depth = 1 - stream = self._match(stream, ctxt) - while depth > 0: - kind, data, pos = stream.next() - if kind is START: - depth += 1 - elif kind is END: - depth -= 1 - content.append((kind, data, pos)) + content += list(self._match(_strip(stream), ctxt)) + tail + + kind, data, pos = tail[0] + for test in [mt[0] for mt in match_templates]: + test(kind, data, pos, ctxt) # Make the select() function available in the body of the # match template diff --git a/markup/tests/template.py b/markup/tests/template.py --- a/markup/tests/template.py +++ b/markup/tests/template.py @@ -601,6 +601,36 @@

Are you ready to mark up?


""", str(tmpl.generate())) + def test_multiple_matches(self): + tmpl = Template(""" + +

+ + +

+ """) + fields = ['hello_%s' % i for i in range(5)] + values = dict([('hello_%s' % i, i) for i in range(5)]) + self.assertEqual(""" +

+ + +

+ + +

+ + +

+ + +

+ + +

+ """, str(tmpl.generate(fields=fields, values=values))) + # FIXME #def test_match_after_step(self): # tmpl = Template("""