|
import re |
|
import streamlit as st |
|
from bs4 import BeautifulSoup |
|
|
|
def parse_variable_file(variable_content): |
|
"""Parses the SCSS variable content and returns a mapping of color names to their values.""" |
|
variables = {} |
|
|
|
for line in variable_content.splitlines(): |
|
|
|
match = re.match(r'^\s*\$([\w-]+)\s*:\s*([^;]+)\s*(!default)?;?', line) |
|
if match: |
|
var_name = match.group(1) |
|
var_value = match.group(2).strip() |
|
variables[var_value] = f"${var_name}" |
|
|
|
variables[f"${var_name}"] = f"${var_name}" |
|
return variables |
|
|
|
def convert_css_to_scss(css_content, variable_mapping): |
|
"""Converts CSS content to SCSS by replacing color values with variable names.""" |
|
|
|
sorted_mappings = sorted(variable_mapping.items(), key=lambda x: len(x[0]), reverse=True) |
|
|
|
|
|
converted_lines = [] |
|
for line in css_content.splitlines(): |
|
processed_line = line |
|
|
|
|
|
if '/*' in line and '*/' in line: |
|
comment_start = line.index('/*') |
|
comment_end = line.index('*/') + 2 |
|
before_comment = line[:comment_start] |
|
comment = line[comment_start:comment_end] |
|
after_comment = line[comment_end:] |
|
|
|
|
|
for value, variable in sorted_mappings: |
|
before_comment = before_comment.replace(value, variable) |
|
after_comment = after_comment.replace(value, variable) |
|
|
|
processed_line = before_comment + comment + after_comment |
|
else: |
|
|
|
for value, variable in sorted_mappings: |
|
processed_line = processed_line.replace(value, variable) |
|
|
|
converted_lines.append(processed_line) |
|
|
|
return '\n'.join(converted_lines) |
|
|
|
def extract_css_and_replace_with_variables(css_content, variables_content): |
|
"""Replaces CSS properties with SCSS variables.""" |
|
try: |
|
variable_mapping = parse_variable_file(variables_content) |
|
return convert_css_to_scss(css_content, variable_mapping) |
|
except Exception as e: |
|
st.error(f"Error processing CSS to SCSS: {e}") |
|
return None |
|
|
|
def convert_html_to_twig(html_content): |
|
"""Converts HTML to Twig format.""" |
|
try: |
|
soup = BeautifulSoup(html_content, "html.parser") |
|
|
|
for tag in soup.find_all(): |
|
|
|
if tag.string and tag.string.strip(): |
|
tag.string = f"{{{{ {tag.name}_content }}}}" |
|
|
|
|
|
for attr, value in tag.attrs.items(): |
|
tag[attr] = f"{{{{ {attr}_{tag.name} }}}}" |
|
|
|
|
|
twig_comments = "\n".join( |
|
[ |
|
f"<!-- `{{{{ {tag.name}_content }}}}`: Content for <{tag.name}> -->" |
|
for tag in soup.find_all() if tag.string |
|
] |
|
) |
|
|
|
return f"{twig_comments}\n{soup.prettify()}" |
|
except Exception as e: |
|
st.error(f"Error converting HTML to Twig: {e}") |
|
return None |
|
|
|
|
|
html_file = st.file_uploader("Upload HTML File", type=["html"], key="html_file") |
|
css_file = st.file_uploader("Upload CSS File (Optional)", type=["css"], key="css_file") |
|
variables_file = st.file_uploader("Upload SCSS Variables File", type=["scss"], key="variables_file") |
|
|
|
|
|
if html_file: |
|
html_content = html_file.read().decode("utf-8") |
|
twig_content = convert_html_to_twig(html_content) |
|
|
|
if twig_content: |
|
st.subheader("Twig Output") |
|
st.code(twig_content, language="twig") |
|
|
|
st.download_button("Download Twig File", data=twig_content, file_name="output.twig", mime="text/plain") |
|
|
|
|
|
if css_file and variables_file: |
|
css_content = css_file.read().decode("utf-8") |
|
variables_content = variables_file.read().decode("utf-8") |
|
|
|
scss_content = extract_css_and_replace_with_variables(css_content, variables_content) |
|
|
|
if scss_content: |
|
st.subheader("SCSS Output") |
|
st.code(scss_content, language="scss") |
|
|
|
st.download_button("Download SCSS File", data=scss_content, file_name="styles.scss", mime="text/plain") |
|
|