From: Kristi Tsukida Date: Wed, 11 Jul 2012 00:13:50 +0000 (-0700) Subject: add lstrip_blocks environment setting, fix lexer X-Git-Tag: 2.7~48^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=59f33664cfcfc6c7fcd2a9bec4a8848b829bde68;p=thirdparty%2Fjinja.git add lstrip_blocks environment setting, fix lexer --- diff --git a/jinja2/defaults.py b/jinja2/defaults.py index d2d45443..559a2ef5 100644 --- a/jinja2/defaults.py +++ b/jinja2/defaults.py @@ -21,6 +21,7 @@ COMMENT_END_STRING = '#}' LINE_STATEMENT_PREFIX = None LINE_COMMENT_PREFIX = None TRIM_BLOCKS = False +LSTRIP_BLOCKS = False NEWLINE_SEQUENCE = '\n' diff --git a/jinja2/environment.py b/jinja2/environment.py index ebb54548..6f089da4 100644 --- a/jinja2/environment.py +++ b/jinja2/environment.py @@ -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) diff --git a/jinja2/lexer.py b/jinja2/lexer.py index 917f0729..adb32627 100644 --- a/jinja2/lexer.py +++ b/jinja2/lexer.py @@ -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(?:\s*%s\-|%s%s)\s*raw\s*(?:\-%s\s*|%s))' % ( + [r'(?P(?:\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