Permalink
Browse files

add lstrip_blocks environment setting, fix lexer

  • Loading branch information...
1 parent 15605a8 commit 59f33664cfcfc6c7fcd2a9bec4a8848b829bde68 @kristi committed Jul 11, 2012
Showing with 20 additions and 6 deletions.
  1. +1 −0 jinja2/defaults.py
  2. +10 −1 jinja2/environment.py
  3. +9 −5 jinja2/lexer.py
View
@@ -21,6 +21,7 @@
LINE_STATEMENT_PREFIX = None
LINE_COMMENT_PREFIX = None
TRIM_BLOCKS = False
+LSTRIP_BLOCKS = False
NEWLINE_SEQUENCE = '\n'
View
@@ -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 @@ def __init__(self,
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 @@ def __init__(self,
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 @@ def overlay(self, block_start_string=missing, block_end_string=missing,
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 @@ def __new__(cls, source,
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 @@ def __new__(cls, source,
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)
View
@@ -424,8 +424,10 @@ def __init__(self, environment):
# 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 @@ def __init__(self, environment):
'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 @@ def __init__(self, environment):
] + 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

0 comments on commit 59f3366

Please sign in to comment.