mirror of
https://github.com/Hopiu/djLint.git
synced 2026-05-15 08:33:11 +00:00
fix regex for finding single line tempalte tags, fix #56
This commit is contained in:
parent
c4e3a07c45
commit
f38383a629
5 changed files with 38 additions and 3 deletions
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
3
tests/config_blank_lines_after_tag/html_four.html
Normal file
3
tests/config_blank_lines_after_tag/html_four.html
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
{% block this %}
|
||||
{% load i18n %}
|
||||
{% endblock this %}
|
||||
|
|
@ -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"])
|
||||
|
|
|
|||
|
|
@ -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 %}
|
||||
"""
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue