TOKEN_COMMENT_END, TOKEN_WHITESPACE,
TOKEN_WHITESPACE, TOKEN_LINECOMMENT_BEGIN,
TOKEN_LINECOMMENT_END, TOKEN_LINECOMMENT])
+ignore_if_empty = frozenset([TOKEN_WHITESPACE, TOKEN_DATA,
+ TOKEN_COMMENT, TOKEN_LINECOMMENT])
def count_newlines(value):
if environment.line_statement_prefix is not None:
rules.append((len(environment.line_statement_prefix), 'linestatement',
- '^\\s*' + e(environment.line_statement_prefix)))
+ r'^\s*' + e(environment.line_statement_prefix)))
if environment.line_comment_prefix is not None:
rules.append((len(environment.line_comment_prefix), 'linecomment',
- '\\s*' + e(environment.line_comment_prefix)))
+ r'(?:^|(?<!\S))\s*' + e(environment.line_comment_prefix)))
return [x[1:] for x in sorted(rules, reverse=True)]
'root': [
# directives
(c('(.*?)(?:%s)' % '|'.join(
- ['(?P<raw_begin>(?:\s*%s\-|%s)\s*raw\s*%s)' % (
+ [r'(?P<raw_begin>(?:\s*%s\-|%s)\s*raw\s*%s)' % (
e(environment.block_start_string),
e(environment.block_start_string),
e(environment.block_end_string)
)] + [
- '(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)
+ r'(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)
for n, r in root_tag_rules
])), (TOKEN_DATA, '#bygroup'), '#bygroup'),
# data
- (c('.+'), 'data', None)
+ (c('.+'), TOKEN_DATA, None)
],
# comments
TOKEN_COMMENT_BEGIN: [
] + tag_rules,
# line comments
TOKEN_LINECOMMENT_BEGIN: [
- (c(r'.*?(?=\n|$)'), TOKEN_LINECOMMENT_END, '#pop')
+ (c(r'(.*?)()(?=\n|$)'), (TOKEN_LINECOMMENT,
+ TOKEN_LINECOMMENT_END), '#pop')
]
}
# normal group
else:
data = m.group(idx + 1)
- if data:
+ if data or token not in ignore_if_empty:
yield lineno, token, data
lineno += data.count('\n')
lineno, name,
filename)
# yield items
- yield lineno, tokens, data
+ if data or tokens not in ignore_if_empty:
+ yield lineno, tokens, data
lineno += data.count('\n')
# fetch new position into new variable so that we can check
{% macro blub() %}foo{% endmacro %}
{{ blub() }}'''
-LINE_SYNTAX_PRIORITY = '''\
+LINE_SYNTAX_PRIORITY1 = '''\
+/* ignore me.
+ I'm a multiline comment */
+## for item in seq:
+* ${item} # this is just extra stuff
+## endfor
+'''
+
+LINE_SYNTAX_PRIORITY2 = '''\
/* ignore me.
I'm a multiline comment */
# for item in seq:
def test_line_syntax_priority():
+ # XXX: why is the whitespace there in front of the newline?
+ env = Environment('{%', '%}', '${', '}', '/*', '*/', '##', '#')
+ tmpl = env.from_string(LINE_SYNTAX_PRIORITY1)
+ assert tmpl.render(seq=[1, 2]).strip() == '* 1 \n* 2'
env = Environment('{%', '%}', '${', '}', '/*', '*/', '#', '##')
- tmpl = env.from_string(LINE_SYNTAX_PRIORITY)
- assert tmpl.render(seq=[1, 2]).strip() == '* 1\n* 2'
+ tmpl = env.from_string(LINE_SYNTAX_PRIORITY2)
+ assert tmpl.render(seq=[1, 2]).strip() == '* 1 \n* 2'