zola/src/templates/global_fns.rs

68 lines
2.5 KiB
Rust
Raw Normal View History

2017-05-03 14:16:09 +00:00
use std::collections::HashMap;
use std::path::{PathBuf};
use tera::{GlobalFn, Value, from_value, to_value, Result};
2017-05-23 11:03:25 +00:00
use content::{Page, Section};
2017-05-17 10:04:26 +00:00
use site::resolve_internal_link;
2017-05-03 14:16:09 +00:00
pub fn make_get_page(all_pages: &HashMap<PathBuf, Page>) -> GlobalFn {
let mut pages = HashMap::new();
for page in all_pages.values() {
pages.insert(page.file.relative.clone(), page.clone());
2017-05-03 14:16:09 +00:00
}
Box::new(move |args| -> Result<Value> {
match args.get("path") {
Some(val) => match from_value::<String>(val.clone()) {
Ok(v) => {
match pages.get(&v) {
Some(p) => Ok(to_value(p).unwrap()),
None => Err(format!("Page `{}` not found.", v).into())
}
},
Err(_) => Err(format!("`get_page` received path={:?} but it requires a string", val).into()),
},
None => Err("`get_page` requires a `path` argument.".into()),
}
})
}
2017-05-17 10:04:26 +00:00
2017-05-23 11:03:25 +00:00
pub fn make_get_section(all_sections: &HashMap<PathBuf, Section>) -> GlobalFn {
let mut sections = HashMap::new();
for section in all_sections.values() {
sections.insert(section.file.relative.clone(), section.clone());
}
Box::new(move |args| -> Result<Value> {
match args.get("path") {
Some(val) => match from_value::<String>(val.clone()) {
Ok(v) => {
match sections.get(&v) {
Some(p) => Ok(to_value(p).unwrap()),
None => Err(format!("Section `{}` not found.", v).into())
}
},
Err(_) => Err(format!("`get_section` received path={:?} but it requires a string", val).into()),
},
None => Err("`get_section` requires a `path` argument.".into()),
}
})
}
2017-05-17 10:04:26 +00:00
pub fn make_get_url(permalinks: HashMap<String, String>,) -> GlobalFn {
Box::new(move |args| -> Result<Value> {
match args.get("link") {
Some(val) => match from_value::<String>(val.clone()) {
Ok(v) => match resolve_internal_link(&v, &permalinks) {
Ok(url) => Ok(to_value(url).unwrap()),
Err(_) => Err(format!("Could not resolve URL for link `{}` not found.", v).into())
},
Err(_) => Err(format!("`get_url` received link={:?} but it requires a string", val).into()),
},
None => Err("`get_url` requires a `link` argument.".into()),
}
})
}