use std::collections::HashMap;
use std::path::PathBuf;
use std::str::FromStr;
use walkdir::WalkDir;
use minijinja::{Environment, context};
use crate::data::formats::RenderConfig;
pub fn get_templates(template: String) -> HashMap<String, String> {
let template_path = PathBuf::from_str(&template);
let mut templates: HashMap<String, String> = HashMap::new();
for path in WalkDir::new(template_path.unwrap()).into_iter().filter_map(|e| e.ok()) {
if path.metadata().unwrap().is_file() {
let relative_path = PathBuf::from(path.path());
let relative_path = relative_path.strip_prefix(&template).unwrap();
let full_path = path.path().display().to_string();
if let Ok(file) = std::fs::read_to_string(&full_path) {
templates.insert(relative_path.to_str().unwrap_or_default().to_string(), file);
}
}
}
templates
}
pub fn render_html(config: RenderConfig, templates: &HashMap<String, String>) -> String {
let mut env: Environment<'_> = Environment::new();
for (name, source) in templates {
let _ = env.add_template(&name, &source).unwrap();
}
let tmpl: minijinja::Template<'_, '_> = env.get_template(&config.template_index).unwrap();
tmpl.render(context!(config => config)).unwrap_or_default()
}