Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,78 +7,69 @@ def parse_variable_file(variable_content):
|
|
| 7 |
"""Parses the SCSS variable content and returns a mapping of color names to their values."""
|
| 8 |
variables = {}
|
| 9 |
|
| 10 |
-
# Print the variables content for debugging
|
| 11 |
-
st.write("Parsing variables file...")
|
| 12 |
-
|
| 13 |
# Process each line
|
| 14 |
for line in variable_content.splitlines():
|
| 15 |
# Skip comments and empty lines
|
| 16 |
if not line.strip() or line.strip().startswith('//'):
|
| 17 |
continue
|
| 18 |
|
| 19 |
-
#
|
| 20 |
var_match = re.match(r'^\s*\$([\w-]+):\s*([^;]+)\s*(!default)?\s*;', line)
|
| 21 |
|
| 22 |
if var_match:
|
| 23 |
var_name = var_match.group(1)
|
| 24 |
var_value = var_match.group(2).strip()
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
|
|
|
| 27 |
if re.match(r'^(#[a-fA-F0-9]{3,6}|rgba?\([^)]+\))$', var_value):
|
| 28 |
-
variables[var_value] = f"${var_name}"
|
| 29 |
-
#
|
| 30 |
st.write(f"Found color variable: ${var_name} = {var_value}")
|
| 31 |
|
| 32 |
-
st.write(f"Total variables found: {len(variables)}")
|
| 33 |
return variables
|
| 34 |
|
| 35 |
def convert_css_to_scss(css_content, variable_mapping):
|
| 36 |
"""Converts CSS content to SCSS by replacing color values with variable names."""
|
| 37 |
-
# Debug output
|
| 38 |
st.write("Starting CSS to SCSS conversion...")
|
| 39 |
-
st.write("Available variable mappings:", variable_mapping)
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
result_lines = []
|
| 42 |
for line in css_content.splitlines():
|
| 43 |
-
original_line = line
|
| 44 |
processed_line = line
|
| 45 |
|
| 46 |
-
# Handle comments separately
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
for color, variable in variable_mapping.items():
|
| 59 |
-
if color in processed_part:
|
| 60 |
-
processed_part = processed_part.replace(color, variable)
|
| 61 |
-
st.write(f"Replaced {color} with {variable}")
|
| 62 |
-
processed_parts.append(processed_part)
|
| 63 |
-
|
| 64 |
-
processed_line = ''.join(processed_parts)
|
| 65 |
-
else:
|
| 66 |
-
# Process lines without comments
|
| 67 |
-
for color, variable in variable_mapping.items():
|
| 68 |
-
if color in processed_line:
|
| 69 |
-
processed_line = processed_line.replace(color, variable)
|
| 70 |
-
st.write(f"Replaced {color} with {variable}")
|
| 71 |
|
| 72 |
# Debug output if line was changed
|
| 73 |
-
if
|
| 74 |
-
st.write(f"
|
| 75 |
|
| 76 |
result_lines.append(processed_line)
|
| 77 |
|
| 78 |
return '\n'.join(result_lines)
|
| 79 |
|
| 80 |
# Streamlit app
|
| 81 |
-
st.title("SCSS Variable Converter
|
| 82 |
|
| 83 |
# File uploaders
|
| 84 |
css_file = st.file_uploader("Upload CSS File", type=["css"])
|
|
@@ -92,8 +83,11 @@ if css_file and variables_file:
|
|
| 92 |
st.write("Processing files...")
|
| 93 |
st.write("---")
|
| 94 |
|
|
|
|
| 95 |
variable_mapping = parse_variable_file(variables_content)
|
|
|
|
| 96 |
if variable_mapping:
|
|
|
|
| 97 |
scss_content = convert_css_to_scss(css_content, variable_mapping)
|
| 98 |
|
| 99 |
if scss_content:
|
|
@@ -105,6 +99,8 @@ if css_file and variables_file:
|
|
| 105 |
file_name="converted.scss",
|
| 106 |
mime="text/plain"
|
| 107 |
)
|
|
|
|
|
|
|
| 108 |
else:
|
| 109 |
st.error("No valid color variables found in the variables file.")
|
| 110 |
|
|
|
|
| 7 |
"""Parses the SCSS variable content and returns a mapping of color names to their values."""
|
| 8 |
variables = {}
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
# Process each line
|
| 11 |
for line in variable_content.splitlines():
|
| 12 |
# Skip comments and empty lines
|
| 13 |
if not line.strip() or line.strip().startswith('//'):
|
| 14 |
continue
|
| 15 |
|
| 16 |
+
# Enhanced variable pattern to catch more SCSS variable formats
|
| 17 |
var_match = re.match(r'^\s*\$([\w-]+):\s*([^;]+)\s*(!default)?\s*;', line)
|
| 18 |
|
| 19 |
if var_match:
|
| 20 |
var_name = var_match.group(1)
|
| 21 |
var_value = var_match.group(2).strip()
|
| 22 |
|
| 23 |
+
# Clean up the value (remove any extra spaces or quotes)
|
| 24 |
+
var_value = var_value.strip('"\'')
|
| 25 |
+
|
| 26 |
+
# Store the mapping in both directions for more reliable replacement
|
| 27 |
if re.match(r'^(#[a-fA-F0-9]{3,6}|rgba?\([^)]+\))$', var_value):
|
| 28 |
+
variables[var_value.upper()] = f"${var_name}" # Store uppercase version
|
| 29 |
+
variables[var_value.lower()] = f"${var_name}" # Store lowercase version
|
| 30 |
st.write(f"Found color variable: ${var_name} = {var_value}")
|
| 31 |
|
| 32 |
+
st.write(f"Total variables found: {len(variables) // 2}") # Divide by 2 because we store each color twice
|
| 33 |
return variables
|
| 34 |
|
| 35 |
def convert_css_to_scss(css_content, variable_mapping):
|
| 36 |
"""Converts CSS content to SCSS by replacing color values with variable names."""
|
|
|
|
| 37 |
st.write("Starting CSS to SCSS conversion...")
|
|
|
|
| 38 |
|
| 39 |
+
def replace_color(match):
|
| 40 |
+
"""Helper function to replace colors in regex matches"""
|
| 41 |
+
color = match.group(0)
|
| 42 |
+
# Try both upper and lower case versions of the color
|
| 43 |
+
return variable_mapping.get(color.upper(), variable_mapping.get(color.lower(), color))
|
| 44 |
+
|
| 45 |
+
# Process the content line by line
|
| 46 |
result_lines = []
|
| 47 |
for line in css_content.splitlines():
|
|
|
|
| 48 |
processed_line = line
|
| 49 |
|
| 50 |
+
# Handle comments and regular content separately
|
| 51 |
+
parts = re.split(r'(/\*.*?\*/)', processed_line)
|
| 52 |
+
|
| 53 |
+
for i in range(len(parts)):
|
| 54 |
+
if not parts[i].startswith('/*'):
|
| 55 |
+
# Replace hex colors (#fff, #ffffff)
|
| 56 |
+
parts[i] = re.sub(r'#[0-9a-fA-F]{3,6}\b', replace_color, parts[i])
|
| 57 |
+
|
| 58 |
+
# Replace rgba/rgb colors
|
| 59 |
+
parts[i] = re.sub(r'rgba?\([^)]+\)', replace_color, parts[i])
|
| 60 |
+
|
| 61 |
+
processed_line = ''.join(parts)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
# Debug output if line was changed
|
| 64 |
+
if processed_line != line:
|
| 65 |
+
st.write(f"Replaced colors in line:\nFrom: {line}\nTo: {processed_line}")
|
| 66 |
|
| 67 |
result_lines.append(processed_line)
|
| 68 |
|
| 69 |
return '\n'.join(result_lines)
|
| 70 |
|
| 71 |
# Streamlit app
|
| 72 |
+
st.title("SCSS Variable Converter")
|
| 73 |
|
| 74 |
# File uploaders
|
| 75 |
css_file = st.file_uploader("Upload CSS File", type=["css"])
|
|
|
|
| 83 |
st.write("Processing files...")
|
| 84 |
st.write("---")
|
| 85 |
|
| 86 |
+
# Create variable mapping
|
| 87 |
variable_mapping = parse_variable_file(variables_content)
|
| 88 |
+
|
| 89 |
if variable_mapping:
|
| 90 |
+
# Convert CSS to SCSS
|
| 91 |
scss_content = convert_css_to_scss(css_content, variable_mapping)
|
| 92 |
|
| 93 |
if scss_content:
|
|
|
|
| 99 |
file_name="converted.scss",
|
| 100 |
mime="text/plain"
|
| 101 |
)
|
| 102 |
+
else:
|
| 103 |
+
st.error("No content was generated after conversion.")
|
| 104 |
else:
|
| 105 |
st.error("No valid color variables found in the variables file.")
|
| 106 |
|