From 198afee13bbe31cbf83d883e8208a3ee9d0ed490 Mon Sep 17 00:00:00 2001 From: Oliver Haas Date: Sat, 30 Nov 2024 10:56:31 +0100 Subject: [PATCH 1/2] fix: template tag inside template tag --- djlint/formatter/expand.py | 4 +-- djlint/settings.py | 4 +++ tests/test_django/test_templatetag_ext.py | 33 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 tests/test_django/test_templatetag_ext.py diff --git a/djlint/formatter/expand.py b/djlint/formatter/expand.py index 5856225a3..5e6c0e5ef 100644 --- a/djlint/formatter/expand.py +++ b/djlint/formatter/expand.py @@ -112,9 +112,7 @@ def should_i_move_template_tag( # break after return re.sub( - r"((?:{%|{{\#)[ ]*?(?:" - + config.break_template_tags - + ")[^}]+?[%}]})(?=[^\n])", + rf"((?:{{%|{{{{\#)[ ]*?(?:{config.break_template_tags})(?>{config.template_tags}|[^}}])+?[%}}]}})(?=[^\n])", partial(should_i_move_template_tag, "%s\n"), html, flags=RE_FLAGS_IMX, diff --git a/djlint/settings.py b/djlint/settings.py index a006fedd9..8b1916d7d 100644 --- a/djlint/settings.py +++ b/djlint/settings.py @@ -727,6 +727,10 @@ def __init__( """ ) + self.template_tags = r""" + {{(?:(?!}}).)*}}|{%(?:(?!%}).)*%} + """ + self.html_tag_regex = r""" (!\[]+\b) # a tag name diff --git a/tests/test_django/test_templatetag_ext.py b/tests/test_django/test_templatetag_ext.py new file mode 100644 index 000000000..62002015c --- /dev/null +++ b/tests/test_django/test_templatetag_ext.py @@ -0,0 +1,33 @@ +"""Test template tag extended usage. + +uv run pytest tests/test_django/test_templatetag_ext.py +""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import pytest + +from djlint.reformat import formatter +from tests.conftest import config_builder, printer + +if TYPE_CHECKING: + from typing import Any + +test_data = [ + pytest.param( + ('{% component "img" src="{% url \'my_image\' %}" %}'), + ('{% component "img" src="{% url \'my_image\' %}" %}\n'), + ({"custom_blocks": "component"}), + id="template_tag_inside_template_tag", + ) +] + + +@pytest.mark.parametrize(("source", "expected", "args"), test_data) +def test_base(source: str, expected: str, args: dict[str, Any]) -> None: + output = formatter(config_builder(args), source) + + printer(expected, source, output) + assert expected == output From 0fe8550ffdbced967be61cbdad90682006225350 Mon Sep 17 00:00:00 2001 From: Oliver Haas Date: Thu, 5 Dec 2024 09:54:19 +0100 Subject: [PATCH 2/2] chore: formatting --- djlint/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/djlint/settings.py b/djlint/settings.py index f38f02c5c..fbf0e116f 100644 --- a/djlint/settings.py +++ b/djlint/settings.py @@ -728,7 +728,7 @@ def __init__( ) self.template_tags = r""" - {{(?:(?!}}).)*}}|{%(?:(?!%}).)*%} + {{(?:(?!}}).)*}}|{%(?:(?!%}).)*%} """ self.html_tag_attribute_regex = rf"""