changeset 242:71062601458a trunk

Implement comments and directive escaping for text templates.
author cmlenz
date Wed, 13 Sep 2006 16:44:12 +0000
parents 4d81439bc097
children 2265ded3e14b
files genshi/template.py genshi/tests/template.py
diffstat 2 files changed, 26 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/template.py
+++ b/genshi/template.py
@@ -1216,7 +1216,6 @@
     Foobar
     """
     directives = [('def', DefDirective),
-                  ('comment', StripDirective),
                   ('when', WhenDirective),
                   ('otherwise', OtherwiseDirective),
                   ('for', ForDirective),
@@ -1224,7 +1223,7 @@
                   ('choose', ChooseDirective),
                   ('with', WithDirective)]
 
-    _directive_re = re.compile('^\s*#(\w+.*)\n?', re.MULTILINE)
+    _DIRECTIVE_RE = re.compile(r'^\s*(?<!\\)#((?:\w+|#).*)\n?', re.MULTILINE)
 
     def _parse(self):
         """Parse the template from text input."""
@@ -1235,7 +1234,8 @@
         source = self.source.read()
         offset = 0
         lineno = 1
-        for idx, mo in enumerate(self._directive_re.finditer(source)):
+
+        for idx, mo in enumerate(self._DIRECTIVE_RE.finditer(source)):
             start, end = mo.span()
             if start > offset:
                 text = source[offset:start]
@@ -1252,25 +1252,25 @@
             else:
                 command, value = directive[0], None
 
-            if command != 'end':
+            if command == 'end':
+                depth -= 1
+                if depth in dirmap:
+                    directive, start_offset = dirmap.pop(depth)
+                    substream = stream[start_offset:]
+                    stream[start_offset:] = [(SUB, ([directive], substream),
+                                              (self.filename, lineno, 0))]
+            elif command != '#':
                 cls = self._dir_by_name.get(command)
                 if cls is None:
                     raise BadDirectiveError(command)
                 directive = cls(value, self.filename, lineno, 0)
                 dirmap[depth] = (directive, len(stream))
                 depth += 1
-            else:
-                depth -= 1
-                if depth in dirmap:
-                    directive, start_offset = dirmap.pop(depth)
-                    substream = stream[start_offset:]
-                    stream[start_offset:] = [(SUB, ([directive], substream),
-                                              (self.filename, lineno, 0))]
 
             offset = end
 
         if offset < len(source):
-            text = source[offset:]
+            text = source[offset:].replace('\\#', '#')
             for kind, data, pos in self._interpolate(text, self.filename,
                                                      lineno, 0):
                 stream.append((kind, data, pos))
--- a/genshi/tests/template.py
+++ b/genshi/tests/template.py
@@ -849,9 +849,7 @@
 
 
 class MarkupTemplateTestCase(unittest.TestCase):
-    """Tests for basic template processing, expression evaluation and error
-    reporting.
-    """
+    """Tests for markup template processing."""
 
     def test_interpolate_mixed3(self):
         tmpl = MarkupTemplate('<root> ${var} $var</root>')
@@ -983,6 +981,18 @@
         </div>""", str(tmpl.generate()))
 
 
+class TextTemplateTestCase(unittest.TestCase):
+    """Tests for text template processing."""
+
+    def test_escaping(self):
+        tmpl = TextTemplate('\\#escaped')
+        self.assertEqual('#escaped', str(tmpl.generate()))
+
+    def test_comment(self):
+        tmpl = TextTemplate('## a comment')
+        self.assertEqual('', str(tmpl.generate()))
+
+
 class TemplateLoaderTestCase(unittest.TestCase):
     """Tests for the template loader."""
 
@@ -1092,6 +1102,7 @@
     suite.addTest(unittest.makeSuite(WithDirectiveTestCase, 'test'))
     suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
     suite.addTest(unittest.makeSuite(MarkupTemplateTestCase, 'test'))
+    suite.addTest(unittest.makeSuite(TextTemplateTestCase, 'test'))
     suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test'))
     return suite
 
Copyright (C) 2012-2017 Edgewall Software