Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# Streamlit App
|
| 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_file_path):
|
| 11 |
+
"""Parses the SCSS variable file and returns a mapping of color names to their values."""
|
| 12 |
+
variables = {}
|
| 13 |
+
with open(variable_file_path, "r", encoding="utf-8") as file:
|
| 14 |
+
for line in file:
|
| 15 |
+
# Match SCSS variables (e.g., `$white: #fff !default;`)
|
| 16 |
+
match = re.match(r"^\s*\$([\w-]+):\s*(#[a-fA-F0-9]{3,6}|rgba?\(.+?\))", line)
|
| 17 |
+
if match:
|
| 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(css_file_path, variable_mapping, output_scss_path):
|
| 22 |
+
"""Converts CSS file to SCSS by replacing color values with variable names."""
|
| 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 |
+
# Save the SCSS content to a new file
|
| 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 |
+
variables = {}
|
| 38 |
+
for line in variables_content.splitlines():
|
| 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
|
| 50 |
+
|
| 51 |
+
def convert_html_to_twig(html_content):
|
| 52 |
+
"""Converts HTML to Twig format."""
|
| 53 |
+
try:
|
| 54 |
+
soup = BeautifulSoup(html_content, "html.parser")
|
| 55 |
+
|
| 56 |
+
for tag in soup.find_all():
|
| 57 |
+
# Replace text with Twig variables
|
| 58 |
+
if tag.string and tag.string.strip():
|
| 59 |
+
tag.string = f"{{{{ {tag.name}_content }}}}" # Example: <h1>{{ h1_content }}</h1>
|
| 60 |
+
|
| 61 |
+
# Replace attributes with Twig variables
|
| 62 |
+
for attr, value in tag.attrs.items():
|
| 63 |
+
tag[attr] = f"{{{{ {attr}_{tag.name} }}}}" # Example: src="{{ src_img }}"
|
| 64 |
+
|
| 65 |
+
# Add comments for Twig variables
|
| 66 |
+
twig_comments = "\n".join(
|
| 67 |
+
[
|
| 68 |
+
f"<!-- `{{{{ {tag.name}_content }}}}`: Content for <{tag.name}> -->"
|
| 69 |
+
for tag in soup.find_all() if tag.string
|
| 70 |
+
]
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return f"{twig_comments}\n{soup.prettify()}"
|
| 74 |
+
except Exception as e:
|
| 75 |
+
st.error(f"Error converting HTML to Twig: {e}")
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
# File uploaders
|
| 79 |
+
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 |
+
if html_file and variables_file:
|
| 84 |
+
variables_content = variables_file.read().decode("utf-8")
|
| 85 |
+
|
| 86 |
+
html_content = html_file.read().decode("utf-8")
|
| 87 |
+
twig_content = convert_html_to_twig(html_content)
|
| 88 |
+
|
| 89 |
+
if twig_content:
|
| 90 |
+
st.subheader("Twig Output")
|
| 91 |
+
st.code(twig_content, language="twig")
|
| 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:
|
| 106 |
+
st.subheader("SCSS Output")
|
| 107 |
+
st.code(scss_content, language="scss")
|
| 108 |
+
|
| 109 |
+
st.download_button("Download SCSS File", data=scss_content, file_name="styles.scss", mime="text/plain")
|