From 8a3c60c72b7d96845fc55e1992ac378cf748053b Mon Sep 17 00:00:00 2001 From: Christopher Pickering Date: Wed, 7 Sep 2022 11:28:51 -0500 Subject: [PATCH] fix(cssbeautify): added support for ignore in cssbeautifier --- src/djlint/formatter/css.py | 38 +++++++++++++++---- src/djlint/formatter/js.py | 4 +- .../test_scripts_styles/ignore.html | 13 +++++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/djlint/formatter/css.py b/src/djlint/formatter/css.py index 90f8372..be6be26 100644 --- a/src/djlint/formatter/css.py +++ b/src/djlint/formatter/css.py @@ -20,13 +20,37 @@ def format_css(html: str, config: Config) -> str: inner_indent = indent + config.indent opts = BeautifierOptions(config.css_config) - beautified = ( - "\n" - + inner_indent - + ("\n" + inner_indent).join( - cssbeautifier.beautify(match.group(3), opts).splitlines() - ) - ) + beautified_lines = cssbeautifier.beautify(match.group(3), opts).splitlines() + beautified = "" + + # add indent back + ignore_indent = False + for line in beautified_lines: + + if re.search( + re.compile( + r"\/\*[ ]*?beautify[ ]+?ignore:end[ ]*?\*\/", + re.DOTALL | re.IGNORECASE | re.MULTILINE, + ), + line, + ): + line = line.lstrip() + ignore_indent = False + + if ignore_indent is False: + + beautified += "\n" + inner_indent + line + else: + beautified += "\n" + line + + if re.search( + re.compile( + r"\/\*[ ]*?beautify[ ]+?ignore:start[ ]*?\*\/", + re.DOTALL | re.IGNORECASE | re.MULTILINE, + ), + line, + ): + ignore_indent = True return match.group(1) + match.group(2) + beautified + "\n" + indent diff --git a/src/djlint/formatter/js.py b/src/djlint/formatter/js.py index ddb7fe3..b7f9e0f 100644 --- a/src/djlint/formatter/js.py +++ b/src/djlint/formatter/js.py @@ -30,7 +30,7 @@ def format_js(html: str, config: Config) -> str: if re.search( re.compile( - r"\/\*[ ]*?beautify[ ]+?preserve:end[ ]*?\*\/", + r"\/\*[ ]*?beautify[ ]+?(?:preserve|ignore):end[ ]*?\*\/", re.DOTALL | re.IGNORECASE | re.MULTILINE, ), line, @@ -46,7 +46,7 @@ def format_js(html: str, config: Config) -> str: if re.search( re.compile( - r"\/\*[ ]*?beautify[ ]+?preserve:start[ ]*?\*\/", + r"\/\*[ ]*?beautify[ ]+?(?:preserve|ignore):start[ ]*?\*\/", re.DOTALL | re.IGNORECASE | re.MULTILINE, ), line, diff --git a/tests/test_config/test_scripts_styles/ignore.html b/tests/test_config/test_scripts_styles/ignore.html index 257f222..2322c17 100644 --- a/tests/test_config/test_scripts_styles/ignore.html +++ b/tests/test_config/test_scripts_styles/ignore.html @@ -6,3 +6,16 @@ function(){} /* beautify preserve:end */ + +