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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -12
app.py CHANGED
@@ -2,27 +2,54 @@ import re
2
  import streamlit as st
3
  from bs4 import BeautifulSoup
4
 
5
- # Streamlit App
6
- st.title("HTML to Twig and SCSS Converter")
7
-
8
- # Functions to parse variables and convert CSS to SCSS
9
  def parse_variable_file(variable_content):
10
  """Parses the SCSS variable content and returns a mapping of color names to their values."""
11
  variables = {}
 
12
  for line in variable_content.splitlines():
13
- # Match SCSS variables (e.g., `$white: #fff !default;`)
14
- match = re.match(r"^\s*\$([\w-]+):\s*(#[a-fA-F0-9]{3,6}|rgba?\(.+?\))", line)
15
  if match:
16
- variables[match.group(2)] = f"${match.group(1)}" # Color value as key, variable name as value
 
 
 
 
17
  return variables
18
 
19
  def convert_css_to_scss(css_content, variable_mapping):
20
  """Converts CSS content to SCSS by replacing color values with variable names."""
21
- for color, variable in variable_mapping.items():
22
- css_content = css_content.replace(color, variable)
23
- return css_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Functions to convert HTML to Twig and CSS to SCSS
26
  def extract_css_and_replace_with_variables(css_content, variables_content):
27
  """Replaces CSS properties with SCSS variables."""
28
  try:
@@ -31,7 +58,7 @@ def extract_css_and_replace_with_variables(css_content, variables_content):
31
  except Exception as e:
32
  st.error(f"Error processing CSS to SCSS: {e}")
33
  return None
34
-
35
  def convert_html_to_twig(html_content):
36
  """Converts HTML to Twig format."""
37
  try:
 
2
  import streamlit as st
3
  from bs4 import BeautifulSoup
4
 
 
 
 
 
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:
 
58
  except Exception as e:
59
  st.error(f"Error processing CSS to SCSS: {e}")
60
  return None
61
+
62
  def convert_html_to_twig(html_content):
63
  """Converts HTML to Twig format."""
64
  try: