Skip to content

Commit 39bcceb

Browse files
match BIRD_TAG_PATTERN regex across newlines (#152)
1 parent 78a284f commit 39bcceb

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/django_bird/templates.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ def get_component_directories(
101101
]
102102

103103

104-
BIRD_TAG_PATTERN = re.compile(
105-
rf"{{%\s*{TAG}\s+(?:\"|')?([a-zA-Z0-9_.-]+)(?:\"|')?.*?%}}"
106-
)
104+
BIRD_TAG_PATTERN = re.compile(rf"{{%\s*{TAG}\s+(.*?)\s*%}}", re.DOTALL)
107105

108106

109107
def scan_file_for_bird_tag(path: Path) -> Generator[str, Any, None]:

tests/test_templates.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pytest
44
from django.test import override_settings
55

6+
from django_bird.templates import BIRD_TAG_PATTERN
67
from django_bird.templates import get_template_names
78

89

@@ -68,3 +69,28 @@ def test_get_template_names_duplicates():
6869

6970
for _, count in template_counts.items():
7071
assert count == 1
72+
73+
74+
@pytest.mark.parametrize(
75+
"template_content,expected_matches",
76+
[
77+
("{% bird button %}", ["button"]),
78+
("{% bird alert %}{% bird button %}", ["alert", "button"]),
79+
("{% bird 'button' %}", ["'button'"]),
80+
('{% bird "button" %}', ['"button"']),
81+
("{%bird button%}", ["button"]),
82+
("{% bird button %}", ["button"]),
83+
("{% endbird button %}", []),
84+
("{% birds button %}", []),
85+
("<bird:button>", []),
86+
("{% extends 'base.html' %}{% bird button %}", ["button"]),
87+
("{% bird\n'multiline'\n%}", ["'multiline'"]),
88+
('{% bird "line1\nline2" %}', ['"line1\nline2"']),
89+
("{% bird ' whitespace ' \n%}", ["' whitespace '"]),
90+
("{% bird 'mixed\"quotes'\n%}", ["'mixed\"quotes'"]),
91+
("{% bird content\n%}", ["content"]),
92+
],
93+
)
94+
def test_bird_tag_pattern(template_content, expected_matches):
95+
matches = [m.group(1) for m in BIRD_TAG_PATTERN.finditer(template_content)]
96+
assert matches == expected_matches

0 commit comments

Comments
 (0)