leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly support
Documentation
//! Template engine for export system

use super::types::ExportError;
use std::collections::HashMap;

/// Template engine for generating export content
pub struct TemplateEngine {
    templates: HashMap<String, String>,
    variables: HashMap<String, String>,
}

impl TemplateEngine {
    /// Create a new template engine
    pub fn new() -> Self {
        Self {
            templates: HashMap::new(),
            variables: HashMap::new(),
        }
    }

    /// Add a template
    pub fn add_template(&mut self, name: &str, content: &str) {
        self.templates.insert(name.to_string(), content.to_string());
    }

    /// Set template variables
    pub fn set_variables(&mut self, variables: HashMap<String, String>) {
        self.variables = variables;
    }

    /// Render a template with variables
    pub fn render(&self, template_name: &str) -> Result<String, ExportError> {
        let template = self.templates.get(template_name)
            .ok_or_else(|| ExportError::TemplateError(format!("Template '{}' not found", template_name)))?;

        let mut result = template.clone();

        // Simple variable substitution
        for (key, value) in &self.variables {
            let placeholder = format!("{{{{{}}}}}", key);
            result = result.replace(&placeholder, value);
        }

        Ok(result)
    }

    /// Render HTML template for chart export
    pub fn render_chart_html(&self, chart_html: &str, title: Option<&str>) -> Result<String, ExportError> {
        let mut variables = self.variables.clone();
        variables.insert("chart_content".to_string(), chart_html.to_string());
        variables.insert("title".to_string(), title.unwrap_or("Chart").to_string());

        let template = r#"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{title}}</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .chart-container { margin: 20px 0; }
        .metadata { font-size: 12px; color: #666; margin-top: 20px; }
    </style>
</head>
<body>
    <h1>{{title}}</h1>
    <div class="chart-container">
        {{chart_content}}
    </div>
    <div class="metadata">
        <p>Generated by Leptos Helios</p>
    </div>
</body>
</html>
        "#;

        let mut result = template.to_string();
        for (key, value) in &variables {
            let placeholder = format!("{{{{{}}}}}", key);
            result = result.replace(&placeholder, value);
        }

        Ok(result)
    }

    /// Get default HTML template
    pub fn get_default_html_template() -> &'static str {
        r#"
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{title}}</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .chart-container { margin: 20px 0; }
    </style>
</head>
<body>
    <h1>{{title}}</h1>
    <div class="chart-container">
        {{chart_content}}
    </div>
</body>
</html>
        "#
    }
}