Don't match variable or comment blocks, Use {%+ to disable lstrip_blocks behavior manually
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kristi Tsukida · committed by Kristi Tsukida · parent fb53966 · Browse files · 1 file·+27 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+27 added−8 removed
- other:+27 added−8 removed
Provenance
- Commit
fdf8201- Tree
4906379- Author
Kristi Tsukida <kristi.dev@gmail.com>- Committer
Kristi Tsukida <kristi.dev@gmail.com>- Parent
fb53966
1 file changed+27 added−8 removed
modified:jinja2/lexer.py+27 added-8 removed
423 unmodified lines424 root_tag_rules = compile_rules(environment)425 426 # block suffix if trimming is enabled427 block_suffix_re = environment.trim_blocks and '\\n?' or ''428+429+ # use '{%+' to manually disable lstrip_blocks behavior430+ no_lstrip_re = e('+')431+ # detect overlap between block and variable or comment strings432+ block_diff_re = c(r'^%s(.*)' % e(environment.block_start_string))433+ # make sure we don't mistake a block for a variable or a comment434+ m = block_diff_re.match(environment.comment_start_string)435+ no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''436+ m = block_diff_re.match(environment.variable_start_string)437+ no_lstrip_re += m and r'|%s' % e(m.group(1)) or ''438+ no_variable_re = m and r'(?!%s)' % e(m.group(1)) or ''439+440 # strip leading spaces if lstrip_blocks is enabled429- block_prefix_re = environment.lstrip_blocks and r'^[ \t]*' or ''441+ prefix_re = {}442+ if environment.lstrip_blocks:443+ lstrip_re = r'^[ \t]*'444+ block_prefix_re = r'%s%s(?!%s)|%s\+?' % (445+ lstrip_re,446+ e(environment.block_start_string),447+ no_lstrip_re,448+ e(environment.block_start_string),449+ )450+ prefix_re['block'] = block_prefix_re451+ else:452+ block_prefix_re = '%s' % e(environment.block_start_string)453 454 self.newline_sequence = environment.newline_sequence455 456 # global lexing rules457 self.rules = {458 'root': [459 # directives460 (c('(.*?)(?:%s)' % '|'.join(438- [r'(?P<raw_begin>(?:\s*%s\-|%s%s|%s)\s*raw\s*(?:\-%s\s*|%s))' % (461+ [r'(?P<raw_begin>(?:\s*%s\-|%s)\s*raw\s*(?:\-%s\s*|%s))' % (462 e(environment.block_start_string),463 block_prefix_re,441- e(environment.block_start_string),442- e(environment.block_start_string),464 e(environment.block_end_string),465 e(environment.block_end_string)466 )] + [446- r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, r if n != "block" else '%s%s|%s' % (block_prefix_re, r, r) )467+ r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, prefix_re.get(n,r))468 for n, r in root_tag_rules469 ])), (TOKEN_DATA, '#bygroup'), '#bygroup'),470 # data471 (c('.+'), TOKEN_DATA, None)23 unmodified lines495 )), TOKEN_VARIABLE_END, '#pop')496 ] + tag_rules,497 # raw block498 TOKEN_RAW_BEGIN: [478- (c('(.*?)((?:\s*%s\-|%s%s|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (499+ (c('(.*?)((?:\s*%s\-|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (500 e(environment.block_start_string),501 block_prefix_re,481- e(environment.block_start_string),482- e(environment.block_start_string),502 e(environment.block_end_string),503 e(environment.block_end_string),504 block_suffix_re505 )), (TOKEN_DATA, TOKEN_RAW_END), '#pop'),202 unmodified lines