From: Ruben Beltran del Rio Date: Fri, 8 Mar 2024 14:09:40 +0000 (+0100) Subject: Add generator X-Git-Tag: 7.0.0~43 X-Git-Url: https://git.r.bdr.sh/rbdr/blog/commitdiff_plain/60307a9a3a39dccf652c9d9b4348e44db1e67627?hp=f6a545b00a4046879b7cc25c06c37bb6b6880b43 Add generator --- diff --git a/src/generator/html.rs b/src/generator/html.rs index cff4aea..b7ddd45 100644 --- a/src/generator/html.rs +++ b/src/generator/html.rs @@ -1,12 +1,17 @@ +use std::fs::write; use std::io::Result; use std::path::PathBuf; use crate::template::{find, parse, TemplateContext}; -pub fn generate(_: &PathBuf, template_directory: &PathBuf, _: &PathBuf, context: &TemplateContext) -> Result<()> { - match find(template_directory, "index.html") { +const FILENAME: &str = "index.html"; + +pub fn generate(_: &PathBuf, template_directory: &PathBuf, target: &PathBuf, context: &TemplateContext) -> Result<()> { + match find(template_directory, FILENAME) { Some(template) => { let parsed_template = parse(&template); - println!("TOKEN {}", parsed_template.render(context)); + let rendered_template = parsed_template.render(context); + let location = target.join(FILENAME); + write(location, rendered_template)?; }, None => {} } diff --git a/src/generator/mod.rs b/src/generator/mod.rs index f0fef75..e967e41 100644 --- a/src/generator/mod.rs +++ b/src/generator/mod.rs @@ -21,6 +21,7 @@ pub fn generate(static_directory: &PathBuf, template_directory: &PathBuf, output fn available_generators() -> Vec Result<()>> { vec![ static_files::generate, + // These three are actually the same. Can generalize, don't know how in rust yet. html::generate, rss::generate, txt::generate diff --git a/src/generator/rss.rs b/src/generator/rss.rs index 5ed9f5c..1aac73d 100644 --- a/src/generator/rss.rs +++ b/src/generator/rss.rs @@ -1,12 +1,17 @@ +use std::fs::write; use std::io::Result; use std::path::PathBuf; use crate::template::{find, parse, TemplateContext}; -pub fn generate(_: &PathBuf, template_directory: &PathBuf, _: &PathBuf, context: &TemplateContext) -> Result<()> { - match find(template_directory, "feed.xml") { +const FILENAME: &str = "feed.xml"; + +pub fn generate(_: &PathBuf, template_directory: &PathBuf, target: &PathBuf, context: &TemplateContext) -> Result<()> { + match find(template_directory, FILENAME) { Some(template) => { let parsed_template = parse(&template); - println!("TOKEN {}", parsed_template.render(context)); + let rendered_template = parsed_template.render(context); + let location = target.join(FILENAME); + write(location, rendered_template)?; }, None => {} } diff --git a/src/generator/txt.rs b/src/generator/txt.rs index ec96420..ae260fe 100644 --- a/src/generator/txt.rs +++ b/src/generator/txt.rs @@ -1,12 +1,17 @@ +use std::fs::write; use std::io::Result; use std::path::PathBuf; use crate::template::{find, parse, TemplateContext}; -pub fn generate(_: &PathBuf, template_directory: &PathBuf, _: &PathBuf, context: &TemplateContext) -> Result<()> { - match find(template_directory, "index.txt") { +const FILENAME: &str = "index.txt"; + +pub fn generate(_: &PathBuf, template_directory: &PathBuf, target: &PathBuf, context: &TemplateContext) -> Result<()> { + match find(template_directory, FILENAME) { Some(template) => { let parsed_template = parse(&template); - println!("TOKEN {}", parsed_template.render(context)); + let rendered_template = parsed_template.render(context); + let location = target.join(FILENAME); + write(location, rendered_template)?; }, None => {} }