Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line statement and leading empty lines #1202

Open
bertwesarg opened this issue May 5, 2020 · 3 comments
Open

Line statement and leading empty lines #1202

bertwesarg opened this issue May 5, 2020 · 3 comments

Comments

@bertwesarg
Copy link

bertwesarg commented May 5, 2020

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
@bertwesarg

This comment has been minimized.

@davidism

This comment has been minimized.

mvolfik added a commit to mvolfik/jinja that referenced this issue Mar 9, 2021
@bertwesarg
Copy link
Author

bertwesarg commented Aug 25, 2021

@mvolfik could you try to explain me, what is the difference between my proposal and yours? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants