Fixed bug in line-based comments with priority.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 59b6bd5 · Browse files · 2 files·+27 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+27 added−11 removed
- tests:+15 added−3 removed
- other:+12 added−8 removed
Provenance
- Commit
db7985d- Tree
78c6133- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
59b6bd5
2 files changed+27 added−11 removed
modified:jinja2/lexer.py+12 added-8 removed
122 unmodified lines123 ignored_tokens = frozenset([TOKEN_COMMENT_BEGIN, TOKEN_COMMENT,124 TOKEN_COMMENT_END, TOKEN_WHITESPACE,125 TOKEN_WHITESPACE, TOKEN_LINECOMMENT_BEGIN,126 TOKEN_LINECOMMENT_END, TOKEN_LINECOMMENT])127+ignore_if_empty = frozenset([TOKEN_WHITESPACE, TOKEN_DATA,128+ TOKEN_COMMENT, TOKEN_LINECOMMENT])129 130 131 def count_newlines(value):132 """Count the number of newline characters in the string. This is15 unmodified lines148 ]149 150 if environment.line_statement_prefix is not None:151 rules.append((len(environment.line_statement_prefix), 'linestatement',150- '^\\s*' + e(environment.line_statement_prefix)))152+ r'^\s*' + e(environment.line_statement_prefix)))153 if environment.line_comment_prefix is not None:154 rules.append((len(environment.line_comment_prefix), 'linecomment',153- '\\s*' + e(environment.line_comment_prefix)))155+ r'(?:^|(?<!\S))\s*' + e(environment.line_comment_prefix)))156 157 return [x[1:] for x in sorted(rules, reverse=True)]158 159 224 unmodified lines384 self.rules = {385 'root': [386 # directives387 (c('(.*?)(?:%s)' % '|'.join(386- ['(?P<raw_begin>(?:\s*%s\-|%s)\s*raw\s*%s)' % (388+ [r'(?P<raw_begin>(?:\s*%s\-|%s)\s*raw\s*%s)' % (389 e(environment.block_start_string),390 e(environment.block_start_string),391 e(environment.block_end_string)392 )] + [391- '(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)393+ r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)394 for n, r in root_tag_rules395 ])), (TOKEN_DATA, '#bygroup'), '#bygroup'),396 # data395- (c('.+'), 'data', None)397+ (c('.+'), TOKEN_DATA, None)398 ],399 # comments400 TOKEN_COMMENT_BEGIN: [401 (c(r'(.*?)((?:\-%s\s*|%s)%s)' % (34 unmodified lines436 (c(r'\s*(\n|$)'), TOKEN_LINESTATEMENT_END, '#pop')437 ] + tag_rules,438 # line comments439 TOKEN_LINECOMMENT_BEGIN: [438- (c(r'.*?(?=\n|$)'), TOKEN_LINECOMMENT_END, '#pop')440+ (c(r'(.*?)()(?=\n|$)'), (TOKEN_LINECOMMENT,441+ TOKEN_LINECOMMENT_END), '#pop')442 ]443 }444 445 def _normalize_newlines(self, value):107 unmodified lines553 % regex)554 # normal group555 else:556 data = m.group(idx + 1)554- if data:557+ if data or token not in ignore_if_empty:558 yield lineno, token, data559 lineno += data.count('\n')560 561 # strings as token just are yielded as it.19 unmodified lines581 (data, expected_op),582 lineno, name,583 filename)584 # yield items585+ if data or tokens not in ignore_if_empty:582- yield lineno, tokens, data586+ yield lineno, tokens, data587 lineno += data.count('\n')588 589 # fetch new position into new variable so that we can check590 # if there is a internal parsing error which would result42 unmodified linesmodified:tests/test_parser.py+15 added-3 removed
44 unmodified lines45 and bar comment #}46 {% macro blub() %}foo{% endmacro %}47 {{ blub() }}'''48 49-LINE_SYNTAX_PRIORITY = '''\49+LINE_SYNTAX_PRIORITY1 = '''\50 /* ignore me.51 I'm a multiline comment */52+## for item in seq:53+* ${item} # this is just extra stuff54+## endfor55+'''56+57+LINE_SYNTAX_PRIORITY2 = '''\58+/* ignore me.59+ I'm a multiline comment */60 # for item in seq:61 * ${item} ## this is just extra stuff62 # endfor63 '''39 unmodified lines103 range(5)104 105 106 def test_line_syntax_priority():107+ # XXX: why is the whitespace there in front of the newline?108+ env = Environment('{%', '%}', '${', '}', '/*', '*/', '##', '#')109+ tmpl = env.from_string(LINE_SYNTAX_PRIORITY1)110+ assert tmpl.render(seq=[1, 2]).strip() == '* 1 \n* 2'111 env = Environment('{%', '%}', '${', '}', '/*', '*/', '#', '##')100- tmpl = env.from_string(LINE_SYNTAX_PRIORITY)101- assert tmpl.render(seq=[1, 2]).strip() == '* 1\n* 2'112+ tmpl = env.from_string(LINE_SYNTAX_PRIORITY2)113+ assert tmpl.render(seq=[1, 2]).strip() == '* 1 \n* 2'