From 4a953801fa7fb450ca588645226871f236601437 Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Mon, 24 Jan 2022 12:03:44 -0600 Subject: [PATCH] fixed #174, fixed #173, added tests --- src/djlint/formatter/indent.py | 28 ++++++++++++++++----- src/djlint/helpers.py | 5 +++- src/djlint/output.py | 2 +- src/djlint/settings.py | 3 ++- tests/test_django.py | 46 +++++++++++++++++++++++++++++++++- tests/test_djlint.py | 8 ++++-- tests/test_html.py | 2 +- 7 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/djlint/formatter/indent.py b/src/djlint/formatter/indent.py index cafe965..26135e8 100644 --- a/src/djlint/formatter/indent.py +++ b/src/djlint/formatter/indent.py @@ -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 diff --git a/src/djlint/helpers.py b/src/djlint/helpers.py index ca4555d..99162e8 100644 --- a/src/djlint/helpers.py +++ b/src/djlint/helpers.py @@ -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, ) ) diff --git a/src/djlint/output.py b/src/djlint/output.py index 2e5247f..ea5c091 100644 --- a/src/djlint/output.py +++ b/src/djlint/output.py @@ -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 ) diff --git a/src/djlint/settings.py b/src/djlint/settings.py index c2cff24..72eacfb 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -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).*?\ | {\*.*?\*} | {\#(?!.*djlint:[ ]*?(?:off|on)\b).*\#} | <\?php.*?\?> diff --git a/tests/test_django.py b/tests/test_django.py index b372f93..390fb3c 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -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""" + + + {% comment %} + + + + {% endcomment %} + + + + + +""", + ) + + assert output.exit_code == 0 + + output = reformat( + tmp_file, + runner, + b""" + + + {# djlint:off #} + {% comment %} + + + + {% endcomment %} + {# djlint:on #} + + + + + +""", + ) + + assert output.exit_code == 0 + def test_inline_comment(runner: CliRunner, tmp_file: TextIO) -> None: output = reformat( diff --git a/tests/test_djlint.py b/tests/test_djlint.py index 8b74c90..3f24b66 100644 --- a/tests/test_djlint.py +++ b/tests/test_djlint.py @@ -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: diff --git a/tests/test_html.py b/tests/test_html.py index d5ad02b..d83b279 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -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