Edgewall Software

Ticket #321: pycomments.patch

File pycomments.patch, 4.8 KB (added by daniel-genshi@…, 6 years ago)

patch for py:comment feature

  • genshi/template/tests/directives.py

     
    5959        </doc>""", str(tmpl.generate()))
    6060
    6161
     62class 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>""", str(tmpl.generate()))
     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>""", str(tmpl.generate()))
     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
    6288class ChooseDirectiveTestCase(unittest.TestCase):
    6389    """Tests for the `py:choose` template directive and the complementary
    6490    directives `py:when` and `py:otherwise`."""
     
    11341160    suite.addTest(doctest.DocTestSuite(directives))
    11351161    suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test'))
    11361162    suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test'))
     1163    suite.addTest(unittest.makeSuite(CommentDirectiveTestCase, 'test'))
    11371164    suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test'))
    11381165    suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test'))
    11391166    suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test'))
  • genshi/template/markup.py

     
    5656                  ('replace', ReplaceDirective),
    5757                  ('content', ContentDirective),
    5858                  ('attrs', AttrsDirective),
     59                  ('comment', CommentDirective),
    5960                  ('strip', StripDirective)]
    6061    serializer = 'xml'
    6162    _number_conv = Markup
  • genshi/template/directives.py

     
    1313
    1414"""Implementation of the various template directives."""
    1515
    16 from genshi.core import QName, Stream
     16from genshi.core import QName, Stream, TEXT, Markup
    1717from genshi.path import Path
    1818from genshi.template.base import TemplateRuntimeError, TemplateSyntaxError, \
    1919                                 EXPR, _apply_directives, _eval_expr
     
    2121                                 _ast, _parse
    2222
    2323__all__ = ['AttrsDirective', 'ChooseDirective', 'ContentDirective',
    24            'DefDirective', 'ForDirective', 'IfDirective', 'MatchDirective',
     24           'CommentDirective', 'DefDirective', 'ForDirective',
     25           'IfDirective', 'MatchDirective',
    2526           'OtherwiseDirective', 'ReplaceDirective', 'StripDirective',
    2627           'WhenDirective', 'WithDirective']
    2728__docformat__ = 'restructuredtext en'
     
    185186        return _apply_directives(_generate(), directives, ctxt, vars)
    186187
    187188
     189class 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
    188224class ContentDirective(Directive):
    189225    """Implementation of the ``py:content`` template directive.
    190226