# HG changeset patch # User cmlenz # Date 1155943425 0 # Node ID ff4f0d89eef7d98ca3b0f854b7b1795f77e9132c # Parent 7fcf8e04514edddf6d66a2984306b66bb17444db Fix for #30 (trouble using `py:def`inside a match template) diff --git a/markup/core.py b/markup/core.py --- a/markup/core.py +++ b/markup/core.py @@ -38,8 +38,9 @@ (kind, data, position) where `kind` is the event kind (such as `START`, `END`, `TEXT`, etc), `data` - depends on the kind of event, and `position` is a `(line, offset)` tuple - that contains the location of the original element or text in the input. + depends on the kind of event, and `position` is a `(filename, line, offset)` + tuple that contains the location of the original element or text in the + input. If the original location is unknown, `position` is `(None, -1, -1)`. """ __slots__ = ['events'] diff --git a/markup/template.py b/markup/template.py --- a/markup/template.py +++ b/markup/template.py @@ -153,7 +153,7 @@ self.expr = value and Expression(value, filename, lineno) or None except SyntaxError, err: err.msg += ' in expression "%s" of "%s" directive' % (value, - self.name) + self.tagname) raise TemplateSyntaxError(err, filename, lineno, offset + (err.offset or 0)) @@ -166,10 +166,12 @@ expr = ' "%s"' % self.expr.source return '<%s%s>' % (self.__class__.__name__, expr) - def name(self): - """Return the local name of the directive as it is used in templates.""" + def tagname(self): + """Return the local tag name of the directive as it is used in + templates. + """ return self.__class__.__name__.lower().replace('directive', '') - name = property(name) + tagname = property(tagname) def _apply_directives(stream, ctxt, directives): @@ -351,6 +353,9 @@ return [] + def __repr__(self): + return '<%s "%s">' % (self.__class__.__name__, self.name) + class ForDirective(Directive): """Implementation of the `py:for` template directive for repeating an @@ -707,11 +712,11 @@ else: self.source = source self.basedir = basedir - self.filename = filename or '' + self.filename = filename if basedir and filename: self.filepath = os.path.join(basedir, filename) else: - self.filepath = '' + self.filepath = None self.filters = [] self.parse() @@ -883,7 +888,7 @@ """Internal stream filter that evaluates any expressions in `START` and `TEXT` events. """ - filters = (self._eval, self._match) + filters = (self._eval, self._match, self._flatten) for kind, data, pos in stream: diff --git a/markup/tests/template.py b/markup/tests/template.py --- a/markup/tests/template.py +++ b/markup/tests/template.py @@ -446,6 +446,16 @@ """, str(tmpl.generate())) + def test_def_in_match(self): + tmpl = Template(""" + + ${select('*')} + ${maketitle(True)} + """) + self.assertEqual(""" + True + """, str(tmpl.generate())) + class StripDirectiveTestCase(unittest.TestCase): """Tests for the `py:strip` template directive."""