Skip to content

Commit c2ccae3

Browse files
committed
Initial work for ignoring comments.
1 parent 4a6d597 commit c2ccae3

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/dj_angles/replacers/__init__.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import re
23

34
from dj_angles.replacers.attributes import replace_attributes
45
from dj_angles.replacers.tags import replace_tags
@@ -18,6 +19,22 @@ def convert_template(html: str, *, origin=None) -> str:
1819
The converted template HTML string.
1920
"""
2021

22+
# 0. Mask comments
23+
comments = []
24+
25+
def mask_match(match):
26+
comments.append(match.group(0))
27+
return f"__DJ_ANGLES_COMMENT_{len(comments) - 1}__"
28+
29+
# Django single line comments {# ... #}
30+
html = re.sub(r"\{#.*?#\}", mask_match, html, flags=re.DOTALL)
31+
32+
# Django block comments {% comment %}...{% endcomment %}
33+
html = re.sub(r"\{%\s*comment\s*%\}.*?\{%\s*endcomment\s*%\}", mask_match, html, flags=re.DOTALL | re.IGNORECASE)
34+
35+
# Custom dj-comment Tags <dj-comment>...</dj-comment>
36+
html = re.sub(r"<dj-comment>.*?</dj-comment>", mask_match, html, flags=re.DOTALL | re.IGNORECASE)
37+
2138
# 1. Replace attributes, e.g. `<div dj-if="condition">`
2239
html = replace_attributes(html)
2340

@@ -27,4 +44,8 @@ def convert_template(html: str, *, origin=None) -> str:
2744
# 3. Replace tags, e.g. `<dj-include />`
2845
html = replace_tags(html, origin=origin)
2946

47+
# 4. Unmask comments
48+
for i, comment in enumerate(comments):
49+
html = html.replace(f"__DJ_ANGLES_COMMENT_{i}__", comment)
50+
3051
return html
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from dj_angles.replacers import convert_template
2+
3+
4+
def test_commented_out_tag_is_ignored():
5+
template = """
6+
{% comment %}
7+
<dj-include 'fake-partial.html' />
8+
{% endcomment %}
9+
"""
10+
# If ignored, it should remain as <dj-include ...> inside the comment
11+
# If processed, it will become {% include ... %}
12+
13+
# We expect it to NOT be processed if the goal is to ignore comments.
14+
# But currently we want to see what happens.
15+
16+
actual = convert_template(template)
17+
18+
# Let's see what we get. I suspect it currently converts it.
19+
print(f"\nACTUAL:\n{actual}")
20+
21+
# If we want it to be ignored, this assertion should pass:
22+
assert "<dj-include 'fake-partial.html' />" in actual
23+
24+
25+
def test_single_line_comment_ignored():
26+
template = "{# <dj-include 'fake-partial.html' /> #}"
27+
actual = convert_template(template)
28+
print(f"\nACTUAL SINGLE LINE:\n{actual}")
29+
assert "<dj-include 'fake-partial.html' />" in actual
30+
31+
32+
def test_custom_dj_comment_ignored():
33+
template = """
34+
<dj-comment>
35+
<dj-include 'fake-partial.html' />
36+
</dj-comment>
37+
"""
38+
actual = convert_template(template)
39+
print(f"\nACTUAL DJ-COMMENT:\n{actual}")
40+
assert "<dj-include 'fake-partial.html' />" in actual

0 commit comments

Comments
 (0)