diff --git a/src/djlint/__init__.py b/src/djlint/__init__.py index 65a6050..2c2df8f 100644 --- a/src/djlint/__init__.py +++ b/src/djlint/__init__.py @@ -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 diff --git a/src/djlint/formatter/condense.py b/src/djlint/formatter/condense.py index 96be9ff..5a955a1 100644 --- a/src/djlint/formatter/condense.py +++ b/src/djlint/formatter/condense.py @@ -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.""" diff --git a/src/djlint/settings.py b/src/djlint/settings.py index 161dcf2..9438946 100644 --- a/src/djlint/settings.py +++ b/src/djlint/settings.py @@ -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", ""))