Skip to content

Commit

Permalink
fix(formatter): allow djlint:off to exluce the rest of a file
Browse files Browse the repository at this point in the history
closes #649
  • Loading branch information
christopherpickering committed May 18, 2023
1 parent 8ea5fd2 commit 17faf5f
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/djlint/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def is_ignored_block_opening(config: Config, item: str) -> bool:
last_index = 0
inline = list(
re.finditer(
config.ignored_blocks,
config.ignored_blocks_inline,
item,
flags=re.IGNORECASE | re.VERBOSE | re.MULTILINE | re.DOTALL,
)
Expand Down
6 changes: 3 additions & 3 deletions src/djlint/rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
message: Img tag should have height and width attributes.
flags: re.DOTALL|re.I
patterns:
- <img\s(?:(?!(?:height)=).)*/?>
- <img\s(?:(?!(?:width)=).)*/?>
- <img\b(?:(?!(?:height)=).)*/?>
- <img\b(?:(?!(?:width)=).)*/?>
- rule:
name: H007
message: <!DOCTYPE ... > should be present before the html tag.
Expand Down Expand Up @@ -104,7 +104,7 @@
message: Img tag should have an alt attribute.
flags: re.DOTALL|re.I
patterns:
- <img\s(?:(?!(?:alt)=).)*/?>
- <img\b(?:(?!(?:alt)=).)*/?>
- rule:
name: H014
message: Found extra blank lines.
Expand Down
21 changes: 21 additions & 0 deletions src/djlint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,27 @@ def __init__(
# html comment
| <!--\s*djlint\:off\s*-->.*?(?=<!--\s*djlint\:on\s*-->)
# django/jinja/nunjucks
| {\#\s*djlint\:\s*off\s*\#}(?:(?!{\#\s*djlint\:\s*on\s*\#}).)*
| {%\s*comment\s*%\}\s*djlint\:off\s*\{%\s*endcomment\s*%\}(?:(?!{%\s*comment\s*%\}\s*djlint\:on\s*\{%\s*endcomment\s*%\}).)*
# inline jinja comments
| {\#(?!\s*djlint\:\s*(?:off|on)).*?\#}
# handlebars
| {{!--\s*djlint\:off\s*--}}(?:(?!{{!--\s*djlint\:on\s*--}}).)*
# golang
| {{-?\s*/\*\s*djlint\:off\s*\*/\s*-?}}(?:(?!{{-?\s*/\*\s*djlint\:on\s*\*/\s*-?}}).)*
| <!--.*?-->
| <\?php.*?\?>
| {%[ ]*?blocktranslate\b(?:(?!%}|\btrimmed\b).)*?%}.*?{%[ ]*?endblocktranslate[ ]*?%}
| {%[ ]*?blocktrans\b(?:(?!%}|\btrimmed\b).)*?%}.*?{%[ ]*?endblocktrans[ ]*?%}
| {%[ ]*?comment\b(?:(?!%}).)*?%}(?:(?!djlint:(?:off|on)).)*?(?={%[ ]*?endcomment[ ]*?%})
| ^---[\s\S]+?---
"""
self.ignored_blocks_inline: str = r"""
<(pre|textarea).*?</(\1)>
| <(script|style).*?(?=(\</(?:\3)>))
# html comment
| <!--\s*djlint\:off\s*-->.*?(?=<!--\s*djlint\:on\s*-->)
# django/jinja/nunjucks
| {\#\s*djlint\:\s*off\s*\#}.*?(?={\#\s*djlint\:\s*on\s*\#})
| {%\s*comment\s*%\}\s*djlint\:off\s*\{%\s*endcomment\s*%\}.*?(?={%\s*comment\s*%\}\s*djlint\:on\s*\{%\s*endcomment\s*%\})
# inline jinja comments
Expand Down
52 changes: 52 additions & 0 deletions tests/test_html/test_ignored.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,58 @@
),
id="ignored_3",
),
pytest.param(
(
"{% comment %}djlint:off{% endcomment %}<div><img><p></p></div>{% comment %}djlint:on{% endcomment %}<div><img></div>{% comment %}djlint:off{% endcomment %}<div><img><p></p></div>"
),
(
"{% comment %}djlint:off{% endcomment %}<div><img><p></p></div>{% comment %}djlint:on{% endcomment %}\n"
"<div>\n"
" <img>\n"
"</div>\n"
"{% comment %}djlint:off{% endcomment %}<div><img><p></p></div>\n"
),
id="{% comment don't require an on block",
),
pytest.param(
(
"{# djlint: off #}<div><img><p></p></div>{# djlint: on #}<div><img></div>{# djlint: off #}<div><img><p></p></div>"
),
(
"{# djlint: off #}<div><img><p></p></div>{# djlint: on #}\n"
"<div>\n"
" <img>\n"
"</div>\n"
"{# djlint: off #}<div><img><p></p></div>\n"
),
id="{# don't require an on block",
),
pytest.param(
(
"{{!-- djlint:off--}}<div><img><p></p></div>{{!-- djlint:on--}}<div><img></div>{{!-- djlint:off--}}<div><img><p></p></div>"
),
(
"{{!-- djlint:off--}}<div><img><p></p></div>{{!-- djlint:on--}}\n"
"<div>\n"
" <img>\n"
"</div>\n"
"{{!-- djlint:off--}}<div><img><p></p></div>\n"
),
id="{{!-- don't require an on block",
),
pytest.param(
(
"{{ /* djlint:off */ }}<div><img><p></p></div>{{ /* djlint:on */ }}<div><img></div>{{ /* djlint:off */ }}<div><img><p></p></div>"
),
(
"{{ /* djlint:off */ }}<div><img><p></p></div>{{ /* djlint:on */ }}\n"
"<div>\n"
" <img>\n"
"</div>\n"
"{{ /* djlint:off */ }}<div><img><p></p></div>\n"
),
id="{{ /* don't require an on block",
),
pytest.param(
("<script>\n" " <div><p><span></span></p></div>\n" "</script>\n"),
("<script>\n" " <div><p><span></span></p></div>\n" "</script>\n"),
Expand Down
20 changes: 20 additions & 0 deletions tests/test_linter/test_h006.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@
([]),
id="partial ignored",
),
pytest.param(
("<img>"),
(
[
{
"code": "H006",
"line": "1:0",
"match": "<img>",
"message": "Img tag should have height and width attributes.",
},
{
"code": "H013",
"line": "1:0",
"match": "<img>",
"message": "Img tag should have an alt attribute.",
},
]
),
id="test empty",
),
]


Expand Down

0 comments on commit 17faf5f

Please sign in to comment.