Commits

Andriy Kornatskyy committed 6841424

Fixed Issue #1: Preserve spaces before var token.

Comments (0)

Files changed (5)

src/wheezy/template/ext/code.py

     def __init__(self, token_start='@'):
 
         self.lexer_rules = {
-            300: (re.compile(r'%s(?=\()' % token_start), code_token),
+            300: (re.compile(r'\s*%s(?=\()' % token_start), code_token),
         }
 
     parser_rules = {

src/wheezy/template/ext/core.py

 
         # region: preprocessors
 
-        RE_CLEAN1 = re.compile('^([ ]+)%s(?!%s)'
-                               % (token_start, token_start), re.S)
-        RE_CLEAN2 = re.compile('\n([ ]+)%s(?!%s)'
-                               % (token_start, token_start), re.S)
+        RE_CLEAN1 = re.compile(r'^([ ]+)%s(%s)'
+                               % (token_start, '|'.join(all_tokens)), re.S)
+        RE_CLEAN2 = re.compile(r'\n([ ]+)%s(%s)'
+                               % (token_start, '|'.join(all_tokens)), re.S)
 
         def clean_source(source):
-            """ Cleans leading whitespace before token start. Ignores
-                escaped token start.
+            """ Cleans leading whitespace before token start for all control
+                tokens. Ignores escaped token start.
             """
-            return RE_CLEAN2.sub('\n' + token_start, RE_CLEAN1.sub(
-                token_start, source.replace('\r\n', '\n')))
+            return RE_CLEAN2.sub(r'\n%s\2' % token_start, RE_CLEAN1.sub(
+                r'%s\2' % token_start, source.replace('\r\n', '\n')))
 
         self.preprocessors = [clean_source]
 

src/wheezy/template/ext/tests/test_code.py

     def test_code_token(self):
         """ Test code token.
         """
-        tokens = self.tokenize('@(i = 1)')
+        tokens = self.tokenize('\n @(i = 1)')
+        assert 1 == len(tokens)
         assert (1, 'code', '(i = 1)') == tokens[0]
         tokens = self.tokenize('@(i = 1)\n')
         assert (1, 'code', '(i = 1)\n') == tokens[0]

src/wheezy/template/ext/tests/test_core.py

         """
         assert 'a\nb' == self.clean_source('a\r\nb')
 
-    def test_leading_whitespace(self):
-        """ Remove leading whitespace before @ symbol.
+    def test_clean_leading_whitespace(self):
+        """ Remove leading whitespace before @<stmt>, e.g. @if, @for, etc.
         """
-        assert 'a\n@b' == self.clean_source('a\n  @b')
-        assert '@b' == self.clean_source('  @b')
+        from wheezy.template.ext.core import all_tokens
+        for token in all_tokens:
+            assert '@' + token == self.clean_source('  @' + token)
+            assert '\n@' + token == self.clean_source('\n  @' + token)
+            assert 'a\n@' + token == self.clean_source('a\n  @' + token)
+
+    def test_leave_leading_whitespace(self):
+        """ Leave leading whitespace before @<var> tokens.
+        """
+        assert 'a\n\n   @b' == self.clean_source('a\n\n   @b')
+        assert 'a\n @b' == self.clean_source('a\n @b')
+        assert 'a\n@b' == self.clean_source('a\n@b')
+        assert 'a@b' == self.clean_source('a@b')
+        assert '  @b' == self.clean_source('  @b')
 
     def test_ignore(self):
         """ Ignore double @.
         assert "w('Welcome, '); w(username); w('!')" == self.build_source(
             'Welcome, @username!')
         assert """\
-w('\\n<i>\\n')
+w('\\n<i>\\n    ')
 
 w(username); w('\\n</i>')""" == self.build_source("""
 <i>
     def test_for(self):
         assert """\
 for color in colors:
-    w(color); w('\\n')""" == self.build_source("""\
+    w('    '); w(color); w('\\n')""" == self.build_source("""\
 @for color in colors:
     @color
 @end
         ctx = {
             'colors': ['red', 'yellow']
         }
-        assert 'red\nyellow\n' == self.render(ctx, """\
+        assert '    red\n    yellow\n' == self.render(ctx, """\
 @require(colors)
 @for color in colors:
     @color
 @end
 """
         })
-        assert '    Hello, John!!!' == self.render('tmpl.html', {})
+        assert '        Hello, John!!!' == self.render('tmpl.html', {})
 
     def test_include(self):
         self.templates.update({

src/wheezy/template/tests/test_preprocessor.py

 """
         })
 
-        assert 'Hi, John!\n' == self.render('tmpl.html', ctx={
+        assert '    Hi, John!\n' == self.render('tmpl.html', ctx={
             '_': lambda x: x,
         })