fixed indentation on ignored blocks with a child ignore block. closes #54

This commit is contained in:
Christopher Pickering 2021-10-05 09:47:40 +02:00
parent 7c6df42dbb
commit e72e4619bd
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
5 changed files with 220 additions and 36 deletions

View file

@ -6,6 +6,9 @@ Next Release
- Split ``alt`` requirement from H006 to H013
- Added optional custom rules file
- Added ``golang`` as profile option
- Fixed formatting of ignored blocks containing inline comments
- Added default text to ``--indent`` and ``-e`` options
- Update url rules to accept #
0.5.1
-----

View file

@ -2,6 +2,12 @@
import regex as re
from ..helpers import (
is_ignored_block_closing,
is_ignored_block_opening,
is_ignored_group_closing,
is_ignored_group_opening,
)
from ..settings import Config
@ -30,7 +36,7 @@ def _strip_html_whitespace(html: str, config: Config) -> str:
# start of ignored block. If we are already in an ingored block, keep true.
is_group_ignored = is_group_ignored or bool(
re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE)
is_ignored_group_opening(config, item)
)
# find ignored blocks and retain indentation, otherwise strip white space
@ -41,18 +47,12 @@ def _strip_html_whitespace(html: str, config: Config) -> str:
):
tmp = _clean_line(item)
elif (
re.search(config.ignored_block_closing, item, re.IGNORECASE | re.VERBOSE)
and is_group_ignored is False
):
elif is_ignored_block_closing(config, item) and is_group_ignored is False:
# do not format ignored lines
tmp = _clean_line(item)
is_block_ignored = False
elif (
re.search(config.ignored_block_opening, item, re.IGNORECASE | re.VERBOSE)
and is_group_ignored is False
):
elif is_ignored_block_opening(config, item) and is_group_ignored is False:
# do not format ignored lines
tmp = _clean_line(item)
is_block_ignored = True
@ -67,9 +67,7 @@ def _strip_html_whitespace(html: str, config: Config) -> str:
tmp = _clean_line(item)
# end of ignore raw code
if bool(
re.search(config.ignored_group_closing, item, re.IGNORECASE | re.VERBOSE)
):
if is_ignored_group_closing(config, item):
is_group_ignored = False
lstrip = False

View file

@ -4,6 +4,11 @@ from functools import partial
import regex as re
from ..helpers import (
is_ignored_group_block_closing,
is_ignored_group_block_opening,
is_ignored_group_opening,
)
from ..settings import Config
from .attributes import format_attributes
@ -29,13 +34,11 @@ def indent_html(rawcode: str, config: Config) -> str:
for item in rawcode_flat_list:
# if a raw tag first line
if not is_block_raw and re.search(
config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE
):
if not is_block_raw and is_ignored_group_opening(config, item):
is_raw_first_line = True
# if a raw tag then start ignoring
if re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE):
if is_ignored_group_opening(config, item):
is_block_raw = True
if re.findall(
@ -130,9 +133,7 @@ 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 (
re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE)
) or re.search(config.ignored_block_opening, item, re.IGNORECASE | re.VERBOSE):
if is_ignored_group_block_opening(config, item):
is_block_raw = True
is_raw_first_line = False
@ -154,19 +155,8 @@ def indent_html(rawcode: str, config: Config) -> str:
re.VERBOSE,
)
# turn off raw block if we hit end - for one line raw blocks
if (
re.search(
re.compile(
config.ignored_group_closing, flags=re.VERBOSE | re.IGNORECASE
),
item,
)
) or re.search(
re.compile(config.ignored_block_closing, flags=re.IGNORECASE | re.VERBOSE),
item,
):
# turn off raw block if we hit end - for one line raw blocks, but not an inline raw
if is_ignored_group_block_closing(config, item):
is_block_raw = False
beautified_code = beautified_code + tmp

164
src/djlint/helpers.py Normal file
View file

