Skip to content

Commit

Permalink
Fix some more bugs in the minify regexes
Browse files Browse the repository at this point in the history
  • Loading branch information
klaashoekstra94 committed Feb 12, 2024
1 parent 7f72707 commit 1a7c1ce
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/heishamon_rules_minify/minifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def minify(cls, input_text):
found_functions = cls._remove_lowercase(found_functions)

for k, v in found_variables.items():
text = re.sub(rf"(?<=\B[#$]){k}(?=[= &|<>\-+%*\/^);,])", v, text)
text = re.sub(rf"(?<=\B[#$]){k}(?=[= &|<>\-+%*\/^!);,])", v, text)

for k, v in found_functions.items():
text = re.sub(rf"{k}(?= then|\()", v, text)
Expand All @@ -49,7 +49,7 @@ def minify(cls, input_text):
text = re.sub(r"^(?:[\t ]*(?:\r?\n|\r))+", "", text, flags=re.MULTILINE)

# Remove spaces and tabs at beginning and end of lines
text = re.sub(r"^[\t| ]+|[\t| ]+$", "", text, flags=re.MULTILINE)
text = re.sub(r"^[\t ]+|[\t ]+$", "", text, flags=re.MULTILINE)

# Remove newline after line ending with 'then' or 'else'
text = re.sub(r"(?<=then|else)\r?\n", " ", text, flags=re.MULTILINE)
Expand All @@ -58,14 +58,19 @@ def minify(cls, input_text):
text = re.sub(r"(?<=[;=&|<>\-+%*\/^()])\r?\n", "", text, flags=re.MULTILINE)

# Remove extra spaces that are present after removing newlines
text = re.sub(r"(?<=[;=&|<>\-+%*\/^()]) *(?=\S)(?!then)", "", text, flags=re.MULTILINE)
text = re.sub(r"(?<=[;=&|<>\-+%*\/^()])[\t ]*(?=\S)(?!then)", "", text, flags=re.MULTILINE)

# Remove newline after line ending with 'end', except for last end of function
text = re.sub(r"(?<=end)\s+(?!\s*on |\Z)", " ", text, flags=re.MULTILINE)

# Correct spaces around operators and functions
text = re.sub(r"(?<!timer)( *(==|!=|>=|<=|\|\||&&|[=+\*\/<>^]) *)(?=-?\d|\b|[(#$@%?].+(;|then|end))", r" \g<2> ", text, flags=re.MULTILINE)
text = re.sub(r"(?<!=)(?<!= ) *- *(?=-*\d|[(#$@%?].+(;|then|end))", " - ", text, flags=re.MULTILINE)
text = re.sub(
r"(?<!timer)( *(==|!=|>=|<=|\|\||&&|[=+\*\/<>^]) *)(?=-?\d|\b|[(#$@%?].+(;|then|end))",
r" \g<2> ",
text,
flags=re.MULTILINE,
)
text = re.sub(r"(?<![=<>(,])(?<![=<>(,] ) *- *(?=-*\d|[(#$@%?].+(;|then|end))", " - ", text, flags=re.MULTILINE)
text = re.sub(r" *% *(?=-*\d|[(#$@%?].+(;|then|end))", " % ", text, flags=re.MULTILINE)

# Remove all spaces around comma signs
Expand Down

0 comments on commit 1a7c1ce

Please sign in to comment.