# HG changeset patch # User cmlenz # Date 1158165852 0 # Node ID 71062601458abb808f43ad309c7d65ac38d10619 # Parent 4d81439bc0976dcbe8b92d0ca32e613d5fe994c4 Implement comments and directive escaping for text templates. diff --git a/genshi/template.py b/genshi/template.py --- 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*(? 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)) diff --git a/genshi/tests/template.py b/genshi/tests/template.py --- 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(' ${var} $var') @@ -983,6 +981,18 @@ """, 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