fix(cssbeautify): added support for ignore in cssbeautifier

This commit is contained in:
Christopher Pickering 2022-09-07 11:28:51 -05:00
parent a1c7376a8c
commit 8a3c60c72b
No known key found for this signature in database
GPG key ID: E14DB3B0A0FACF84
3 changed files with 46 additions and 9 deletions

View file

@ -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

View file

@ -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,

View file

@ -6,3 +6,16 @@ function(){}
/* beautify preserve:end */
</script>
<script>
/* beautify ignore:start */
function(){}
function(){}
function(){}
/* beautify ignore:end */
</script>
<style>
/* beautify ignore:start */
.class {display: none;}
/* beautify ignore:end */
</style>