mirror of
https://github.com/Hopiu/djLint.git
synced 2026-03-17 05:50:24 +00:00
fix(macro): allow html tags inside macro functions to skip formatting
closes #353
This commit is contained in:
parent
c57e9444a1
commit
bffb093c4c
4 changed files with 31 additions and 3 deletions
|
|
@ -8,7 +8,7 @@ from functools import partial
|
|||
|
||||
import regex as re
|
||||
|
||||
from ..helpers import inside_ignored_block
|
||||
from ..helpers import inside_ignored_block, inside_template_block
|
||||
from ..settings import Config
|
||||
|
||||
|
||||
|
|
@ -19,10 +19,15 @@ def expand_html(html: str, config: Config) -> str:
|
|||
"""Add whitespace.
|
||||
|
||||
Do not add whitespace if the tag is in a non indent block.
|
||||
|
||||
Do not add whiatespace if the tag is a in a template block
|
||||
"""
|
||||
if inside_ignored_block(config, html, match):
|
||||
return match.group(1)
|
||||
|
||||
if inside_template_block(config, html, match):
|
||||
return match.group(1)
|
||||
|
||||
if out_format == "\n%s" and match.start() == 0:
|
||||
return match.group(1)
|
||||
|
||||
|
|
@ -54,6 +59,7 @@ def expand_html(html: str, config: Config) -> str:
|
|||
add_right,
|
||||
html,
|
||||
)
|
||||
print(html)
|
||||
|
||||
# template tag breaks
|
||||
def should_i_move_template_tag(out_format: str, match: re.Match) -> str:
|
||||
|
|
|
|||
|
|
@ -85,6 +85,22 @@ def is_safe_closing_tag(config: Config, item: str) -> bool:
|
|||
)
|
||||
|
||||
|
||||
def inside_template_block(config: Config, html: str, match: re.Match) -> bool:
|
||||
"""Check if a re.Match is inside of a template block."""
|
||||
return any(
|
||||
ignored_match.start(0) <= match.start() and match.end(0) <= ignored_match.end()
|
||||
for ignored_match in list(
|
||||
re.finditer(
|
||||
re.compile(
|
||||
config.template_blocks,
|
||||
re.DOTALL | re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
||||
),
|
||||
html,
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool:
|
||||
"""Do not add whitespace if the tag is in a non indent block."""
|
||||
return any(
|
||||
|
|
@ -93,7 +109,7 @@ def inside_ignored_block(config: Config, html: str, match: re.Match) -> bool:
|
|||
re.finditer(
|
||||
re.compile(
|
||||
config.ignored_blocks,
|
||||
re.DOTALL | re.IGNORECASE | re.VERBOSE | re.MULTILINE | re.DOTALL,
|
||||
re.DOTALL | re.IGNORECASE | re.VERBOSE | re.MULTILINE,
|
||||
),
|
||||
html,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -577,7 +577,9 @@ class Config:
|
|||
+ r"""
|
||||
"""
|
||||
)
|
||||
|
||||
self.template_blocks: str = r"""
|
||||
{%((?!%}).)+%}
|
||||
"""
|
||||
self.ignored_blocks: str = r"""
|
||||
<(pre|textarea).*?</(\1)>
|
||||
| <(script|style).*?(?=(\</(?:\3)>))
|
||||
|
|
|
|||
|
|
@ -42,3 +42,7 @@ def test_call(runner: CliRunner, tmp_file: TextIO) -> None:
|
|||
{% endcall %}
|
||||
"""
|
||||
)
|
||||
|
||||
# tags inside template tags should not be formatted.
|
||||
output = reformat(tmp_file, runner, b"{% call (a, b) render_form(form, '<hr>'>) %}")
|
||||
assert output.exit_code == 0
|
||||
|
|
|
|||
Loading…
Reference in a new issue