@ -0,0 +1,164 @@
import regex as re
from .settings import Config
def is_ignored_group_opening(config: Config, item: str) -> bool:
"""Find ignored group opening.
A valid ignored group opening tag will not be part of a
single line block.
"""
last_index = 0
inline = list(
re.finditer(
config.ignored_inline_blocks, item, flags=re.IGNORECASE | re.VERBOSE
)
)
if inline:
last_index = inline[
-1
].end() # get the last index. The ignored opening should start after this.
return bool(
re.search(
re.compile(config.ignored_group_opening, re.IGNORECASE | re.VERBOSE),
item[last_index:],
)
)
def is_ignored_block_opening(config: Config, item: str) -> bool:
"""Find ignored group opening.
A valid ignored group opening tag will not be part of a
single line block.
"""
last_index = 0
inline = list(
re.finditer(
config.ignored_inline_blocks, item, flags=re.IGNORECASE | re.VERBOSE
)
)
if inline:
last_index = inline[
-1
].end() # get the last index. The ignored opening should start after this.
return bool(
re.search(
re.compile(config.ignored_block_opening, re.IGNORECASE | re.VERBOSE),
item[last_index:],
)
)
def is_ignored_group_block_opening(config: Config, item: str) -> bool:
"""Find ignored group opening.
A valid ignored group opening tag will not be part of a
single line block.
"""
last_index = 0
inline = list(
re.finditer(
config.ignored_inline_blocks, item, flags=re.IGNORECASE | re.VERBOSE
)
)
if inline:
last_index = inline[
-1
].end() # get the last index. The ignored opening should start after this.
return (
re.search(
re.compile(config.ignored_group_opening, re.IGNORECASE | re.VERBOSE),
item[last_index:],
)
) or re.search(
re.compile(config.ignored_block_opening, re.IGNORECASE | re.VERBOSE),
item[last_index:],
)
def is_ignored_group_closing(config: Config, item: str) -> bool:
"""Find ignored group opening.
A valid ignored group opening tag will not be part of a
single line block.
"""
last_index = 0
inline = list(
re.finditer(
re.compile(config.ignored_inline_blocks, flags=re.IGNORECASE | re.VERBOSE),
item,
)
)
if inline:
last_index = inline[
-1
].end() # get the last index. The ignored opening should start after this.
return re.search(
re.compile(config.ignored_group_closing, flags=re.VERBOSE | re.IGNORECASE),
item[last_index:],
)
def is_ignored_group_block_closing(config: Config, item: str) -> bool:
"""Find ignored group opening.
A valid ignored group opening tag will not be part of a
single line block.
"""
last_index = 0
inline = list(
re.finditer(
re.compile(config.ignored_inline_blocks, flags=re.IGNORECASE | re.VERBOSE),
item,
)
)
if inline:
last_index = inline[
-1
].end() # get the last index. The ignored opening should start after this.
return (
re.search(
re.compile(config.ignored_group_closing, flags=re.VERBOSE | re.IGNORECASE),
item[last_index:],
)
) or re.search(
re.compile(config.ignored_block_closing, flags=re.IGNORECASE | re.VERBOSE),
item[last_index:],
)
def is_ignored_block_closing(config: Config, item: str) -> bool:
"""Find ignored group opening.
A valid ignored group opening tag will not be part of a
single line block.
"""
last_index = 0
inline = list(
re.finditer(
re.compile(config.ignored_inline_blocks, flags=re.IGNORECASE | re.VERBOSE),
item,
)
)
if inline:
last_index = inline[
-1
].end() # get the last index. The ignored opening should start after this.
return re.search(
re.compile(config.ignored_block_closing, flags=re.IGNORECASE | re.VERBOSE),
item[last_index:],
)

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_ignored_block --cov=src/djlint --cov-branch \
pytest tests/test_html.py::test_html_comments_tag --cov=src/djlint --cov-branch \
--cov-report xml:coverage.xml --cov-report term-missing
@ -57,14 +57,14 @@ console.log();
def test_html_comments_tag(runner: CliRunner, tmp_file: TextIO) -> None:
write_to_file(
tmp_file.name,
b"""<div>\n <!-- asdf-->\n\n <!--\n multi\nline\ncomment--></div>""",
b"""<div>\n <!-- asdf--><!--\n multi\nline\ncomment--></div>""",
)
runner.invoke(djlint, [tmp_file.name, "--reformat"])
print(Path(tmp_file.name).read_text())
assert (
Path(tmp_file.name).read_text()
== """<div>
<!-- asdf-->
<!--
<!-- asdf--><!--
multi
line
comment-->
@ -282,3 +282,32 @@ def test_ignored_block(runner: CliRunner, tmp_file: TextIO) -> None:
</script>
"""
)
def test_style_tag(runner: CliRunner, tmp_file: TextIO) -> None:
output = reformat(
tmp_file,
runner,
b"""<style>
{# override to fix text all over the place in media upload box #}
.k-dropzone .k-upload-status {
color: #a1a1a1;
}
</style>
""",
)
assert output["exit_code"] == 0
output = reformat(
tmp_file,
runner,
b"""<style>
.k-dropzone .k-upload-status {
color: #a1a1a1;
}
</style>
""",
)
assert output["exit_code"] == 0