use std::collections::HashMap; use std::path::Path; use std::path::PathBuf; use std::str::FromStr; use walkdir::WalkDir; use minijinja::{Environment, context}; use crate::data::formats; use crate::util::journal; use super::filters; use super::functions; pub fn get_templates(template: String) -> HashMap { let template_path: PathBuf = PathBuf::from_str(&template).unwrap(); let mut templates: HashMap = HashMap::new(); for path in WalkDir::new(template_path).into_iter().filter_map(|e| e.ok()) { if path.metadata().unwrap().is_file() { let relative_path: PathBuf = PathBuf::from(path.path()); let relative_path: &Path = relative_path.strip_prefix(&template).unwrap(); let full_path: String = 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: formats::RenderConfig, journal: journal::Journal, templates: &HashMap) -> String { let env: &mut Environment<'_> = &mut Environment::new(); for (name, source) in templates { let _ = env.add_template(&name, &source).unwrap(); } filters::register_filters(env); functions::register_functions(env); let tmpl: minijinja::Template<'_, '_> = env.get_template(&config.template_index).unwrap(); tmpl.render(context!(config => config, journal => journal)).unwrap_or_default() }