# HG changeset patch
# User cmlenz
# Date 1195763760 0
# Node ID 54964f7d225330572d35c8be17a0b22df7dff53b
# Parent f3bc1de7138d05a56cf5c36044029e29bafc18a9
Allow use of py:replace as element. Closes #144.
diff --git a/ChangeLog b/ChangeLog
--- a/ChangeLog
+++ b/ChangeLog
@@ -38,6 +38,8 @@
`fragment` parameter is `True`.
* The `striptags` function now also removes HTML/XML-style comments (ticket
#150).
+ * The `py:replace` directive can now also be used as an element, with an
+ attribute named `value` (ticket #144).
Version 0.4.4
diff --git a/doc/xml-templates.txt b/doc/xml-templates.txt
--- a/doc/xml-templates.txt
+++ b/doc/xml-templates.txt
@@ -499,7 +499,14 @@
Bye
-This directive can only be used as an attribute.
+This directive can also be used as an element (since version 0.5):
+
+.. code-block:: genshi
+
+
+
.. _`py:strip`:
diff --git a/genshi/template/directives.py b/genshi/template/directives.py
--- a/genshi/template/directives.py
+++ b/genshi/template/directives.py
@@ -207,6 +207,10 @@
__slots__ = []
def attach(cls, template, stream, value, namespaces, pos):
+ if type(value) is dict:
+ raise TemplateSyntaxError('The content directive can not be used '
+ 'as an element', template.filepath,
+ *pos[1:])
expr = cls._parse_expr(value, template, *pos[1:])
return None, [stream[0], (EXPR, expr, pos), stream[-1]]
attach = classmethod(attach)
@@ -484,6 +488,8 @@
__slots__ = []
def attach(cls, template, stream, value, namespaces, pos):
+ if type(value) is dict:
+ value = value.get('value')
if not value:
raise TemplateSyntaxError('missing value for "replace" directive',
template.filepath, *pos[1:])
diff --git a/genshi/template/tests/directives.py b/genshi/template/tests/directives.py
--- a/genshi/template/tests/directives.py
+++ b/genshi/template/tests/directives.py
@@ -916,6 +916,21 @@
# """, str(tmpl.generate()))
+class ContentDirectiveTestCase(unittest.TestCase):
+ """Tests for the `py:content` template directive."""
+
+ def test_as_element(self):
+ try:
+ tmpl = MarkupTemplate("""
+ Foo
+ """, filename='test.html')
+ self.fail('Expected TemplateSyntaxError')
+ except TemplateSyntaxError, e:
+ self.assertEqual('test.html', e.filename)
+ if sys.version_info[:2] >= (2, 4):
+ self.assertEqual(2, e.lineno)
+
+
class ReplaceDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:replace` template directive."""
@@ -934,6 +949,14 @@
if sys.version_info[:2] >= (2, 4):
self.assertEqual(2, e.lineno)
+ def test_as_element(self):
+ tmpl = MarkupTemplate("""""", filename='test.html')
+ self.assertEqual("""
+ Test
+
""", str(tmpl.generate(title='Test')))
+
class StripDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:strip` template directive."""
@@ -1065,6 +1088,7 @@
suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
+ suite.addTest(unittest.makeSuite(ContentDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(ReplaceDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(WithDirectiveTestCase, 'test'))