Update app.py
Browse files
app.py
CHANGED
@@ -7,43 +7,28 @@ from pathlib import Path
|
|
7 |
st.title("HTML to Twig and SCSS Converter")
|
8 |
|
9 |
# Functions to parse variables and convert CSS to SCSS
|
10 |
-
def parse_variable_file(
|
11 |
-
"""Parses the SCSS variable
|
12 |
variables = {}
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
variables[match.group(2)] = f"${match.group(1)}" # Color value as key, variable name as value
|
19 |
return variables
|
20 |
|
21 |
-
def convert_css_to_scss(
|
22 |
-
"""Converts CSS
|
23 |
-
with open(css_file_path, "r", encoding="utf-8") as css_file:
|
24 |
-
css_content = css_file.read()
|
25 |
-
# Replace color values with SCSS variables using the mapping
|
26 |
for color, variable in variable_mapping.items():
|
27 |
css_content = css_content.replace(color, variable)
|
28 |
-
|
29 |
-
with open(output_scss_path, "w", encoding="utf-8") as scss_file:
|
30 |
-
scss_file.write(css_content)
|
31 |
-
print(f"SCSS file generated: {output_scss_path}")
|
32 |
|
33 |
# Functions to convert HTML to Twig and CSS to SCSS
|
34 |
def extract_css_and_replace_with_variables(css_content, variables_content):
|
35 |
"""Replaces CSS properties with SCSS variables."""
|
36 |
try:
|
37 |
-
|
38 |
-
|
39 |
-
match = re.match(r"\$(\w+):\s*(.+);", line)
|
40 |
-
if match:
|
41 |
-
variables[match.group(2).strip()] = match.group(1)
|
42 |
-
|
43 |
-
for value, var_name in variables.items():
|
44 |
-
css_content = re.sub(rf"\b{re.escape(value)}\b", f"${{{var_name}}}", css_content)
|
45 |
-
|
46 |
-
return css_content
|
47 |
except Exception as e:
|
48 |
st.error(f"Error processing CSS to SCSS: {e}")
|
49 |
return None
|
@@ -80,9 +65,8 @@ html_file = st.file_uploader("Upload HTML File", type=["html"], key="html_file")
|
|
80 |
css_file = st.file_uploader("Upload CSS File (Optional)", type=["css"], key="css_file")
|
81 |
variables_file = st.file_uploader("Upload SCSS Variables File", type=["scss"], key="variables_file")
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
html_content = html_file.read().decode("utf-8")
|
87 |
twig_content = convert_html_to_twig(html_content)
|
88 |
|
@@ -92,14 +76,11 @@ if html_file and variables_file:
|
|
92 |
|
93 |
st.download_button("Download Twig File", data=twig_content, file_name="output.twig", mime="text/plain")
|
94 |
|
|
|
95 |
if css_file and variables_file:
|
96 |
css_content = css_file.read().decode("utf-8")
|
97 |
variables_content = variables_file.read().decode("utf-8")
|
98 |
|
99 |
-
variable_mapping = parse_variable_file("variables.scss") # Adjust path if needed
|
100 |
-
output_scss_path = "output.scss"
|
101 |
-
convert_css_to_scss("input.css", variable_mapping, output_scss_path)
|
102 |
-
|
103 |
scss_content = extract_css_and_replace_with_variables(css_content, variables_content)
|
104 |
|
105 |
if scss_content:
|
|
|
7 |
st.title("HTML to Twig and SCSS Converter")
|
8 |
|
9 |
# Functions to parse variables and convert CSS to SCSS
|
10 |
+
def parse_variable_file(variable_content):
|
11 |
+
"""Parses the SCSS variable content and returns a mapping of color names to their values."""
|
12 |
variables = {}
|
13 |
+
for line in variable_content.splitlines():
|
14 |
+
# Match SCSS variables (e.g., `$white: #fff !default;`)
|
15 |
+
match = re.match(r"^\s*\$([\w-]+):\s*(#[a-fA-F0-9]{3,6}|rgba?\(.+?\))", line)
|
16 |
+
if match:
|
17 |
+
variables[match.group(2)] = f"${match.group(1)}" # Color value as key, variable name as value
|
|
|
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 |
for color, variable in variable_mapping.items():
|
23 |
css_content = css_content.replace(color, variable)
|
24 |
+
return css_content
|
|
|
|
|
|
|
25 |
|
26 |
# Functions to convert HTML to Twig and CSS to SCSS
|
27 |
def extract_css_and_replace_with_variables(css_content, variables_content):
|
28 |
"""Replaces CSS properties with SCSS variables."""
|
29 |
try:
|
30 |
+
variable_mapping = parse_variable_file(variables_content)
|
31 |
+
return convert_css_to_scss(css_content, variable_mapping)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
except Exception as e:
|
33 |
st.error(f"Error processing CSS to SCSS: {e}")
|
34 |
return None
|
|
|
65 |
css_file = st.file_uploader("Upload CSS File (Optional)", type=["css"], key="css_file")
|
66 |
variables_file = st.file_uploader("Upload SCSS Variables File", type=["scss"], key="variables_file")
|
67 |
|
68 |
+
# Handle HTML to Twig conversion
|
69 |
+
if html_file:
|
|
|
70 |
html_content = html_file.read().decode("utf-8")
|
71 |
twig_content = convert_html_to_twig(html_content)
|
72 |
|
|
|
76 |
|
77 |
st.download_button("Download Twig File", data=twig_content, file_name="output.twig", mime="text/plain")
|
78 |
|
79 |
+
# Handle CSS to SCSS conversion
|
80 |
if css_file and variables_file:
|
81 |
css_content = css_file.read().decode("utf-8")
|
82 |
variables_content = variables_file.read().decode("utf-8")
|
83 |
|
|
|
|
|
|
|
|
|
84 |
scss_content = extract_css_and_replace_with_variables(css_content, variables_content)
|
85 |
|
86 |
if scss_content:
|