From 1c7f1d654d469f61173f323d6c68b4ff1122fce0 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Mon, 11 Oct 2021 11:59:19 +0300 Subject: [PATCH] fixed #94, added tests --- src/djlint/formatter/compress_html.py | 12 ++++++++++++ src/djlint/formatter/indent_html.py | 18 +----------------- src/djlint/lint.py | 2 +- .../html_five.html | 4 ++++ .../html_three.html | 2 +- tests/test_config.py | 9 +++++++-- 6 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 tests/config_blank_lines_after_tag/html_five.html diff --git a/src/djlint/formatter/compress_html.py b/src/djlint/formatter/compress_html.py index f8b7baa..5979ce3 100644 --- a/src/djlint/formatter/compress_html.py +++ b/src/djlint/formatter/compress_html.py @@ -43,4 +43,16 @@ def compress_html(html: str, config: Config) -> str: html, ) + # should we add blank lines after load tags? + if config.blank_line_after_tag: + for tag in [x.strip() for x in config.blank_line_after_tag.split(",")]: + html = re.sub( + re.compile( + fr"((?:{{%\s*?{tag}[^}}]+?%}}\n?)+)", + re.IGNORECASE | re.MULTILINE | re.DOTALL, + ), + r"\1\n", + html, + ) + return html diff --git a/src/djlint/formatter/indent_html.py b/src/djlint/formatter/indent_html.py index 8098741..a4e48b5 100644 --- a/src/djlint/formatter/indent_html.py +++ b/src/djlint/formatter/indent_html.py @@ -110,13 +110,9 @@ def indent_html(rawcode: str, config: Config) -> str: elif is_raw_first_line is True: tmp = (indent * indent_level) + item + "\n" - elif is_block_raw is True: + elif is_block_raw is True or item.strip() == "": tmp = item + "\n" - # if just a blank line - elif item.strip() == "": - tmp = item.strip() - # otherwise, just leave same level else: tmp = (indent * indent_level) + item + "\n" @@ -160,16 +156,4 @@ def indent_html(rawcode: str, config: Config) -> str: beautified_code = beautified_code + tmp - # should we add blank lines after load tags? - if config.blank_line_after_tag: - for tag in [x.strip() for x in config.blank_line_after_tag.split(",")]: - beautified_code = re.sub( - re.compile( - fr"((?:{{%\s*?{tag}[^}}]+?%}}\n?)+)", - re.IGNORECASE | re.MULTILINE | re.DOTALL, - ), - r"\1\n", - beautified_code, - ) - return beautified_code.strip() + "\n" diff --git a/src/djlint/lint.py b/src/djlint/lint.py index 19b95a4..fb90539 100644 --- a/src/djlint/lint.py +++ b/src/djlint/lint.py @@ -61,7 +61,7 @@ def lint_file(config: Config, this_file: Path) -> Dict: # rule H025 is a special case where the output must be an even num. if rule["name"] == "H025": - open_tags = [] + open_tags: List[re.Match] = [] for match in re.finditer( re.compile( diff --git a/tests/config_blank_lines_after_tag/html_five.html b/tests/config_blank_lines_after_tag/html_five.html new file mode 100644 index 0000000..036ee35 --- /dev/null +++ b/tests/config_blank_lines_after_tag/html_five.html @@ -0,0 +1,4 @@ + + {% include "common/pdfs/partials/display_quality_improvements.html" %} + + diff --git a/tests/config_blank_lines_after_tag/html_three.html b/tests/config_blank_lines_after_tag/html_three.html index a85633d..83be9a8 100644 --- a/tests/config_blank_lines_after_tag/html_three.html +++ b/tests/config_blank_lines_after_tag/html_three.html @@ -1,6 +1,6 @@
{% include "pages/task/details_source.html.j2" %} -
+
diff --git a/tests/test_config.py b/tests/test_config.py index 451fde8..430355f 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -7,7 +7,7 @@ run:: for a single test, run:: - pytest tests/test_config.py::test_indent --cov=src/djlint \ + pytest tests/test_config.py::test_blank_lines_after_tag --cov=src/djlint \ --cov-branch --cov-report xml:coverage.xml --cov-report term-missing """ @@ -91,7 +91,6 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: result = runner.invoke( djlint, ["tests/config_blank_lines_after_tag/html.html", "--check"] ) - assert ( """+{% extends "nothing.html" %} + @@ -141,6 +140,12 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None: in result.output ) + # something perfect should stay perfect :) + result = runner.invoke( + djlint, ["tests/config_blank_lines_after_tag/html_five.html", "--check"] + ) + assert result.exit_code == 0 + def test_profile(runner: CliRunner) -> None: result = runner.invoke(djlint, ["tests/config_profile/html.html"])