fix(formatter): fixed issue with blank links added between blocks

closes #402
This commit is contained in:
Christopher Pickering 2022-09-22 09:41:20 +02:00
parent 8f36fec142
commit 31161ce1aa
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 56 additions and 4 deletions

View file

@ -104,7 +104,11 @@ def condense_html(html: str, config: Config) -> str:
if inside_ignored_block(config, html, match):
return match.group()
return match.group() + "\n"
# check that next line is not blank.
if html[match.end() : match.end() + 1] != "\n": # noqa:E203
return match.group() + "\n"
return match.group()
func = partial(add_blank_line_after, config, html)
@ -113,7 +117,7 @@ def condense_html(html: str, config: Config) -> str:
for tag in [x.strip() for x in config.blank_line_after_tag.split(",")]:
html = re.sub(
re.compile(
rf"((?:{{%\s*?{tag}\b[^}}]+?%}}\n?)+)(?=[^\n])",
rf"((?:{{%\s*?{tag}\b[^}}]+?%}}\n?)+)",
re.IGNORECASE | re.MULTILINE | re.DOTALL,
),
func,
@ -132,6 +136,7 @@ def condense_html(html: str, config: Config) -> str:
# should we add blank lines before load tags?
if config.blank_line_before_tag:
for tag in [x.strip() for x in config.blank_line_before_tag.split(",")]:
html = re.sub(
re.compile(
rf"(?<!^\n)((?:{{%\s*?{tag}\b[^}}]+?%}}\n?)+)",

View file

@ -0,0 +1,4 @@
{% include "pages/task/details/data_source.html.j2" %}
{% include "pages/task/details/query_location.html.j2" %}
{% include "pages/task/details/processing.html.j2" %}
{% include "pages/task/details/destination.html.j2" %}

View file

@ -7,7 +7,7 @@ run::
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
pytest tests/test_config/test_blank_lines_after_tag/test_config.py::test_blank_lines_after_tag_eight --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
@ -36,6 +36,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
def test_blank_lines_after_tag_two(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_after_tag/html_two.html", "--check"],
@ -49,6 +51,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
def test_blank_lines_after_tag_three(runner: CliRunner) -> None:
# check blocks that do not start on a newline - they should be left as is.
result = runner.invoke(
djlint,
@ -58,6 +62,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
assert """0 files would be updated.""" in result.output
assert result.exit_code == 0
def test_blank_lines_after_tag_four(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_after_tag/html_four.html", "--check"],
@ -74,6 +80,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
in result.output
)
def test_blank_lines_after_tag_five(runner: CliRunner) -> None:
# something perfect should stay perfect :)
result = runner.invoke(
djlint,
@ -81,6 +89,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
)
assert result.exit_code == 0
def test_blank_lines_after_tag_six(runner: CliRunner) -> None:
# something perfect should stay perfect :)
result = runner.invoke(
djlint,
@ -88,6 +98,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
)
assert result.exit_code == 0
def test_blank_lines_after_tag_seven(runner: CliRunner) -> None:
# make sure endblock doesn't pick up endblocktrans :)
result = runner.invoke(
djlint,
@ -95,6 +107,8 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
)
assert result.exit_code == 0
def test_blank_lines_after_tag_eight(runner: CliRunner) -> None:
# check that multiple blank lines are not added
result = runner.invoke(
djlint,
@ -104,8 +118,11 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
"--check",
],
)
print(result.output)
assert result.exit_code == 0
def test_blank_lines_after_tag_nine(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
[
@ -114,3 +131,14 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
],
)
assert result.exit_code == 0
def test_blank_lines_after_tag_ten(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
[
"tests/test_config/test_blank_lines_after_tag/html_ten.html",
"--check",
],
)
assert result.exit_code == 0

View file

@ -7,7 +7,7 @@ run::
for a single test, run::
pytest tests/test_config.py::test_custom_html --cov=src/djlint \
pytest tests/test_config/test_blank_lines_before_tag/test_config.py::test_blank_lines_before_tag_four --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
@ -36,12 +36,16 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None:
assert """1 file would be updated.""" in result.output
assert result.exit_code == 1
def test_blank_lines_before_tag_two(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_before_tag/html_two.html", "--check"],
)
assert result.exit_code == 0
def test_blank_lines_before_tag_three(runner: CliRunner) -> None:
# check blocks that do not start on a newline - they should be left as is.
result = runner.invoke(
djlint,
@ -51,12 +55,15 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None:
assert """0 files would be updated.""" in result.output
assert result.exit_code == 0
def test_blank_lines_before_tag_four(runner: CliRunner) -> None:
result = runner.invoke(
djlint,
["tests/test_config/test_blank_lines_before_tag/html_four.html", "--check"],
)
assert result.exit_code == 1
print(result.output)
assert (
""" {% block this %}
-{% load i18n %}
@ -68,6 +75,8 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None:
in result.output
)
def test_blank_lines_before_tag_five(runner: CliRunner) -> None:
# something perfect should stay perfect :)
result = runner.invoke(
djlint,
@ -75,6 +84,8 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None:
)
assert result.exit_code == 0
def test_blank_lines_before_tag_six(runner: CliRunner) -> None:
# something perfect should stay perfect :)
result = runner.invoke(
djlint,
@ -82,6 +93,8 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None:
)
assert result.exit_code == 0
def test_blank_lines_before_tag_seven(runner: CliRunner) -> None:
# make sure endblock doesn't pick up endblocktrans :)
result = runner.invoke(
djlint,
@ -89,6 +102,8 @@ def test_blank_lines_before_tag(runner: CliRunner) -> None:
)
assert result.exit_code == 0
def test_blank_lines_before_tag_eight(runner: CliRunner) -> None:
# check that multiple blank lines are not added
result = runner.invoke(
djlint,