import re import streamlit as st from bs4 import BeautifulSoup from pathlib import Path # Streamlit App st.title("HTML to Twig and SCSS Converter") # Functions to parse variables and convert CSS to SCSS 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 SCSS variables (e.g., `$white: #fff !default;`) match = re.match(r"^\s*\$([\w-]+):\s*(#[a-fA-F0-9]{3,6}|rgba?\(.+?\))", line) if match: variables[match.group(2)] = f"${match.group(1)}" # Color value as key, variable name as value return variables def convert_css_to_scss(css_content, variable_mapping): """Converts CSS content to SCSS by replacing color values with variable names.""" for color, variable in variable_mapping.items(): css_content = css_content.replace(color, variable) return css_content # Functions to convert HTML to Twig and CSS to SCSS 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(): # Replace text with Twig variables if tag.string and tag.string.strip(): tag.string = f"{{{{ {tag.name}_content }}}}" # Example: