Index: genshi/template/tests/directives.py
===================================================================
--- genshi/template/tests/directives.py (revision 1092)
+++ genshi/template/tests/directives.py (working copy)
@@ -59,6 +59,32 @@
""", tmpl.generate().render(encoding=None))
+class CommentDirectiveTestCase(unittest.TestCase):
+ """Tests for the `py:comment` template directive."""
+
+ def test_comment_processes(self):
+ tmpl = MarkupTemplate("""
""")
+ self.assertEqual("""
+
+
""", tmpl.generate().render(encoding=None))
+
+ def test_comment_empty(self):
+ tmpl = MarkupTemplate("""""")
+ self.assertEqual("""
+
+
""", tmpl.generate().render(encoding=None))
+
+ def test_comment_not_attribute(self):
+ tmpl = MarkupTemplate("""""")
+ self.assertRaises(TemplateSyntaxError, tmpl.generate)
+
+
class ChooseDirectiveTestCase(unittest.TestCase):
"""Tests for the `py:choose` template directive and the complementary
directives `py:when` and `py:otherwise`."""
@@ -1138,6 +1164,7 @@
suite.addTest(doctest.DocTestSuite(directives))
suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test'))
+ suite.addTest(unittest.makeSuite(CommentDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test'))
Index: genshi/template/markup.py
===================================================================
--- genshi/template/markup.py (revision 1092)
+++ genshi/template/markup.py (working copy)
@@ -56,6 +56,7 @@
('replace', ReplaceDirective),
('content', ContentDirective),
('attrs', AttrsDirective),
+ ('comment', CommentDirective),
('strip', StripDirective)]
serializer = 'xml'
_number_conv = Markup
Index: genshi/template/directives.py
===================================================================
--- genshi/template/directives.py (revision 1092)
+++ genshi/template/directives.py (working copy)
@@ -13,7 +13,7 @@
"""Implementation of the various template directives."""
-from genshi.core import QName, Stream
+from genshi.core import QName, Stream, TEXT, Markup
from genshi.path import Path
from genshi.template.base import TemplateRuntimeError, TemplateSyntaxError, \
EXPR, _apply_directives, _eval_expr
@@ -21,7 +21,8 @@
_ast, _parse
__all__ = ['AttrsDirective', 'ChooseDirective', 'ContentDirective',
- 'DefDirective', 'ForDirective', 'IfDirective', 'MatchDirective',
+ 'CommentDirective', 'DefDirective', 'ForDirective',
+ 'IfDirective', 'MatchDirective',
'OtherwiseDirective', 'ReplaceDirective', 'StripDirective',
'WhenDirective', 'WithDirective']
__docformat__ = 'restructuredtext en'
@@ -185,6 +186,41 @@
return _apply_directives(_generate(), directives, ctxt, vars)
+class CommentDirective(Directive):
+ """Implementation of the ``py:comment`` template directive.
+
+ This directive replaces the content of the element with the result of
+ evaluating the value of the ``py:comment`` attribute:
+
+ >>> from genshi.template import MarkupTemplate
+ >>> tmpl = MarkupTemplate('''''')
+ >>> print tmpl.generate(bar='Bye')
+
+
+
+ """
+ __slots__ = []
+
+ @classmethod
+ def attach(cls, template, stream, value, namespaces, pos):
+ if type(value) is not dict:
+ raise TemplateSyntaxError('The comment directive can only be used '
+ 'as an element', template.filepath,
+ *pos[1:])
+ return super(CommentDirective, cls).attach(template, stream,
+ value, namespaces, pos)
+
+ def __call__(self, stream, directives, ctxt, **vars):
+ stream = list(stream)
+
+ stream.insert(0, (TEXT, Markup(""), (None, 1, 0)))
+
+ return _apply_directives(stream, directives, ctxt, vars)
+
+
class ContentDirective(Directive):
"""Implementation of the ``py:content`` template directive.