]> git.r.bdr.sh - rbdr/blog/blobdiff - src/generator/html.rs
Add tests for the archivers
[rbdr/blog] / src / generator / html.rs
index 0b258bf044119b2747672d3f3159416ef8705aa9..70cc3206c0fac5f3a4534ffe2f2023a2d2102a11 100644 (file)
@@ -1,18 +1,22 @@
-use std::io::Result;
-use std::path::PathBuf;
-use crate::post::Post;
-use crate::template::{find, parse};
+use crate::template::{find, parse, Context};
+use std::fs::write;
+use std::io::{Error, ErrorKind::Other, Result};
+use std::path::Path;
 
-pub fn generate(_: &PathBuf, template_directory: &PathBuf, _: &PathBuf, _: &Vec<Post>) -> Result<()> {
-    println!("READING TEMP");
-    match find(template_directory, "index.html") {
-        Some(template) => {
-            let parsed_template = parse(&template);
-            for token in parsed_template.tokens {
-                println!("TOKEN {}", token);
-            }
-        },
-        None => {}
+const FILENAME: &str = "index.html";
+
+pub fn generate(
+    _: &Path,
+    template_directory: &Path,
+    target: &Path,
+    context: &Context,
+) -> Result<()> {
+    if let Some(template) = find(template_directory, FILENAME) {
+        let parsed_template =
+            parse(&template).ok_or_else(|| Error::new(Other, "Unable to parse HTML template"))?;
+        let rendered_template = parsed_template.render(context)?;
+        let location = target.join(FILENAME);
+        write(location, rendered_template)?;
     }
     Ok(())
 }