lollms-webui/scripts/code_fixes/code_semicolumns.py

53 lines
1.9 KiB
Python
Raw Normal View History

2023-09-26 06:58:02 +00:00
import os
2024-12-19 12:48:57 +00:00
import re
2023-09-26 06:58:02 +00:00
# Define a list of file extensions to process
2024-12-19 12:48:57 +00:00
file_extensions = [".js", ".vue", ".html"]
2023-09-26 06:58:02 +00:00
# Regular expressions to match lines where semicolons can be added
2024-12-19 12:48:57 +00:00
javascript_pattern = (
r"\b(?:(?<!if|else|while|for|switch|catch|return|function)\s*[^;]*$|^\s*{)"
)
vue_pattern = r"\b(?:data|computed|methods|watch|beforeCreate|created|beforeMount|mounted|beforeUpdate|updated|beforeDestroy|destroyed)\s*:[^;]*$"
html_pattern = r"<[^>]*>$"
2023-09-26 06:58:02 +00:00
# Function to add semicolons to the end of lines in a file
def add_semicolons_to_file(file_path):
try:
2024-12-19 12:48:57 +00:00
with open(file_path, "r") as file:
2023-09-26 06:58:02 +00:00
lines = file.readlines()
2024-12-19 12:48:57 +00:00
with open(file_path, "w") as file:
2023-09-26 06:58:02 +00:00
for line in lines:
2024-12-19 12:48:57 +00:00
if file_path.endswith(".js") and re.search(
javascript_pattern, line.strip()
):
line = line.rstrip() + ";"
elif file_path.endswith(".vue") and re.search(
vue_pattern, line.strip()
):
line = line.rstrip() + ";"
elif file_path.endswith(".html") and re.search(
html_pattern, line.strip()
):
line = line.rstrip() + ";"
2023-09-26 06:58:02 +00:00
file.write(line)
except FileNotFoundError:
print(f"File not found: {file_path}")
except Exception as e:
print(f"An error occurred: {str(e)}")
2024-12-19 12:48:57 +00:00
2023-09-26 06:58:02 +00:00
# Specify the path to the directory containing your JavaScript, Vue.js, or HTML files
2024-12-19 12:48:57 +00:00
directory_path = "/path/to/your/files"
2023-09-26 06:58:02 +00:00
# Iterate through files in the directory and add semicolons
for root, _, files in os.walk(directory_path):
for file_name in files:
if any(file_name.endswith(ext) for ext in file_extensions):
file_path = os.path.join(root, file_name)
add_semicolons_to_file(file_path)
print("Semicolons added successfully.")