#!/usr/bin/env python3 import os import subprocess import time from pathlib import Path from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.firefox.options import Options as FirefoxOptions from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import json def check_browser_availability(): """Check which browser is available""" browsers = [] # Check for Chrome/Chromium for browser in ['google-chrome', 'chromium', 'chromium-browser']: try: result = subprocess.run([browser, '--version'], capture_output=True, text=True) if result.returncode == 0: browsers.append(('chrome', browser)) break except: continue # Check for Firefox try: result = subprocess.run(['firefox', '--version'], capture_output=True, text=True) if result.returncode == 0: browsers.append(('firefox', 'firefox')) except: pass return browsers def capture_with_selenium(browser_type, browser_path, html_file, output_file): """Capture screenshot using Selenium""" if browser_type == 'chrome': options = Options() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--window-size=1200,900') try: driver = webdriver.Chrome(options=options) except: # Try with chromium options.binary_location = browser_path driver = webdriver.Chrome(options=options) else: # firefox options = FirefoxOptions() options.add_argument('--headless') options.add_argument('--width=1200') options.add_argument('--height=900') driver = webdriver.Firefox(options=options) try: # Load the HTML file driver.get(f"file://{html_file}") # Wait for page to load time.sleep(2) # Crop to content area (400x300) driver.set_window_size(400, 300) # Take screenshot driver.save_screenshot(str(output_file)) finally: driver.quit() def capture_with_headless_browser(html_file, output_file): """Capture screenshot using headless browser directly""" browsers = check_browser_availability() if not browsers: return False for browser_type, browser_path in browsers: try: if browser_type == 'chrome': cmd = [ browser_path, '--headless', '--disable-gpu', '--window-size=400,300', '--screenshot=' + str(output_file), '--default-background-color=0', f'file://{html_file}' ] else: # firefox cmd = [ browser_path, '--headless', '--window-size=400,300', '--screenshot=' + str(output_file), f'file://{html_file}' ] result = subprocess.run(cmd, capture_output=True, text=True, timeout=10) if result.returncode == 0 and output_file.exists(): return True except Exception as e: print(f"Error with {browser_type}: {e}") continue return False def generate_mini_html(theme_dir, temp_html): """Generate a mini HTML file that showcases the theme""" theme_name = theme_dir.name # Read the theme's example.html to get color info example_html = theme_dir / "example.html" if not example_html.exists(): return False # Create a mini version that fits in 400x300 mini_html = f"""
This is a preview of the theme's typography and color scheme.
Clean design with interactive links and elegant styling.
theme = "{theme_name}"