feat: add get_feed_config function in templates
This commit is contained in:
parent
6bb67a8129
commit
73ecda59d4
3 changed files with 34 additions and 6 deletions
|
|
@ -4,9 +4,10 @@ use crate::Config;
|
|||
use anyhow::Result;
|
||||
use camino::Utf8Path;
|
||||
use feed_rs::model::Feed;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::fs::{copy, create_dir_all, File};
|
||||
use tera::{from_value, Tera};
|
||||
use url::Url;
|
||||
|
||||
pub fn build(config: &Config, feed_store: &mut FeedStore) -> Result<()> {
|
||||
let mut tera = if let Some(theme) = &config.theme {
|
||||
|
|
@ -27,6 +28,9 @@ pub fn build(config: &Config, feed_store: &mut FeedStore) -> Result<()> {
|
|||
context.insert("PKG_NAME", env!("CARGO_PKG_NAME"));
|
||||
context.insert("PKG_VERSION", env!("CARGO_PKG_VERSION"));
|
||||
tera.register_function("get_author", GetAuthorFunction { feeds });
|
||||
tera.register_function("get_feed_config", GetFeedConfigFunction {
|
||||
feeds: config.feeds.iter().map(|feed| (feed.url.clone(), feed.clone())).collect()
|
||||
});
|
||||
|
||||
for name in tera.get_template_names() {
|
||||
debug!("Processing template {name}");
|
||||
|
|
@ -73,6 +77,31 @@ fn create_tera(templates_dir: &Utf8Path) -> Result<Tera> {
|
|||
Ok(tera)
|
||||
}
|
||||
|
||||
struct GetFeedConfigFunction {
|
||||
feeds: BTreeMap<Url, super::FeedConfig>,
|
||||
}
|
||||
|
||||
impl tera::Function for GetFeedConfigFunction {
|
||||
fn call(&self, args: &HashMap<String, tera::Value>) -> Result<tera::Value, tera::Error> {
|
||||
let feed: Url = match args.get("feed") {
|
||||
None => {
|
||||
return Err(tera::Error::msg(
|
||||
"No argument of name 'feed' given to function.",
|
||||
))
|
||||
}
|
||||
Some(val) => from_value(val.clone())?,
|
||||
};
|
||||
|
||||
if let Some(feed_config) = self.feeds.get(&feed) {
|
||||
Ok(tera::to_value(feed_config)?)
|
||||
} else {
|
||||
Err(tera::Error::msg(
|
||||
format!("Invalid feed URL: {}", feed),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct GetAuthorFunction {
|
||||
feeds: HashMap<String, Feed>,
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue