Commits

Andriy Kornatskyy committed a488be8

Improved whitespace extension for wheezy.template.

Comments (0)

Files changed (2)

src/wheezy/html/ext/template.py

     preprocessors = [WheezyPreprocessor()]
 
 
+whitespace_preprocessor = WhitespacePreprocessor(
+    rules=[
+        (re.compile(r'^[ \t]+', re.MULTILINE),
+            r''),
+        (re.compile(r'>\s*<', re.MULTILINE),
+            r'><'),
+        (re.compile(r'\s*(?<!\\)\n', re.MULTILINE),
+            r'\\\n'),
+    ],
+    ignore_rules=[
+        re.compile(r'<(pre|code).*?>.*?</\1>', re.DOTALL)
+    ])
+
+
+def whitespace_postprocessor(tokens):
+    for i in xrange(len(tokens)):
+        lineno, token, value = tokens[i]
+        if token == 'markup':
+            value = whitespace_preprocessor(value)
+            tokens[i] = (lineno, token, value)
+
+
 class WhitespaceExtension(object):
 
     preprocessors = [WhitespacePreprocessor(rules=[
-        (re.compile(r'>\s+<'),
-            r'><'),
-        (re.compile(r'^\s*$', re.MULTILINE),
-            r''),
-        (re.compile(r'^ \s+', re.MULTILINE),
-            r''),
-        (re.compile(r'\s+$', re.MULTILINE),
-            r''),
-        (re.compile(r'>\n@def'),
-            r'>\\\n@def'),
-        (re.compile(r'(?<!:|\\)\n@end'),
-            r'\\\n@end'),
+        (re.compile(r'\s+$', re.MULTILINE), r'')
     ])]
+
+    postprocessors = [whitespace_postprocessor]

src/wheezy/html/ext/tests/test_template.py

 
     def setUp(self):
         from wheezy.html.ext.template import WhitespaceExtension
-        self.preprocess = WhitespaceExtension.preprocessors[0]
+        from wheezy.html.ext.template import whitespace_preprocessor
+        whitespace_preprocessor1 = WhitespaceExtension.preprocessors[0]
+        self.preprocess = lambda text: whitespace_preprocessor(
+            whitespace_preprocessor1(text))
 
     def test_whitespace(self):
         """
         assert '' == self.preprocess('  ')
         assert 'x' == self.preprocess('  x')
         assert 'x' == self.preprocess('x  ')
-        assert '>' == self.preprocess('>  ')
-        assert '>\\\n@def' == self.preprocess('>\n@def')
-        assert '>\\\n@end' == self.preprocess('>\n@end')
-        assert ':\n@end' == self.preprocess(':\n@end')
-        assert 'x\\\n@end' == self.preprocess('x\\\n@end')
+        assert '<code>\n 1\n 2 </code>' == self.preprocess(
+            '  <code> \n 1 \n 2 </code> ')
+        assert '<pre>\n </pre>' == self.preprocess('  <pre> \n </pre> ')
 
 try:
     from wheezy.html.ext.template import WidgetExtension