add cargo

This commit is contained in:
2025-06-04 23:53:05 +09:00
parent e191cb376c
commit 02dd69840d
16 changed files with 1473 additions and 0 deletions

35
src/template.rs Normal file
View File

@ -0,0 +1,35 @@
use anyhow::Result;
use tera::{Tera, Context};
use std::path::PathBuf;
use crate::config::Config;
use crate::generator::Post;
pub struct TemplateEngine {
tera: Tera,
}
impl TemplateEngine {
pub fn new(template_dir: PathBuf) -> Result<Self> {
let pattern = format!("{}/**/*.html", template_dir.display());
let tera = Tera::new(&pattern)?;
Ok(Self { tera })
}
pub fn create_context(&self, config: &Config, posts: &[Post]) -> Result<Context> {
let mut context = Context::new();
context.insert("config", &config.site);
context.insert("posts", posts);
Ok(context)
}
pub fn render(&self, template: &str, context: &Context) -> Result<String> {
let output = self.tera.render(template, context)?;
Ok(output)
}
pub fn render_with_context(&self, template: &str, context: &Context) -> Result<String> {
let output = self.tera.render(template, context)?;
Ok(output)
}
}