Skip to content

Commit

Permalink
Merge pull request #35 from sjoerdjob/issue-33-jinja-set
Browse files Browse the repository at this point in the history
Correctly deal with Jinja "set" tag.
  • Loading branch information
JaapJoris authored Jun 3, 2021
2 parents f0d8db0 + 3de9e2d commit 61e98a8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
15 changes: 14 additions & 1 deletion djhtml/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class DjTXT:
"plural",
]

AMBIGUOUS_BLOCK_TAGS = {
# token_name: regex_if_not_block
"set": " = ",
}

def __init__(self, source="", return_mode=None):
self.source = source
self.return_mode = return_mode or self
Expand Down Expand Up @@ -157,7 +162,7 @@ def create_token(self, raw_token, src):
if name == "comment":
token = Token.Open(raw_token, kind)
self.next_mode = Comment(r"\{% *endcomment.*?%\}", self, kind)
elif re.search(f"{{% *end{name}.*?%}}", src):
elif self._has_closing_token(name, raw_token, src):
token = Token.Open(raw_token, kind)
elif name in self.DJANGO_OPENING_AND_CLOSING_TAGS:
token = Token.OpenAndClose(raw_token, kind)
Expand All @@ -177,6 +182,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):
return False
regex = self.AMBIGUOUS_BLOCK_TAGS.get(name)
if regex and re.search(regex, raw_token):
return False
return True

def debug(self):
self.tokenize()
return "\n".join(
Expand Down
8 changes: 8 additions & 0 deletions tests/suite/jinja_assignment.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
{% set "five" = 5 %}
Five is {{ five }}
{% set blockdata %}
Contents
{% endset %}
Contents of block is block
</div>
8 changes: 8 additions & 0 deletions tests/suite/jinja_assignment.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>
{% set "five" = 5 %}
Five is {{ five }}
{% set blockdata %}
Contents
{% endset %}
Contents of block is block
</div>

0 comments on commit 61e98a8

Please sign in to comment.