changeset 172:ff4f0d89eef7 trunk

Fix for #30 (trouble using `py:def`inside a match template)
author cmlenz
date Fri, 18 Aug 2006 23:23:45 +0000
parents 7fcf8e04514e
children 128005041637
files markup/core.py markup/template.py markup/tests/template.py
diffstat 3 files changed, 25 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- 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']
 
--- 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 '<string>'
+        self.filename = filename
         if basedir and filename:
             self.filepath = os.path.join(basedir, filename)
         else:
-            self.filepath = '<string>'
+            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:
 
--- a/markup/tests/template.py
+++ b/markup/tests/template.py
@@ -446,6 +446,16 @@
           </div>
         </doc>""", str(tmpl.generate()))
 
+    def test_def_in_match(self):
+        tmpl = Template("""<doc xmlns:py="http://markup.edgewall.org/">
+          <py:def function="maketitle(test)"><b py:replace="test" /></py:def>
+          <head py:match="head">${select('*')}</head>
+          <head><title>${maketitle(True)}</title></head>
+        </doc>""")
+        self.assertEqual("""<doc>
+          <head><title>True</title></head>
+        </doc>""", str(tmpl.generate()))
+
 
 class StripDirectiveTestCase(unittest.TestCase):
     """Tests for the `py:strip` template directive."""
Copyright (C) 2012-2017 Edgewall Software