fix regex for finding single line tempalte tags, fix #56

This commit is contained in:
Christopher Pickering 2021-10-05 10:19:28 +02:00
parent c4e3a07c45
commit f38383a629
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
5 changed files with 38 additions and 3 deletions

View file

@ -79,6 +79,7 @@ def _strip_html_whitespace(html: str, config: Config) -> str:
def compress_html(html: str, config: Config) -> str:
"""Compress back tags that do not need to be expanded."""
# put empty tags on one line
html = _strip_html_whitespace(html, config)
html = re.sub(
@ -91,7 +92,7 @@ def compress_html(html: str, config: Config) -> str:
# put empty template tags on one line
html = re.sub(
re.compile(
rf"({{%-?[ ]*?({config.start_template_tags})[^}}]+?-?%}})\s+?(\{{\%-?[ ]end[^}}]*?\%\}})",
rf"({{%-?[ ]*?({config.start_template_tags})[^}}]+?-?%}})\s+?(\{{\%-?[ ]end\2[^}}]*?\%\}})",
flags=re.MULTILINE | re.IGNORECASE | re.VERBOSE,
),
r"\1\3",

View file

@ -166,7 +166,7 @@ def indent_html(rawcode: str, config: Config) -> str:
for tag in [x.strip() for x in config.blank_line_after_tag.split(",")]:
beautified_code = re.sub(
re.compile(
fr"((?:^{{%\s*?{tag}[^}}]+?%}}\n?)+)",
fr"((?:^\s*?{{%\s*?{tag}[^}}]+?%}}\n?)+)",
re.IGNORECASE | re.MULTILINE | re.DOTALL,
),
r"\1\n",

View file

@ -0,0 +1,3 @@
{% block this %}
{% load i18n %}
{% endblock this %}

View file

@ -126,6 +126,21 @@ def test_blank_lines_after_tag(runner: CliRunner) -> None:
assert """0 files would be updated.""" in result.output
assert result.exit_code == 0
result = runner.invoke(
djlint, ["tests/config_blank_lines_after_tag/html_four.html", "--check"]
)
assert result.exit_code == 1
assert (
""" {% block this %}
-{% load i18n %}
+ {% load i18n %}
+
{% endblock this %}
"""
in result.output
)
def test_profile(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["tests/config_profile/html.html"])

View file

@ -7,7 +7,7 @@ run::
for a single test, run::
pytest tests/test_django.py::test_inline_comment --cov=src/djlint \
pytest tests/test_django.py::test_load_tag --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
@ -314,3 +314,19 @@ def test_complex_attributes(runner: CliRunner, tmp_file: TextIO) -> None:
{% ifchanged comment.stream_id %} comments-msg {% else %} comments-newMsgReply {% endifchanged %}>
"""
)
def test_load_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% block content %}{% load i18n %}{% endblock %}""",
)
assert output["exit_code"] == 1
assert (
output["text"]
== r"""{% block content %}
{% load i18n %}
{% endblock %}
"""
)