implemented also atom feed via template
This commit is contained in:
parent
8b17e93d74
commit
cfab9ef5c0
10 changed files with 259 additions and 114 deletions
35
web/planet-mars/src/template_engine.rs
Normal file
35
web/planet-mars/src/template_engine.rs
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
use crate::feed_store::FeedStore;
|
||||
use crate::to_checked_pathbuf;
|
||||
use crate::Config;
|
||||
use anyhow::Result;
|
||||
use feed_rs::model::Entry;
|
||||
use std::fs::File;
|
||||
use tera::Tera;
|
||||
|
||||
pub fn build(config: &Config, feed_store: &FeedStore) -> Result<()> {
|
||||
let tera = create_tera(&config.templates_dir)?;
|
||||
let out_dir = to_checked_pathbuf(&config.out_dir);
|
||||
|
||||
let mut context = tera::Context::new();
|
||||
let feed_entries: Vec<Entry> = feed_store.collect(&config.feeds);
|
||||
context.insert("entries", &feed_entries);
|
||||
context.insert("PKG_AUTHORS", env!("CARGO_PKG_AUTHORS"));
|
||||
context.insert("PKG_HOMEPAGE", env!("CARGO_PKG_HOMEPAGE"));
|
||||
context.insert("PKG_NAME", env!("CARGO_PKG_NAME"));
|
||||
context.insert("PKG_VERSION", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
for name in tera.get_template_names() {
|
||||
debug!("Processing template {name}");
|
||||
let file = File::create(format!("{}/{name}", out_dir.display()))?;
|
||||
tera.render_to(name, &context, file)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn create_tera(templates_dir: &str) -> Result<Tera> {
|
||||
let dir = to_checked_pathbuf(templates_dir);
|
||||
let mut tera = tera::Tera::new(&format!("{}/*", &dir.display()))?;
|
||||
// disable autoescape as this would corrupt urls or the entriy contents. todo check this!
|
||||
tera.autoescape_on(vec![]);
|
||||
Ok(tera)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue