Skip to content

Commit

Permalink
Improve closing Django/Jinja block tag detection
Browse files Browse the repository at this point in the history
Closes #32
Closes #38
  • Loading branch information
JaapJoris committed Jun 3, 2021
1 parent 61e98a8 commit cb4fe68
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
14 changes: 9 additions & 5 deletions djhtml/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class DjTXT:
]

AMBIGUOUS_BLOCK_TAGS = {
# token_name: regex_if_not_block
"set": " = ",
# token_name: (regex_if_block, regex_if_not_block)
"set": (None, " = "),
"video": (" as ", None),
}

def __init__(self, source="", return_mode=None):
Expand Down Expand Up @@ -183,11 +184,14 @@ def create_token(self, raw_token, src):
return token

def _has_closing_token(self, name, raw_token, src):
if not re.search(f"{{% *end{name}.*?%}}", src):
if not re.search(f"{{% *end{name}( .*?|)%}}", src):
return False
regex = self.AMBIGUOUS_BLOCK_TAGS.get(name)
if regex and re.search(regex, raw_token):
return False
if regex:
if regex[0]:
return re.search(regex[0], raw_token)
if regex[1]:
return not re.search(regex[1], raw_token)
return True

def debug(self):
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = djhtml
version = 1.4.5
version = 1.4.6
description = Django/Jinja template indenter
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
16 changes: 16 additions & 0 deletions tests/suite/django.in
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,19 @@ Click here!
/ .\
\_,--._/
#}


<!-- video tag from django-embed-video #32 -->
{% video form.instance.video as my_video %}
{% video my_video '360 x 200' %}
{% endvideo %}


<!-- match all characters of tag #38 -->
{% component_block "instruction" border_top=True header="Login" %}
{% slot "body" %}
{% with "login --token="|add:token as content %}
{% component "copy_field" field_selector="token-field" content=content %}
{% endwith %}
{% endslot %}
{% endcomponent_block %}
16 changes: 16 additions & 0 deletions tests/suite/django.out
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,19 @@
/ .\
\_,--._/
#}


<!-- video tag from django-embed-video #32 -->
{% video form.instance.video as my_video %}
{% video my_video '360 x 200' %}
{% endvideo %}


<!-- match all characters of tag #38 -->
{% component_block "instruction" border_top=True header="Login" %}
{% slot "body" %}
{% with "login --token="|add:token as content %}
{% component "copy_field" field_selector="token-field" content=content %}
{% endwith %}
{% endslot %}
{% endcomponent_block %}

0 comments on commit cb4fe68

Please sign in to comment.