From 250ed023fe677a3e0425540c400b4e55f4cce3fe Mon Sep 17 00:00:00 2001 From: luozhiya Date: Sun, 29 Sep 2024 00:28:43 +0800 Subject: [PATCH] Refine --- lua/fittencode/template/refine.py | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lua/fittencode/template/refine.py diff --git a/lua/fittencode/template/refine.py b/lua/fittencode/template/refine.py new file mode 100644 index 00000000..6380617f --- /dev/null +++ b/lua/fittencode/template/refine.py @@ -0,0 +1,39 @@ +import os +import re +from pathlib import Path # Import Path from pathlib module for modern path manipulations. + +# Automatically determine the directory where the script is located +current_script_dir = Path(__file__).parent + +# Define the regex pattern to match and the replacement logic +pattern = re.compile(r'<\|\s*([a-zA-Z0-9_-]+)\s*\|>') + + +def replace_in_file(file_path): + """Function to replace content inside the <| |> tags in a file""" + with open(file_path, 'r', encoding='utf-8') as file: + content = file.read() + + # Perform the replacement + new_content = re.sub(pattern, r'<|\1|>', content) + + # If there's a change, write it back to the file + if new_content != content: + print(f"Updating file: {file_path}") + with open(file_path, 'w', encoding='utf-8') as file: + file.write(new_content) + else: + print(f"No changes in file: {file_path}") + + +def batch_replace_in_directory(directory): + """Recursively find and replace content in files in the specified directory""" + for foldername, subfolders, filenames in os.walk(directory): + for filename in filenames: + if filename.endswith('.lua'): # You can modify this to match different file types + file_path = os.path.join(foldername, filename) + replace_in_file(file_path) + + +# Run the batch replacement in the current script's directory +batch_replace_in_directory(current_script_dir) \ No newline at end of file