fixed #174, fixed #173, added tests

This commit is contained in:
Christopher Pickering 2022-01-24 12:03:44 -06:00
parent e3844193fc
commit 4a953801fa
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
7 changed files with 81 additions and 13 deletions

View file

@ -33,6 +33,9 @@ def indent_html(rawcode: str, config: Config) -> str:
# here using all tags cause we allow empty tags on one line
slt_template = config.optional_single_line_template_tags
# nested ignored blocks..
ignored_level = 0
for item in rawcode_flat_list:
# if a raw tag first line
if not is_block_raw and is_ignored_block_opening(config, item):
@ -41,9 +44,18 @@ def indent_html(rawcode: str, config: Config) -> str:
# if a raw tag then start ignoring
if is_ignored_block_opening(config, item):
is_block_raw = True
ignored_level += 1
if re.findall(
config.ignored_inline_blocks, item, flags=re.IGNORECASE | re.VERBOSE
if is_safe_closing_tag(config, item):
ignored_level -= 1
if is_block_raw is True and ignored_level == 0:
is_block_raw = False
if (
re.findall(
config.ignored_inline_blocks, item, flags=re.IGNORECASE | re.VERBOSE
)
and is_block_raw is False
):
tmp = (indent * indent_level) + item + "\n"
@ -84,6 +96,7 @@ def indent_html(rawcode: str, config: Config) -> str:
re.IGNORECASE | re.MULTILINE | re.VERBOSE,
)
and is_block_raw is False
and not is_safe_closing_tag(config, item)
):
indent_level = max(indent_level - 1, 0)
tmp = (indent * indent_level) + item + "\n"
@ -112,11 +125,12 @@ def indent_html(rawcode: str, config: Config) -> str:
tmp = (indent * indent_level) + item + "\n"
indent_level = indent_level + 1
elif is_raw_first_line is True or is_safe_closing_tag(config, item):
elif is_raw_first_line is True or (
is_safe_closing_tag(config, item) and is_block_raw is False
):
tmp = (indent * indent_level) + item + "\n"
elif is_block_raw is True or item.strip() == "":
tmp = item + "\n"
# otherwise, just leave same level
@ -125,7 +139,6 @@ def indent_html(rawcode: str, config: Config) -> str:
# if a opening raw tag then start ignoring.. only if there is no closing tag
# on the same line
if is_ignored_block_opening(config, item):
is_block_raw = True
is_raw_first_line = False
@ -146,7 +159,10 @@ def indent_html(rawcode: str, config: Config) -> str:
# turn off raw block if we hit end - for one line raw blocks, but not an inline raw
if is_ignored_block_closing(config, item):
is_block_raw = False
if not is_safe_closing_tag(config, item):
ignored_level -= 1
if ignored_level == 0:
is_block_raw = False
beautified_code = beautified_code + tmp

View file

@ -62,7 +62,10 @@ def is_safe_closing_tag(config: Config, item: str) -> bool:
last_index = 0
inline = list(
re.finditer(
re.compile(config.ignored_inline_blocks, flags=re.IGNORECASE | re.VERBOSE),
re.compile(
config.ignored_inline_blocks + r" | " + config.ignored_blocks,
flags=re.IGNORECASE | re.VERBOSE,
),
item,
)
)

View file

@ -98,7 +98,7 @@ def build_output(error: dict, config: Config) -> int:
line = Fore.BLUE + message_dict["line"] + Style.RESET_ALL
code = (
(Fore.RED if bool(message_dict["code"][:1] == "E") else Fore.YELLOW)
(Fore.RED if message_dict["code"][:1] == "E" else Fore.YELLOW)
+ message_dict["code"]
+ Style.RESET_ALL
)

View file

@ -495,7 +495,7 @@ class Config:
self.template_unindent: str = r"""
(?:
(?:\{\{\/)
| (?:\{%-?[ ]*?end)
| (?:\{%-?[ ]*?end(?!comment))
)
"""
@ -691,6 +691,7 @@ class Config:
self.ignored_inline_blocks: str = r"""
<!--.*?-->
| <(script|style).*?\</(?:\1)>
| {\*.*?\*}
| {\#(?!.*djlint:[ ]*?(?:off|on)\b).*\#}
| <\?php.*?\?>

View file

@ -7,7 +7,7 @@ run::
for a single test, run::
pytest tests/test_django.py::test_blocktranslate --cov=src/djlint \
pytest tests/test_django.py::test_comment --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
"""
@ -126,6 +126,50 @@ def test_comment(runner: CliRunner, tmp_file: TextIO) -> None:
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<html>
<head>
<script src="file1.js"></script>
{% comment %}
<script src="file2.js"></script>
<script src="file3.js"></script>
<script src="file4.js"></script>
{% endcomment %}
<script src="file5.js"></script>
</head>
<body>
</body>
</html>
""",
)
assert output.exit_code == 0
output = reformat(
tmp_file,
runner,
b"""<html>
<head>
<script src="file1.js"></script>
{# djlint:off #}
{% comment %}
<script src="file2.js"></script>
<script src="file3.js"></script>
<script src="file4.js"></script>
{% endcomment %}
{# djlint:on #}
<script src="file5.js"></script>
</head>
<body>
</body>
</html>
""",
)
assert output.exit_code == 0
def test_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(

View file

@ -7,7 +7,7 @@ run::
for a single test::
pytest tests/test_djlint.py::test_help --cov=src/djlint \
pytest tests/test_djlint.py::test_version --cov=src/djlint \
--cov-branch --cov-report xml:coverage.xml --cov-report term-missing
or::
@ -17,6 +17,7 @@ or::
"""
import subprocess
import sys
from importlib.metadata import version
# pylint: disable=C0116
from pathlib import Path
@ -162,7 +163,10 @@ def test_check_reformatter_no_error(runner: CliRunner, tmp_file: TextIO) -> None
def test_version(runner: CliRunner) -> None:
result = runner.invoke(djlint, ["--version"])
assert pkg_resources.get_distribution("djlint").version in result.output
print(result.output)
print(pkg_resources.get_distribution("djlint").version)
print(version("djlint"))
assert version("djlint") in result.output
def test_python_call() -> None:

View file

@ -5,7 +5,7 @@ run::
pytest tests/test_html.py --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
pytest tests/test_html.py::test_span_tag --cov=src/djlint --cov-branch \
pytest tests/test_html.py::test_script_tag --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing