]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Apply lstrip_blocks to comments (match trim_blocks behavior)
authorKristi Tsukida <kristi.dev@gmail.com>
Thu, 12 Jul 2012 20:37:21 +0000 (13:37 -0700)
committerKristi Tsukida <kristi.dev@gmail.com>
Thu, 12 Jul 2012 20:37:21 +0000 (13:37 -0700)
jinja2/lexer.py

index f7f920d687bac3890e7a47cd0feaf24c74f95df2..e488601083ba6af2e4a1cbbc0d45fd67febc6633 100644 (file)
@@ -426,20 +426,24 @@ class Lexer(object):
         # block suffix if trimming is enabled
         block_suffix_re = environment.trim_blocks and '\\n?' or ''
 
-        # use '{%+' to manually disable lstrip_blocks behavior
-        no_lstrip_re = e('+')
-        # detect overlap between block and variable or comment strings
-        block_diff_re = c(r'^%s(.*)' % e(environment.block_start_string))
-        # make sure we don't mistake a block for a variable or a comment
-        m = block_diff_re.match(environment.comment_start_string)
-        no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''
-        m = block_diff_re.match(environment.variable_start_string)
-        no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''
-        no_variable_re = m and r'(?!%s)' % e(m.group(1)) or ''
-
         # strip leading spaces if lstrip_blocks is enabled
         prefix_re = {}
         if environment.lstrip_blocks:
+            # use '{%+' to manually disable lstrip_blocks behavior
+            no_lstrip_re = e('+')
+            # detect overlap between block and variable or comment strings
+            block_diff = c(r'^%s(.*)' % e(environment.block_start_string))
+            # make sure we don't mistake a block for a variable or a comment
+            m = block_diff.match(environment.comment_start_string)
+            no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''
+            m = block_diff.match(environment.variable_start_string)
+            no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''
+
+            # detect overlap between comment and variable strings
+            comment_diff = c(r'^%s(.*)' % e(environment.comment_start_string))
+            m = comment_diff.match(environment.variable_start_string)
+            no_variable_re = m and r'(?!%s)' % e(m.group(1)) or ''
+
             lstrip_re = r'^[ \t]*'
             block_prefix_re = r'%s%s(?!%s)|%s\+?' % (
                     lstrip_re,
@@ -447,7 +451,14 @@ class Lexer(object):
                     no_lstrip_re,
                     e(environment.block_start_string),
                     )
+            comment_prefix_re = r'%s%s%s|%s\+?' % (
+                    lstrip_re,
+                    e(environment.comment_start_string),
+                    no_variable_re,
+                    e(environment.comment_start_string),
+                    )
             prefix_re['block'] = block_prefix_re
+            prefix_re['comment'] = comment_prefix_re
         else:
             block_prefix_re = '%s' % e(environment.block_start_string)