Ticket #321: pycomment1.patch
File pycomment1.patch, 4.9 KB (added by daniel-genshi@…, 14 years ago) |
---|
-
genshi/template/tests/directives.py
59 59 </doc>""", tmpl.generate().render(encoding=None)) 60 60 61 61 62 class CommentDirectiveTestCase(unittest.TestCase): 63 """Tests for the `py:comment` template directive.""" 64 65 def test_comment_processes(self): 66 tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/"> 67 <py:comment>${1 + 1}</py:comment> 68 </div>""") 69 self.assertEqual("""<div> 70 <!--2--> 71 </div>""", tmpl.generate().render(encoding=None)) 72 73 def test_comment_empty(self): 74 tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/"> 75 <py:comment /> 76 </div>""") 77 self.assertEqual("""<div> 78 <!----> 79 </div>""", tmpl.generate().render(encoding=None)) 80 81 def test_comment_not_attribute(self): 82 tmpl = MarkupTemplate("""<div xmlns:py="http://genshi.edgewall.org/"> 83 <p py:comment=""></p> 84 </div>""") 85 self.assertRaises(TemplateSyntaxError, tmpl.generate) 86 87 62 88 class ChooseDirectiveTestCase(unittest.TestCase): 63 89 """Tests for the `py:choose` template directive and the complementary 64 90 directives `py:when` and `py:otherwise`.""" … … 1138 1164 suite.addTest(doctest.DocTestSuite(directives)) 1139 1165 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test')) 1140 1166 suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test')) 1167 suite.addTest(unittest.makeSuite(CommentDirectiveTestCase, 'test')) 1141 1168 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test')) 1142 1169 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test')) 1143 1170 suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test')) -
genshi/template/markup.py
56 56 ('replace', ReplaceDirective), 57 57 ('content', ContentDirective), 58 58 ('attrs', AttrsDirective), 59 ('comment', CommentDirective), 59 60 ('strip', StripDirective)] 60 61 serializer = 'xml' 61 62 _number_conv = Markup -
genshi/template/directives.py
13 13 14 14 """Implementation of the various template directives.""" 15 15 16 from genshi.core import QName, Stream 16 from genshi.core import QName, Stream, TEXT, Markup 17 17 from genshi.path import Path 18 18 from genshi.template.base import TemplateRuntimeError, TemplateSyntaxError, \ 19 19 EXPR, _apply_directives, _eval_expr … … 21 21 _ast, _parse 22 22 23 23 __all__ = ['AttrsDirective', 'ChooseDirective', 'ContentDirective', 24 'DefDirective', 'ForDirective', 'IfDirective', 'MatchDirective', 24 'CommentDirective', 'DefDirective', 'ForDirective', 25 'IfDirective', 'MatchDirective', 25 26 'OtherwiseDirective', 'ReplaceDirective', 'StripDirective', 26 27 'WhenDirective', 'WithDirective'] 27 28 __docformat__ = 'restructuredtext en' … … 185 186 return _apply_directives(_generate(), directives, ctxt, vars) 186 187 187 188 189 class CommentDirective(Directive): 190 """Implementation of the ``py:comment`` template directive. 191 192 This directive replaces the content of the element with the result of 193 evaluating the value of the ``py:comment`` attribute: 194 195 >>> from genshi.template import MarkupTemplate 196 >>> tmpl = MarkupTemplate('''<div xmlns:py="http://genshi.edgewall.org/"> 197 ... <py:comment>${2 + 2}</py:comment> 198 ... </div>''') 199 >>> print tmpl.generate(bar='Bye') 200 <div> 201 <!--4--> 202 </div> 203 """ 204 __slots__ = [] 205 206 @classmethod 207 def attach(cls, template, stream, value, namespaces, pos): 208 if type(value) is not dict: 209 raise TemplateSyntaxError('The comment directive can only be used ' 210 'as an element', template.filepath, 211 *pos[1:]) 212 return super(CommentDirective, cls).attach(template, stream, 213 value, namespaces, pos) 214 215 def __call__(self, stream, directives, ctxt, **vars): 216 stream = list(stream) 217 218 stream.insert(0, (TEXT, Markup("<!--"), (None, 1, 0))) 219 stream.append((TEXT, Markup("-->"), (None, 1, 0))) 220 221 return _apply_directives(stream, directives, ctxt, vars) 222 223 188 224 class ContentDirective(Directive): 189 225 """Implementation of the ``py:content`` template directive. 190 226