#!/usr/bin/env python3 """ EasySmartInventory Generator Genereert standalone HTML inventarisatieformulieren op basis van YAML configuratie Gebruik: python generator.py [--output ] [--theme ] """ import argparse import json import sys from pathlib import Path from html import escape from yaml_parser import parse_yaml, validate_config, InventoryConfig, FieldConfig from templates import get_theme_css, JAVASCRIPT def generate_field_html(field: FieldConfig) -> str: """Genereer HTML voor een veld""" required_attr = 'data-required="true"' if field.required else '' required_mark = '*' if field.required else '' min_length_attr = f'data-min-length="{field.min_length}"' if field.min_length else '' placeholder_attr = f'placeholder="{escape(field.placeholder)}"' if field.placeholder else '' html = f'
\n' html += f' \n' if field.type == "text": html += f' \n' html += f'
\n' elif field.type == "number": min_attr = f'min="{field.min}"' if field.min is not None else '' max_attr = f'max="{field.max}"' if field.max is not None else '' html += f' \n' html += f'
\n' elif field.type == "date": html += f' \n' html += f'
\n' elif field.type == "textarea": html += f' \n' html += f'
\n' elif field.type == "dropdown": html += f' \n' html += f'
\n' elif field.type == "multiselect": html += f'
\n' for option in field.options: safe_value = escape(option) html += f' \n' html += f'
\n' elif field.type == "boolean": html += f'
\n' html += f' \n' html += f' Ja\n' html += f'
\n' elif field.type == "photo": html += f'
\n' html += f' \n' html += f' \n' html += f'
📷 Klik om foto te maken of uploaden
\n' html += f' \n' html += f'
\n' html += '
\n' return html def generate_html(config: InventoryConfig) -> str: """Genereer complete standalone HTML""" # Config voor JavaScript js_config = { "name": config.name, "version": config.version, "autosave": { "enabled": config.autosave.enabled, "interval_seconds": config.autosave.interval_seconds, "use_url_hash": config.autosave.use_url_hash, "use_localstorage": config.autosave.use_localstorage, }, "export": { "csv": { "enabled": config.export.csv.enabled, "include_photo": config.export.csv.include_photo, }, "mailto": { "enabled": config.export.mailto.enabled, "to": config.export.mailto.to, "subject_prefix": config.export.mailto.subject_prefix, "subject_fields": config.export.mailto.subject_fields, "include_timestamp": config.export.mailto.include_timestamp, }, }, } # CSS genereren css = get_theme_css(config.style.theme, config.style) # Extra CSS voor photo container css += """ .photo-container.has-photo .photo-placeholder { display: none; } .photo-container.has-photo .photo-preview { display: block !important; } .photo-container.has-photo .photo-buttons { display: flex !important; } """ # JavaScript met config js = JAVASCRIPT.replace("{CONFIG_JSON}", json.dumps(js_config, ensure_ascii=False)) # Secties genereren sections_html = "" for section in config.sections: sections_html += f'
\n' sections_html += f'

{escape(section.name)}

\n' if section.description: sections_html += f'

{escape(section.description)}

\n' for field in section.fields: sections_html += generate_field_html(field) sections_html += '
\n' # Logo HTML logo_html = "" if config.style.logo: if config.style.logo.startswith("data:") or config.style.logo.startswith("http"): logo_html = f'' else: logo_html = f'' # Actions HTML actions_html = '
\n' if config.export.csv.enabled: actions_html += ' \n' if config.export.mailto.enabled: actions_html += ' \n' actions_html += ' \n' actions_html += '
\n' # Complete HTML html = f''' {escape(config.name)}
{logo_html}

{escape(config.name)}

Versie {config.version}
{sections_html} {actions_html}
Gereed EasySmartInventory v{config.version}
''' return html def main(): parser = argparse.ArgumentParser( description="Genereer standalone HTML inventarisatieformulieren" ) parser.add_argument("config", help="Pad naar YAML configuratiebestand") parser.add_argument("-o", "--output", help="Output HTML bestand (default: .html)") parser.add_argument("-t", "--theme", choices=["modern", "corporate", "minimal"], help="Override theme van YAML config") args = parser.parse_args() # Parse YAML config_path = Path(args.config) if not config_path.exists(): print(f"Error: Configuratiebestand niet gevonden: {config_path}") sys.exit(1) print(f"📖 Lezen configuratie: {config_path}") config = parse_yaml(str(config_path)) # Valideer errors = validate_config(config) if errors: print("❌ Validatie errors:") for error in errors: print(f" - {error}") sys.exit(1) # Override theme indien opgegeven if args.theme: config.style.theme = args.theme # Output pad bepalen if args.output: output_path = Path(args.output) else: output_path = config_path.parent / f"{config.name}.html" # Genereer HTML print(f"⚙️ Genereren HTML met theme '{config.style.theme}'...") html = generate_html(config) # Schrijf bestand output_path.write_text(html, encoding="utf-8") print(f"✅ HTML gegenereerd: {output_path}") print(f" Grootte: {len(html):,} bytes") print(f" Secties: {len(config.sections)}") total_fields = sum(len(s.fields) for s in config.sections) print(f" Velden: {total_fields}") if __name__ == "__main__": main()