Skip to content

Commit

Permalink
Add reformat_array function and fix clean_characters for better text …
Browse files Browse the repository at this point in the history
…processing
  • Loading branch information
nattadasu committed Dec 6, 2024
1 parent 99186c7 commit f73efd4
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import argparse as ap
from pathlib import Path
from regex import DOTALL, MULTILINE, match, sub, findall
from regex import DOTALL, MULTILINE, match, sub, findall, compile
from typing import Union
from random import choice

Expand Down Expand Up @@ -175,9 +175,10 @@ def clean_characters(text: str) -> str:
"""
# fmt: off
replacements = [
(r",\s*;", ","), (r"{\s*;", "{"), (r";\s*}", "}"),
(r"};\n{", "}\n{"), (r";\s*$", ""), (r"\[\s*;", "["),
(r";\s*\]", "]"), (r",\s*\]", "]"), (r"(\s)*", "\\1"),
(r",\s*;", ","), (r"{\s*;", "{"), (r";\s*}", "}"), (r"};\n{", "}\n{"),
(r";\s*$", ""), (r"\[\s*;", "["), (r";\s*\]", "]"), (r",\s+\)", ")"),
(r",\s*\]", "]"), (r"(\s)*", "\\1"), (r"->;", "->"), (r"->", "-> "),
(r"\r", ""), (r"\n", ""),
]
# fmt: on

Expand All @@ -187,28 +188,6 @@ def clean_characters(text: str) -> str:
return text


def final_stringify(text: str) -> str:
"""
Stringify the final output as a single line
Args:
text (str): Text to sanitize
Returns:
str: Sanitized text
"""

# Remove leading and trailing whitespace
text = text.strip()

# Replace newlines with spaces
text = text.replace("\r", "").replace("\n", "")
text = text.replace("->;", "->")
text = text.replace("->", "-> ")

return text


def obfuscate_variables(text: str) -> str:
"""
Obfuscate variables in the input text, replacing them with 1-3 character strings.
Expand Down Expand Up @@ -288,6 +267,18 @@ def dequoter(text: str) -> str:
text = text.replace(fx, key)
return text

def reformat_array(text: str) -> str:
"""
Remove spaces between array elements, or between hash keys and values
DO NOT remove spaces in {".+"} outside of array or hash
"""
#
text = sub(r"(?<=\S),\s(?=\S|[^\"\}])", ",", text)
text = sub(r"(?<=\S):\s(?=\S)", ":", text)
return text




def main():
args = parse_args()
Expand All @@ -301,8 +292,8 @@ def main():
script = remove_blank_lines(script)
script = array_stringify(remove_leading_whitespace(script))
script = clean_characters(script)
script = final_stringify(script)
script = obfuscate_variables(script)
# script = obfuscate_variables(script)
script = reformat_array(script)
# script = dequoter(script)

out.parent.mkdir(parents=True, exist_ok=True)
Expand Down

0 comments on commit f73efd4

Please sign in to comment.