use crate::generator::posts::{HtmlContent, Post};
use crate::generator::util::output_rendered_template;
use crate::generator::util::templates::filters;
use crate::generator::util::templates::TemplateCommon;
use askama::Template;
use std::path::PathBuf;

pub fn generate(posts: &[Post<HtmlContent>]) -> Result<(), std::io::Error> {
    for post in posts.iter() {
        output_rendered_template(&ArticleTemplate { post }, output_path(post))?;
    }
    Ok(())
}

fn output_path(post: &Post<HtmlContent>) -> PathBuf {
    let mut output = PathBuf::from(post.permalink());
    output.push("index.html");
    output
}

#[derive(Template)]
#[template(path = "layout/article.html")]
struct ArticleTemplate<'a> {
    post: &'a Post<HtmlContent>,
}

impl<'a> TemplateCommon for ArticleTemplate<'a> {}

impl<'a> ArticleTemplate<'a> {
    fn permalink(&self) -> String {
        self.post.permalink()
    }
}
