diff --git a/docs/djlint/changelog.rst b/docs/djlint/changelog.rst index 4a592ed..4da4aeb 100644 --- a/docs/djlint/changelog.rst +++ b/docs/djlint/changelog.rst @@ -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 ----- diff --git a/src/djlint/formatter/compress_html.py b/src/djlint/formatter/compress_html.py index 7b2ef70..6027912 100644 --- a/src/djlint/formatter/compress_html.py +++ b/src/djlint/formatter/compress_html.py @@ -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 diff --git a/src/djlint/formatter/indent_html.py b/src/djlint/formatter/indent_html.py index 89fba08..9eb3385 100644 --- a/src/djlint/formatter/indent_html.py +++ b/src/djlint/formatter/indent_html.py @@ -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 diff --git a/src/djlint/helpers.py b/src/djlint/helpers.py new file mode 100644 index 0000000..91b18ca --- /dev/null +++ b/src/djlint/helpers.py @@ -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:], + ) diff --git a/tests/test_html.py b/tests/test_html.py index 3679602..1a5f76b 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_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"""