]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
add lstrip_blocks environment setting, fix lexer
authorKristi Tsukida <kristi.dev@gmail.com>
Wed, 11 Jul 2012 00:13:50 +0000 (17:13 -0700)
committerKristi Tsukida <kristi.dev@gmail.com>
Wed, 11 Jul 2012 00:13:50 +0000 (17:13 -0700)
jinja2/defaults.py
jinja2/environment.py
jinja2/lexer.py

index d2d45443adde9d94b1319bc4ffa54a5abbe99baf..559a2ef5f28c4ce2e68f9da7d58a9160c02a8e60 100644 (file)
@@ -21,6 +21,7 @@ COMMENT_END_STRING = '#}'
 LINE_STATEMENT_PREFIX = None
 LINE_COMMENT_PREFIX = None
 TRIM_BLOCKS = False
+LSTRIP_BLOCKS = False
 NEWLINE_SEQUENCE = '\n'
 
 
index ebb54548e3ef901f6606863778f32572c5f2ba44..6f089da4601d3b481d26d55d7232bb97e6a2424c 100644 (file)
@@ -134,6 +134,10 @@ class Environment(object):
             If this is set to ``True`` the first newline after a block is
             removed (block, not variable tag!).  Defaults to `False`.
 
+        `lstrip_blocks`
+            If this is set to ``True`` leading spaces and tabs are stripped
+            from a block if the block starts the line.  Defaults to `False`.
+
         `newline_sequence`
             The sequence that starts a newline.  Must be one of ``'\r'``,
             ``'\n'`` or ``'\r\n'``.  The default is ``'\n'`` which is a
@@ -224,6 +228,7 @@ class Environment(object):
                  line_statement_prefix=LINE_STATEMENT_PREFIX,
                  line_comment_prefix=LINE_COMMENT_PREFIX,
                  trim_blocks=TRIM_BLOCKS,
+                 lstrip_blocks=LSTRIP_BLOCKS,
                  newline_sequence=NEWLINE_SEQUENCE,
                  extensions=(),
                  optimized=True,
@@ -255,6 +260,7 @@ class Environment(object):
         self.line_statement_prefix = line_statement_prefix
         self.line_comment_prefix = line_comment_prefix
         self.trim_blocks = trim_blocks
+        self.lstrip_blocks = lstrip_blocks
         self.newline_sequence = newline_sequence
 
         # runtime information
@@ -300,7 +306,8 @@ class Environment(object):
                 variable_start_string=missing, variable_end_string=missing,
                 comment_start_string=missing, comment_end_string=missing,
                 line_statement_prefix=missing, line_comment_prefix=missing,
-                trim_blocks=missing, extensions=missing, optimized=missing,
+                trim_blocks=missing, lstrip_blocks=missing,
+                extensions=missing, optimized=missing,
                 undefined=missing, finalize=missing, autoescape=missing,
                 loader=missing, cache_size=missing, auto_reload=missing,
                 bytecode_cache=missing):
@@ -816,6 +823,7 @@ class Template(object):
                 line_statement_prefix=LINE_STATEMENT_PREFIX,
                 line_comment_prefix=LINE_COMMENT_PREFIX,
                 trim_blocks=TRIM_BLOCKS,
+                lstrip_blocks=LSTRIP_BLOCKS,
                 newline_sequence=NEWLINE_SEQUENCE,
                 extensions=(),
                 optimized=True,
@@ -826,6 +834,7 @@ class Template(object):
             block_start_string, block_end_string, variable_start_string,
             variable_end_string, comment_start_string, comment_end_string,
             line_statement_prefix, line_comment_prefix, trim_blocks,
+            lstrip_blocks,
             newline_sequence, frozenset(extensions), optimized, undefined,
             finalize, autoescape, None, 0, False, None)
         return env.from_string(source, template_class=cls)
index 917f07294946cb01c8a6c606d0aad49a63201a92..adb326278be465e4757f3e094b2eeaa182b5c3aa 100644 (file)
@@ -424,8 +424,10 @@ class Lexer(object):
 
         # block suffix if trimming is enabled
         block_suffix_re = environment.trim_blocks and '\\n?' or ''
-        # TODO hook environment setting
-        block_prefix_re = True and '[ \\t]*' or ''
+        # strip leading spaces if lstrip_blocks is enabled
+        block_prefix_re = environment.lstrip_blocks and r'^[ \t]*' or ''
+
+        print 'block_prefix_re = %s' % block_prefix_re
 
         self.newline_sequence = environment.newline_sequence
 
@@ -434,14 +436,15 @@ class Lexer(object):
             'root': [
                 # directives
                 (c('(.*?)(?:%s)' % '|'.join(
-                    [r'(?P<raw_begin>(?:\s*%s\-|%s%s)\s*raw\s*(?:\-%s\s*|%s))' % (
+                    [r'(?P<raw_begin>(?:\s*%s\-|%s%s|%s)\s*raw\s*(?:\-%s\s*|%s))' % (
                         e(environment.block_start_string),
                         block_prefix_re,
                         e(environment.block_start_string),
+                        e(environment.block_start_string),
                         e(environment.block_end_string),
                         e(environment.block_end_string)
                     )] + [
-                        r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)
+                        r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, r if n != "block" else '%s%s|%s' % (block_prefix_re, r, r) )
                         for n, r in root_tag_rules
                     ])), (TOKEN_DATA, '#bygroup'), '#bygroup'),
                 # data
@@ -473,10 +476,11 @@ class Lexer(object):
             ] + tag_rules,
             # raw block
             TOKEN_RAW_BEGIN: [
-                (c('(.*?)((?:\s*%s\-|%s%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (
+                (c('(.*?)((?:\s*%s\-|%s%s|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (
                     e(environment.block_start_string),
                     block_prefix_re,
                     e(environment.block_start_string),
+                    e(environment.block_start_string),
                     e(environment.block_end_string),
                     e(environment.block_end_string),
                     block_suffix_re