Update app.py
Browse files
app.py
CHANGED
@@ -5,56 +5,89 @@ from bs4 import BeautifulSoup
|
|
5 |
def parse_variable_file(variable_content):
|
6 |
"""Parses the SCSS variable content and returns a mapping of color names to their values."""
|
7 |
variables = {}
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
variables[f"${var_name}"] = f"${var_name}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
return variables
|
19 |
|
20 |
def convert_css_to_scss(css_content, variable_mapping):
|
21 |
"""Converts CSS content to SCSS by replacing color values with variable names."""
|
22 |
-
# Sort
|
23 |
-
sorted_mappings = sorted(variable_mapping.items(),
|
|
|
|
|
24 |
|
25 |
-
# Process
|
26 |
-
|
27 |
for line in css_content.splitlines():
|
28 |
processed_line = line
|
29 |
|
30 |
-
#
|
31 |
if '/*' in line and '*/' in line:
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
after_comment = after_comment.replace(value, variable)
|
42 |
-
|
43 |
-
processed_line = before_comment + comment + after_comment
|
44 |
else:
|
45 |
-
# Process
|
46 |
-
for
|
47 |
-
|
|
|
48 |
|
49 |
-
|
50 |
|
51 |
-
return '\n'.join(
|
52 |
|
53 |
def extract_css_and_replace_with_variables(css_content, variables_content):
|
54 |
"""Replaces CSS properties with SCSS variables."""
|
55 |
try:
|
56 |
variable_mapping = parse_variable_file(variables_content)
|
57 |
-
|
|
|
58 |
except Exception as e:
|
59 |
st.error(f"Error processing CSS to SCSS: {e}")
|
60 |
return None
|
|
|
5 |
def parse_variable_file(variable_content):
|
6 |
"""Parses the SCSS variable content and returns a mapping of color names to their values."""
|
7 |
variables = {}
|
8 |
+
variable_references = {}
|
9 |
+
|
10 |
+
# First pass: collect direct color definitions
|
11 |
+
# Match both with and without !default, including rgba values
|
12 |
+
color_pattern = re.compile(r'^\s*\$([\w-]+):\s*(#[a-fA-F0-9]{3,6}|rgba?\([^)]+\))\s*(!default)?\s*;')
|
13 |
+
# Match variable references
|
14 |
+
reference_pattern = re.compile(r'^\s*\$([\w-]+):\s*\$([\w-]+)\s*(!default)?\s*;')
|
15 |
+
|
16 |
+
# Process each line
|
17 |
+
lines = variable_content.splitlines()
|
18 |
+
for line in lines:
|
19 |
+
# Skip comments and empty lines
|
20 |
+
if line.strip().startswith('//') or not line.strip():
|
21 |
+
continue
|
22 |
+
|
23 |
+
# Skip map-merge blocks
|
24 |
+
if 'map-merge' in line:
|
25 |
+
continue
|
26 |
+
|
27 |
+
# Try to match direct color values
|
28 |
+
color_match = color_pattern.match(line)
|
29 |
+
if color_match:
|
30 |
+
var_name = color_match.group(1)
|
31 |
+
color_value = color_match.group(2)
|
32 |
+
variables[color_value] = f"${var_name}"
|
33 |
+
# Store the variable name itself as a key for reference
|
34 |
variables[f"${var_name}"] = f"${var_name}"
|
35 |
+
continue
|
36 |
+
|
37 |
+
# Try to match variable references
|
38 |
+
ref_match = reference_pattern.match(line)
|
39 |
+
if ref_match:
|
40 |
+
var_name = ref_match.group(1)
|
41 |
+
referenced_var = ref_match.group(2)
|
42 |
+
variable_references[var_name] = referenced_var
|
43 |
+
|
44 |
+
# Second pass: resolve variable references
|
45 |
+
for var_name, referenced_var in variable_references.items():
|
46 |
+
for color, original_var in variables.items():
|
47 |
+
if f"${referenced_var}" == original_var:
|
48 |
+
variables[f"${var_name}"] = original_var
|
49 |
+
|
50 |
return variables
|
51 |
|
52 |
def convert_css_to_scss(css_content, variable_mapping):
|
53 |
"""Converts CSS content to SCSS by replacing color values with variable names."""
|
54 |
+
# Sort mappings by length of the color value (longest first)
|
55 |
+
sorted_mappings = sorted(variable_mapping.items(),
|
56 |
+
key=lambda x: len(x[0]) if not x[0].startswith('$') else 0,
|
57 |
+
reverse=True)
|
58 |
|
59 |
+
# Process each line while preserving comments
|
60 |
+
result_lines = []
|
61 |
for line in css_content.splitlines():
|
62 |
processed_line = line
|
63 |
|
64 |
+
# Handle lines with comments
|
65 |
if '/*' in line and '*/' in line:
|
66 |
+
parts = re.split(r'(/\*.*?\*/)', line)
|
67 |
+
for i, part in enumerate(parts):
|
68 |
+
if not part.startswith('/*'):
|
69 |
+
# Only process non-comment parts
|
70 |
+
for color, variable in sorted_mappings:
|
71 |
+
if not color.startswith('$'): # Only replace actual color values
|
72 |
+
part = part.replace(color, variable)
|
73 |
+
parts[i] = part
|
74 |
+
processed_line = ''.join(parts)
|
|
|
|
|
|
|
75 |
else:
|
76 |
+
# Process lines without comments
|
77 |
+
for color, variable in sorted_mappings:
|
78 |
+
if not color.startswith('$'): # Only replace actual color values
|
79 |
+
processed_line = processed_line.replace(color, variable)
|
80 |
|
81 |
+
result_lines.append(processed_line)
|
82 |
|
83 |
+
return '\n'.join(result_lines)
|
84 |
|
85 |
def extract_css_and_replace_with_variables(css_content, variables_content):
|
86 |
"""Replaces CSS properties with SCSS variables."""
|
87 |
try:
|
88 |
variable_mapping = parse_variable_file(variables_content)
|
89 |
+
scss_content = convert_css_to_scss(css_content, variable_mapping)
|
90 |
+
return scss_content
|
91 |
except Exception as e:
|
92 |
st.error(f"Error processing CSS to SCSS: {e}")
|
93 |
return None
|