feat(whitespace): add option to preserver some whitespace before text

This option preserves some leading whitespace in text blocks.

closes #260
This commit is contained in:
Christopher Pickering 2022-06-15 13:41:14 -05:00
parent fbc9db1d51
commit 15ead700d1
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
3 changed files with 15 additions and 1 deletions

View file

@ -94,6 +94,11 @@ from .src import get_src
is_flag=True,
help="Return errors as warnings.",
)
@click.option(
"--preserve-leading-space",
is_flag=True,
help="Attempt to preserve leading space on text.",
)
@colorama_text(autoreset=True)
def main(
src: List[str],
@ -108,6 +113,7 @@ def main(
lint: bool,
use_gitignore: bool,
warn: bool,
preserve_leading_space: bool,
) -> None:
"""djLint · HTML template linter and formatter."""
config = Config(
@ -123,6 +129,7 @@ def main(
check=check,
use_gitignore=use_gitignore,
warn=warn,
preserve_leading_space=preserve_leading_space,
)
temp_file = None

View file

@ -25,7 +25,8 @@ def condense_html(html: str, config: Config) -> str:
func = partial(strip_space, config, html)
html = re.sub(re.compile(r"^[ \t]*(.*?)[\n \t]*$", re.M), func, html)
if not config.preserve_leading_space:
html = re.sub(re.compile(r"^[ \t]*(.*?)[\n \t]*$", re.M), func, html)
def if_blank_line_after_match(config: Config, html: str) -> bool:
"""Check if there should be a blank line after."""

View file

@ -191,6 +191,7 @@ class Config:
lint: bool = False,
use_gitignore: bool = False,
warn: bool = False,
preserve_leading_space: bool = False,
):
self.reformat = reformat
@ -228,6 +229,11 @@ class Config:
"format_attribute_template_tags", False
)
self.preserve_leading_space: bool = (
preserve_leading_space
or djlint_settings.get("preserve_leading_space", False)
)
# ignore is based on input and also profile
self.ignore: str = str(ignore or djlint_settings.get("ignore", ""))