Akshayram1 commited on
Commit
a5ce568
·
verified ·
1 Parent(s): 1afcf85

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -32
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
- # First pass: collect all variable definitions
9
- for line in variable_content.splitlines():
10
- # Match SCSS variables with more flexible pattern
11
- match = re.match(r'^\s*\$([\w-]+)\s*:\s*([^;]+)\s*(!default)?;?', line)
12
- if match:
13
- var_name = match.group(1)
14
- var_value = match.group(2).strip()
15
- variables[var_value] = f"${var_name}"
16
- # Also store the variable name itself for reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the color mappings by length (longest first) to avoid partial replacements
23
- sorted_mappings = sorted(variable_mapping.items(), key=lambda x: len(x[0]), reverse=True)
 
 
24
 
25
- # Process the content line by line
26
- converted_lines = []
27
  for line in css_content.splitlines():
28
  processed_line = line
29
 
30
- # Skip comments
31
  if '/*' in line and '*/' in line:
32
- comment_start = line.index('/*')
33
- comment_end = line.index('*/') + 2
34
- before_comment = line[:comment_start]
35
- comment = line[comment_start:comment_end]
36
- after_comment = line[comment_end:]
37
-
38
- # Process only the non-comment parts
39
- for value, variable in sorted_mappings:
40
- before_comment = before_comment.replace(value, variable)
41
- after_comment = after_comment.replace(value, variable)
42
-
43
- processed_line = before_comment + comment + after_comment
44
  else:
45
- # Process normal lines
46
- for value, variable in sorted_mappings:
47
- processed_line = processed_line.replace(value, variable)
 
48
 
49
- converted_lines.append(processed_line)
50
 
51
- return '\n'.join(converted_lines)
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
- return convert_css_to_scss(css_content, variable_mapping)
 
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