From 507dae9b50e35a106482b07729d402ac1528bc4b Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Tue, 16 Nov 2021 11:49:25 +0100 Subject: [PATCH] fixed #75, added test --- pyproject.toml | 3 +++ src/djlint/formatter/condense.py | 13 +++++++++++-- tests/config_blank_lines_after_tag/html_six.html | 3 +++ tests/test_config.py | 6 ++++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/config_blank_lines_after_tag/html_six.html diff --git a/pyproject.toml b/pyproject.toml index 462916c..7765a27 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -63,3 +63,6 @@ quiet = true [tool.pylint.messages_control] disable = "E1120, R0914, E0401, R0912, R0916, R0913, W0104, R0801, W1404, R0902, R0903, R1732, R0915, C0301, R1702" + +[tool.djlint] +blank_line_after_tag = "load, extends, include " diff --git a/src/djlint/formatter/condense.py b/src/djlint/formatter/condense.py index 3a12900..0e5860a 100644 --- a/src/djlint/formatter/condense.py +++ b/src/djlint/formatter/condense.py @@ -52,7 +52,14 @@ def condense_html(html: str, config: Config) -> str: return match.group() - func = partial(condense_line, config) + def add_blank_line(config: Config, html: str, match: re.Match) -> str: + """Add break after if not in ignored block.""" + if inside_ignored_block(config, html, match): + return match.group() + + return match.group() + "\n" + + func = partial(add_blank_line, config, html) # should we add blank lines after load tags? if config.blank_line_after_tag: @@ -62,10 +69,12 @@ def condense_html(html: str, config: Config) -> str: fr"((?:{{%\s*?{tag}[^}}]+?%}}\n?)+)", re.IGNORECASE | re.MULTILINE | re.DOTALL, ), - r"\1\n", + func, html, ) + func = partial(condense_line, config) + # put short single line tags on one line html = re.sub( re.compile( diff --git a/tests/config_blank_lines_after_tag/html_six.html b/tests/config_blank_lines_after_tag/html_six.html new file mode 100644 index 0000000..2b44644 --- /dev/null +++ b/tests/config_blank_lines_after_tag/html_six.html @@ -0,0 +1,3 @@ +{% block include %} + {# {% include 'common/sticky-topbar-hidden-nav.html' %}#} +{% endblock %} diff --git a/tests/test_config.py b/tests/test_config.py index d06d8e1..7167681 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -147,6 +147,12 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: ) assert result.exit_code == 0 + # something perfect should stay perfect :) + result = runner.invoke( + djlint, ["tests/config_blank_lines_after_tag/html_six.html", "--check"] + ) + assert result.exit_code == 0 + def test_profile(runner: CliRunner) -> None: result = runner.invoke(djlint, ["tests/config_profile/html.html"])