jinja
jinja copied to clipboard
Line statement and leading empty lines
Following up #52, I'm wondering while Jinja does not also keep leading empty lines after a line statement.
Expected Behavior
== No empty lines
0
1
2
== Leading empty line
0
1
2
== Trailing empty line
0
1
2
== Leading and trailing empty lines
0
1
2
Actual Behavior
== No empty lines
0
1
2
== Leading empty line
0
1
2
== Trailing empty line
0
1
2
== Leading and trailing empty lines
0
1
2
Template Code
env = Environment(line_statement_prefix='%%')
print('== No empty lines')
print(env.from_string("""%% for item in seq
{{item}}
%% endfor""").render(seq=list(range(3))), end='')
print('== Leading empty line')
print(env.from_string("""%% for item in seq
{{item}}
%% endfor""").render(seq=list(range(3))), end='')
print('== Trailing empty line')
print(env.from_string("""%% for item in seq
{{item}}
%% endfor""").render(seq=list(range(3))), end='')
print('== Leading and trailing empty lines')
print(env.from_string("""%% for item in seq
{{item}}
%% endfor""").render(seq=list(range(3))), end='')
Your Environment
- Python version: 2.7.17
- Jinja version: 2.11.2
Proposed Fix
The fix for #52 only handled the trailing empty lines. Here is a fix to also handle the leading empty lines:
diff --git a/src/jinja2/lexer.py b/src/jinja2/lexer.py
index 552356a..f8fb7f5 100644 src/jinja2/lexer.py
--- a/src/jinja2/lexer.py
+++ b/src/jinja2/lexer.py
@@ -593,7 +593,7 @@ class Lexer(object):
],
# line statements
TOKEN_LINESTATEMENT_BEGIN: [
- (c(r"\s*(\n|$)"), TOKEN_LINESTATEMENT_END, "#pop")
+ (c(r"[ \t\v]*(\n|$)"), TOKEN_LINESTATEMENT_END, "#pop")
]
+ tag_rules,
# line comments
@davidism any comments?
Working on other things right now. Please don't ping maintainers for updates, we'll get to it when we can. Don't have an idea for this yet, given other space-related issues right now.
@mvolfik could you try to explain me, what is the difference between my proposal and yours? Thanks.