add lstrip_blocks environment setting, fix lexer
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 15605a8 · Browse files · 3 files·+20 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+20 added−6 removed
- other:+20 added−6 removed
Provenance
- Commit
59f3366- Tree
01dbb00- Author
Kristi Tsukida <kristi.dev@gmail.com>- Committer
Kristi Tsukida <kristi.dev@gmail.com>- Parent
15605a8
3 files changed+20 added−6 removed
modified:jinja2/defaults.py+1 added
19 unmodified lines20 COMMENT_END_STRING = '#}'21 LINE_STATEMENT_PREFIX = None22 LINE_COMMENT_PREFIX = None23 TRIM_BLOCKS = False24+LSTRIP_BLOCKS = False25 NEWLINE_SEQUENCE = '\n'26 27 28 # default filters, tests and namespace13 unmodified linesmodified:jinja2/environment.py+10 added-1 removed
132 unmodified lines133 `trim_blocks`134 If this is set to ``True`` the first newline after a block is135 removed (block, not variable tag!). Defaults to `False`.136 137+ `lstrip_blocks`138+ If this is set to ``True`` leading spaces and tabs are stripped139+ from a block if the block starts the line. Defaults to `False`.140+141 `newline_sequence`142 The sequence that starts a newline. Must be one of ``'\r'``,143 ``'\n'`` or ``'\r\n'``. The default is ``'\n'`` which is a144 useful default for Linux and OS X systems as well as web82 unmodified lines227 comment_end_string=COMMENT_END_STRING,228 line_statement_prefix=LINE_STATEMENT_PREFIX,229 line_comment_prefix=LINE_COMMENT_PREFIX,230 trim_blocks=TRIM_BLOCKS,231+ lstrip_blocks=LSTRIP_BLOCKS,232 newline_sequence=NEWLINE_SEQUENCE,233 extensions=(),234 optimized=True,235 undefined=Undefined,23 unmodified lines259 self.comment_end_string = comment_end_string260 self.line_statement_prefix = line_statement_prefix261 self.line_comment_prefix = line_comment_prefix262 self.trim_blocks = trim_blocks263+ self.lstrip_blocks = lstrip_blocks264 self.newline_sequence = newline_sequence265 266 # runtime information267 self.undefined = undefined37 unmodified lines305 def overlay(self, block_start_string=missing, block_end_string=missing,306 variable_start_string=missing, variable_end_string=missing,307 comment_start_string=missing, comment_end_string=missing,308 line_statement_prefix=missing, line_comment_prefix=missing,303- trim_blocks=missing, extensions=missing, optimized=missing,309+ trim_blocks=missing, lstrip_blocks=missing,310+ extensions=missing, optimized=missing,311 undefined=missing, finalize=missing, autoescape=missing,312 loader=missing, cache_size=missing, auto_reload=missing,313 bytecode_cache=missing):314 """Create a new overlay environment that shares all the data with the507 unmodified lines822 comment_end_string=COMMENT_END_STRING,823 line_statement_prefix=LINE_STATEMENT_PREFIX,824 line_comment_prefix=LINE_COMMENT_PREFIX,825 trim_blocks=TRIM_BLOCKS,826+ lstrip_blocks=LSTRIP_BLOCKS,827 newline_sequence=NEWLINE_SEQUENCE,828 extensions=(),829 optimized=True,830 undefined=Undefined,2 unmodified lines833 env = get_spontaneous_environment(834 block_start_string, block_end_string, variable_start_string,835 variable_end_string, comment_start_string, comment_end_string,836 line_statement_prefix, line_comment_prefix, trim_blocks,837+ lstrip_blocks,838 newline_sequence, frozenset(extensions), optimized, undefined,839 finalize, autoescape, None, 0, False, None)840 return env.from_string(source, template_class=cls)841 289 unmodified linesmodified:jinja2/lexer.py+9 added-5 removed
422 unmodified lines423 root_tag_rules = compile_rules(environment)424 425 # block suffix if trimming is enabled426 block_suffix_re = environment.trim_blocks and '\\n?' or ''427- # TODO hook environment setting428- block_prefix_re = True and '[ \\t]*' or ''427+ # strip leading spaces if lstrip_blocks is enabled428+ block_prefix_re = environment.lstrip_blocks and r'^[ \t]*' or ''429 430+ print 'block_prefix_re = %s' % block_prefix_re431+432 self.newline_sequence = environment.newline_sequence433 434 # global lexing rules435 self.rules = {436 'root': [437 # directives438 (c('(.*?)(?:%s)' % '|'.join(437- [r'(?P<raw_begin>(?:\s*%s\-|%s%s)\s*raw\s*(?:\-%s\s*|%s))' % (439+ [r'(?P<raw_begin>(?:\s*%s\-|%s%s|%s)\s*raw\s*(?:\-%s\s*|%s))' % (440 e(environment.block_start_string),441 block_prefix_re,442 e(environment.block_start_string),443+ e(environment.block_start_string),444 e(environment.block_end_string),445 e(environment.block_end_string)446 )] + [444- r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)447+ r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, r if n != "block" else '%s%s|%s' % (block_prefix_re, r, r) )448 for n, r in root_tag_rules449 ])), (TOKEN_DATA, '#bygroup'), '#bygroup'),450 # data451 (c('.+'), TOKEN_DATA, None)23 unmodified lines475 )), TOKEN_VARIABLE_END, '#pop')476 ] + tag_rules,477 # raw block478 TOKEN_RAW_BEGIN: [476- (c('(.*?)((?:\s*%s\-|%s%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (479+ (c('(.*?)((?:\s*%s\-|%s%s|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (480 e(environment.block_start_string),481 block_prefix_re,482 e(environment.block_start_string),483+ e(environment.block_start_string),484 e(environment.block_end_string),485 e(environment.block_end_string),486 block_suffix_re487 )), (TOKEN_DATA, TOKEN_RAW_END), '#pop'),202 unmodified lines