Skip to content

Commit

Permalink
Support Jinja multi-line comment syntax
Browse files Browse the repository at this point in the history
Previously, `{# ... #}` comments were only recognized if they were opened
and closed on the same line. This commit allows them to span multiple lines.
As with the other comment tags, anything inside is ignored.

Thanks to @notpushkin for pointing this out!

Ref. #22
  • Loading branch information
JaapJoris committed May 30, 2021
1 parent a18a3f8 commit f0d8db0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
17 changes: 11 additions & 6 deletions djhtml/modes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class DjTXT:
r"\n",
r"{%.*?%}",
r"{#.*?#}",
r"{#",
]

DJANGO_OPENING_AND_CLOSING_TAGS = [
Expand Down Expand Up @@ -151,7 +152,6 @@ def create_token(self, raw_token, src):
token = Token.Text(raw_token)

tag = re.match(r"{% *(\w+).*?%}", raw_token)
comment = None if tag else re.match(r"{# *(\w+:\w+).*?#}", raw_token)
if tag:
name = tag.group(1)
if name == "comment":
Expand All @@ -163,12 +163,17 @@ def create_token(self, raw_token, src):
token = Token.OpenAndClose(raw_token, kind)
elif name.startswith("end"):
token = Token.Close(raw_token, kind)
return token

elif comment:
name = comment.group(1)
if name == "fmt:off":
token = Token.Open(raw_token, kind)
self.next_mode = Comment(r"\{# *fmt:on.*?#\}", self, kind)
comment = re.match(r"{# *(\w+:\w+).*?#}", raw_token)
if comment and comment[1] == "fmt:off":
token = Token.Open(raw_token, kind)
self.next_mode = Comment(r"\{# *fmt:on.*?#\}", self, kind)
return token

if raw_token == "{#":
token = Token.Open(raw_token, kind)
self.next_mode = Comment(r"#\}", self, kind)

return token

Expand Down
8 changes: 8 additions & 0 deletions tests/suite/django.in
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ There are {{ count }} items.
<a{% if %} class=""{% endif %}>
Click here!
</a>


<!-- Multiline Jinja comment -->
{#
,-._|\
/ .\
\_,--._/
#}
8 changes: 8 additions & 0 deletions tests/suite/django.out
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@
<a{% if %} class=""{% endif %}>
Click here!
</a>


<!-- Multiline Jinja comment -->
{#
,-._|\
/ .\
\_,--._/
#}

0 comments on commit f0d8db0

Please sign in to comment.