-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect_and_replace.py
59 lines (49 loc) · 2.31 KB
/
detect_and_replace.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import re
import os
# Function to perform the replacements
def replace_shorthands_in_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
original_content = file.read()
# Copy the original content
content = original_content
# Define the replacements as patterns
replacements = {
r"var\(--main-color\)": "$main-color", # Detect var(--main-color)
r"var\(--hover-color\)": "$hover-color", # Detect var(--hover-color)
r"var\(--text-normal\)": "$text-normal", # Detect var(--text-normal)
r"var\(--background-shading\)": "$bg-shading", # Detect var(--background-shading)
r"cv\('colors\.main'\)": "$main-color", # Detect cv('colors.main')
r"cv\('colors\.hover'\)": "$hover-color", # Detect cv('colors.hover')
r"cv\('bg\.app\.shading'\)": "$bg-shading", # Detect cv('bg.app.shading')
r"#\{\$(main-color|hover-color|bg-shading|text-normal)\}": r"$\1" # Detect #{$variable}
}
# Apply all replacements
for pattern, replacement in replacements.items():
content = re.sub(pattern, replacement, content)
# Only write back if there are changes
if content != original_content:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
print(f"Replaced shorthand variables in {file_path}")
else:
print(f"No changes needed for {file_path}")
# Define the directory to scan for SCSS files
def replace_in_directory(directory):
# List of specific files to ignore
ignored_files = {
os.path.normpath('src/defaultSettings.scss'),
os.path.normpath('src/variables.scss'),
}
# Recursively walk through the directory to find .scss and .css files
for root, dirs, files in os.walk(directory):
for file in files:
# Process only .scss and .css files
if file.endswith(".scss") or file.endswith(".css"):
file_path = os.path.normpath(os.path.join(root, file))
# Skip ignored files
if file_path in ignored_files:
print(f"Skipping file {file_path}")
continue
replace_shorthands_in_file(file_path)
# Specify the 'src' directory for scanning
replace_in_directory('./src')