Skip to content

Commit

Permalink
Refine
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Sep 28, 2024
1 parent da5049d commit 250ed02
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lua/fittencode/template/refine.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 250ed02

Please sign in to comment.