index : livejrnl-rs.git

ascending towards madness

author berrysweet <berrysweet@noreply.codeberg.org> 2023-07-06 10:54:59.0 +00:00:00
committer berrysweet <berrysweet@noreply.codeberg.org> 2023-07-06 10:54:59.0 +00:00:00
commit
5c5d5c4eb104f7bb134a24d3ac35dbc1dbb86650 [patch]
tree
42257029f99bfbb84853a097b20915b75d97a1ca
parent
bc205e82eeb6496c381f29b562439bdd339aed6c
download
5c5d5c4eb104f7bb134a24d3ac35dbc1dbb86650.tar.gz

feat: add html rendering



Diff

 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<String, String> {
	let template_path = PathBuf::from_str(&template);
@@ -17,4 +19,15 @@ pub fn get_templates(template: String) -> HashMap<String, String> {
		}
	}
	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()
}
\ No newline at end of file