use super::types::ExportError;
use std::collections::HashMap;
pub struct TemplateEngine {
templates: HashMap<String, String>,
variables: HashMap<String, String>,
}
impl TemplateEngine {
pub fn new() -> Self {
Self {
templates: HashMap::new(),
variables: HashMap::new(),
}
}
pub fn add_template(&mut self, name: &str, content: &str) {
self.templates.insert(name.to_string(), content.to_string());
}
pub fn set_variables(&mut self, variables: HashMap<String, String>) {
self.variables = 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();
for (key, value) in &self.variables {
let placeholder = format!("{{{{{}}}}}", key);
result = result.replace(&placeholder, value);
}
Ok(result)
}
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)
}
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>
"#
}
}