11import logging
2+ import re
23
34from dj_angles .replacers .attributes import replace_attributes
45from 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
0 commit comments