fix(preserve blank lines): fixed duplicate blank lines being inserted after h-space,

Fixed extra blank lines being added to beginning of file

closes #300
This commit is contained in:
Christopher Pickering 2022-07-26 08:12:03 -05:00
parent d3a4e543e0
commit 9ed3393be0
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
4 changed files with 38 additions and 4 deletions

View file

@ -23,6 +23,9 @@ def expand_html(html: str, config: Config) -> str:
if inside_ignored_block(config, html, match):
return match.group(1)
if out_format == "\n%s" and match.start() == 0:
return match.group(1)
return out_format % match.group(1)
html_tags = config.break_html_tags
@ -45,7 +48,7 @@ def expand_html(html: str, config: Config) -> str:
# html tags - break after
html = re.sub(
re.compile(
rf"(</?(?:{html_tags})\b(\"[^\"]*\"|'[^']*'|{{[^}}]*}}|[^'\">{{}}])*>)(?=[^\n])",
rf"(</?(?:{html_tags})\b(\"[^\"]*\"|'[^']*'|{{[^}}]*}}|[^'\">{{}}])*>)(?!\s*?\n)(?=[^\n])",
flags=re.IGNORECASE | re.VERBOSE,
),
add_right,
@ -54,7 +57,7 @@ def expand_html(html: str, config: Config) -> str:
# template tag breaks
def should_i_move_template_tag(out_format: str, match: re.Match) -> str:
# ensure template tag is not inside an html tag
# ensure template tag is not inside an html tag and also not the first line of the file
if inside_ignored_block(config, html, match):
return match.group(1)
@ -68,13 +71,14 @@ def expand_html(html: str, config: Config) -> str:
html[: match.end()],
re.MULTILINE | re.VERBOSE,
):
if out_format == "\n%s" and match.start() == 0:
return match.group(1)
return out_format % match.group(1)
return match.group(1)
# template tags
# break before
html = re.sub(
re.compile(
break_char

View file

@ -0,0 +1,3 @@
{% block someblock %}{% endblock %}
<br/>
<br/>

View file

@ -0,0 +1 @@
<div>

View file

@ -5,7 +5,7 @@ run::
pytest tests/test_config/test_preserve_blank_lines/test_config.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_config/test_preserve_blank_lines/test_config.py::test_config
pytest tests/test_config/test_preserve_blank_lines/test_config.py::test_whitespace
"""
# pylint: disable=C0116
@ -27,3 +27,29 @@ def test_config(runner: CliRunner) -> None:
)
assert result.exit_code == 0
def test_whitespace(runner: CliRunner) -> None:
# blank line should not be added before template tags
result = runner.invoke(
djlint,
[
"tests/test_config/test_preserve_blank_lines/html_one.html",
"--check",
"--preserve-blank-lines",
],
)
assert result.exit_code == 0
# blank line should not be added before html tags
result = runner.invoke(
djlint,
[
"tests/test_config/test_preserve_blank_lines/html_two.html",
"--check",
"--preserve-blank-lines",
],
)
print(result.output)
assert result.exit_code == 0