updated indentation to indent start of ignored blocks

This commit is contained in:
Christopher Pickering 2021-09-29 14:44:39 +02:00
parent b00e51488a
commit 29ae9cfda4
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
3 changed files with 22 additions and 2 deletions

View file

@ -20,8 +20,13 @@ def _strip_html_whitespace(html: str, config: Config) -> str:
rawcode_flat = ""
is_block_ignored = False
is_group_ignored = False
lstrip = False
for item in html.strip().splitlines():
# we can left strip ignored blocks on the opening tag.
lstrip = not is_group_ignored and bool(
re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE)
)
# start of ignored block. If we are already in an ingored block, keep true.
is_group_ignored = is_group_ignored or bool(
@ -52,6 +57,9 @@ def _strip_html_whitespace(html: str, config: Config) -> str:
tmp = _clean_line(item)
is_block_ignored = True
elif lstrip:
tmp = item.lstrip()
elif is_group_ignored or is_block_ignored:
tmp = item
@ -63,6 +71,7 @@ def _strip_html_whitespace(html: str, config: Config) -> str:
re.search(config.ignored_group_closing, item, re.IGNORECASE | re.VERBOSE)
):
is_group_ignored = False
lstrip = False
rawcode_flat = rawcode_flat + tmp + "\n"

View file

@ -16,6 +16,7 @@ def indent_html(rawcode: str, config: Config) -> str:
beautified_code = ""
indent_level = 0
is_raw_first_line = False
is_block_raw = False
slt_html = config.break_html_tags
@ -27,6 +28,12 @@ def indent_html(rawcode: str, config: Config) -> str:
slt_template = config.single_line_template_tags
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
):
is_raw_first_line = True
# if a raw tag then start ignoring
if re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE):
is_block_raw = True
@ -97,6 +104,9 @@ 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:
tmp = (indent * indent_level) + item + "\n"
elif is_block_raw is True:
tmp = item + "\n"
@ -122,6 +132,7 @@ def indent_html(rawcode: str, config: Config) -> str:
re.search(config.ignored_group_opening, item, re.IGNORECASE | re.VERBOSE)
) or re.search(config.ignored_block_opening, item, re.IGNORECASE | re.VERBOSE):
is_block_raw = True
is_raw_first_line = False
# if a normal tag, we can try to expand attributes
elif (

View file

@ -27,7 +27,7 @@ def test_textarea_tag(runner: CliRunner, tmp_file: TextIO) -> None:
assert (
Path(tmp_file.name).read_text()
== """<div>
<textarea>
<textarea>
asdf
asdf</textarea>
</div>
@ -64,7 +64,7 @@ def test_html_comments_tag(runner: CliRunner, tmp_file: TextIO) -> None:
Path(tmp_file.name).read_text()
== """<div>
<!-- asdf-->
<!--
<!--
multi
line
comment-->