From 5c5d5c4eb104f7bb134a24d3ac35dbc1dbb86650 Mon Sep 17 00:00:00 2001 From: berrysweet Date: Thu, 6 Jul 2023 12:54:59 +0200 Subject: [PATCH] feat: add html rendering --- Cargo.toml | 3 ++- src/main.rs | 4 +++- src/util/templating.rs | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c918f03..b62dfda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2021" confy = "0.5.1" serde = { version = "1.0.164", features = ["derive"] } walkdir = "2.3.3" -read_pipe = "0.1.2" \ No newline at end of file +read_pipe = "0.1.2" +minijinja = "1.0.3" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5a889c7..1203baf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,9 +19,11 @@ fn main() { Ok(config_data) => { println!("{:#?}", config_data); - let template_files = util::templating::get_templates(config_data.template); + let template_files = util::templating::get_templates(config_data.template.clone()); println!("{:?}", template_files); + let html = util::templating::render_html(config_data, &template_files); + println!("{}", html); }, Err(error) => println!("{:#?}", error) }; diff --git a/src/util/templating.rs b/src/util/templating.rs index 28242cb..88bd4b1 100644 --- a/src/util/templating.rs +++ b/src/util/templating.rs @@ -2,6 +2,8 @@ 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 { let template_path = PathBuf::from_str(&template); @@ -17,4 +19,15 @@ pub fn get_templates(template: String) -> HashMap { } } templates +} + +pub fn render_html(config: RenderConfig, templates: &HashMap) -> 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() } \ No newline at end of file -- libgit2 1.7.2