Permalink
Browse files

Don't match variable or comment blocks, Use {%+ to disable lstrip_blo…

…cks behavior manually
  • Loading branch information...
1 parent fb53966 commit fdf8201437e8978c15fa542073c4f7243dff7f9c @kristi committed Jul 12, 2012
Showing with 27 additions and 8 deletions.
  1. +27 −8 jinja2/lexer.py
View
35 jinja2/lexer.py
@@ -425,8 +425,31 @@ def __init__(self, environment):
# 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
- block_prefix_re = environment.lstrip_blocks and r'^[ \t]*' or ''
+ prefix_re = {}
+ if environment.lstrip_blocks:
+ lstrip_re = r'^[ \t]*'
+ block_prefix_re = r'%s%s(?!%s)|%s\+?' % (
+ lstrip_re,
+ e(environment.block_start_string),
+ no_lstrip_re,
+ e(environment.block_start_string),
+ )
+ prefix_re['block'] = block_prefix_re
+ else:
+ block_prefix_re = '%s' % e(environment.block_start_string)
self.newline_sequence = environment.newline_sequence
@@ -435,15 +458,13 @@ def __init__(self, environment):
'root': [
# directives
(c('(.*?)(?:%s)' % '|'.join(
- [r'(?P<raw_begin>(?:\s*%s\-|%s%s|%s)\s*raw\s*(?:\-%s\s*|%s))' % (
+ [r'(?P<raw_begin>(?:\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 if n != "block" else '%s%s|%s' % (block_prefix_re, r, r) )
+ r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, prefix_re.get(n,r))
for n, r in root_tag_rules
])), (TOKEN_DATA, '#bygroup'), '#bygroup'),
# data
@@ -475,11 +496,9 @@ def __init__(self, environment):
] + tag_rules,
# raw block
TOKEN_RAW_BEGIN: [
- (c('(.*?)((?:\s*%s\-|%s%s|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (
+ (c('(.*?)((?:\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 fdf8201

Please sign in to comment.