fix(twig): fixed indent issue with comments. Added test

closes #290
This commit is contained in:
Christopher Pickering 2022-07-15 08:24:43 -05:00
parent f38328c829
commit 47ef81f96b
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
5 changed files with 46 additions and 5 deletions

View file

@ -12,7 +12,11 @@ def is_ignored_block_opening(config: Config, item: str) -> bool:
"""
last_index = 0
inline = list(
re.finditer(config.ignored_blocks, item, flags=re.IGNORECASE | re.VERBOSE)
re.finditer(
config.ignored_blocks,
item,
flags=re.IGNORECASE | re.VERBOSE | re.MULTILINE | re.DOTALL,
)
)
if inline:
@ -64,7 +68,7 @@ def is_safe_closing_tag(config: Config, item: str) -> bool:
re.finditer(
re.compile(
config.ignored_inline_blocks + r" | " + config.ignored_blocks,
flags=re.IGNORECASE | re.VERBOSE,
flags=re.IGNORECASE | re.VERBOSE | re.MULTILINE | re.DOTALL,
),
item,
)
@ -88,7 +92,8 @@ def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool:
for ignored_match in list(
re.finditer(
re.compile(
config.ignored_blocks, re.DOTALL | re.IGNORECASE | re.VERBOSE
config.ignored_blocks,
re.DOTALL | re.IGNORECASE | re.VERBOSE | re.MULTILINE | re.DOTALL,
),
html,
)

View file

@ -18,8 +18,11 @@ def reformat_file(config: Config, this_file: Path) -> dict:
rawcode = this_file.read_text(encoding="utf8")
compressed = compress_html(rawcode, config)
expanded = expand_html(compressed, config)
condensed = condense_html(expanded, config)
indented = indent_html(condensed, config)
beautified_code = indented

View file

@ -341,7 +341,8 @@ class Config:
| <\?php
| <script
| <!--
| [^\{]{\#
| [^\{]{\#(?!\s*djlint\:\s*on)
| ^{\#(?!\s*djlint\:\s*on)
| <pre
| <textarea
| {%[ ]*?blocktrans(?:late)?[^(?:%})]*?%}
@ -568,7 +569,7 @@ class Config:
| {\#\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)).*?\#}
| {\#(?!\s*djlint\:\s*(?:off|on)).*?\#}
# handlebars
| {{!--\s*djlint\:off\s*--}}.*?(?={{!--\s*djlint\:on\s*--}})
# golang

View file

View file

@ -0,0 +1,32 @@
"""Djlint tests specific to twig.
run::
pytest tests/test_twig/test_comments.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_twig/test_comments.py::test_nested --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
"""
# pylint: disable=C0116
from typing import TextIO
from click.testing import CliRunner
from tests.conftest import reformat
def test_macro(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""{% if %}
{#
line
#}
{% endif %}
""",
)
assert output.exit_code == 0