Skip to content

Commit de41400

Browse files
committed
Fix trailing whitespace/newline logic
1 parent a6fbf5d commit de41400

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

scripts/jinja/jinja-tester.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,16 @@ def format_template_content(template_content):
142142

143143
i += 1
144144

145-
# Clean up leading and trailing empty lines that would change output
145+
# Clean up trailing newlines and spaces
146146
result = result.rstrip()
147147

148-
# But preserve the exact trailing newline behavior of the original
149-
if template_content.endswith("\n") and not result.endswith("\n"):
150-
result += "\n"
151-
elif not template_content.endswith("\n") and result.endswith("\n"):
152-
result = result[:-1]
148+
# Copy the newline / space count from the original
149+
tc_rstrip = template_content.rstrip()
150+
if not tc_rstrip.endswith("-%}") and not tc_rstrip.endswith(r'-}}'):
151+
# If orig ends with -%} or -}}, that means all newlines are stripped anyways
152+
# So we shouldn't add anything
153+
trailing_length = len(template_content) - len(tc_rstrip)
154+
result += template_content[:-trailing_length]
153155

154156
return result
155157

0 commit comments

Comments
 (0)