From 6f5e00885312f040668fd97319fbfe08ed1fb5d2 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Wed, 3 May 2017 23:16:09 +0900 Subject: [PATCH] Add global get_page tera fn --- Cargo.lock | 8 ++++---- src/global_fns.rs | 29 +++++++++++++++++++++++++++++ src/lib.rs | 2 ++ src/site.rs | 3 +++ 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/global_fns.rs diff --git a/Cargo.lock b/Cargo.lock index 5737d73a..1313da34 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,7 +19,7 @@ dependencies = [ "staticfile 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntect 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", - "tera 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tera 0.10.4 (registry+https://github.com/rust-lang/crates.io-index)", "term-painter 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "toml 0.4.0 (git+https://github.com/alexcrichton/toml-rs)", "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", @@ -820,7 +820,7 @@ dependencies = [ [[package]] name = "tera" -version = "0.10.3" +version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "chrono 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -895,7 +895,7 @@ dependencies = [ [[package]] name = "toml" version = "0.4.0" -source = "git+https://github.com/alexcrichton/toml-rs#58f51ef03b88e06745c4113e13ea2738e1af247d" +source = "git+https://github.com/alexcrichton/toml-rs#f94dc8d69b22f330e6be19882f2f02e09895f18f" dependencies = [ "serde 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1165,7 +1165,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum syntect 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24204b1f4bdd49f84e5f4b219d0bf1dc45ac2fd7fc46320ab6627b537d6d4b69" "checksum tempdir 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "87974a6f5c1dfb344d733055601650059a3363de2a6104819293baff662132d6" -"checksum tera 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)" = "86bc1156f5502b5eb3904348f4bea155d728e51fec7c981c44b3f1d10b8e574b" +"checksum tera 0.10.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5d440e681b91b057dcaa4d49d7e04af824d0d0769e8c5d21ebb8fc86f7c4b3b7" "checksum term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d168af3930b369cfe245132550579d47dfd873d69470755a19c2c6568dbbd989" "checksum term-painter 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ab900bf2f05175932b13d4fc12f8ff09ef777715b04998791ab2c930841e496b" "checksum term_size 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2b6b55df3198cc93372e85dd2ed817f0e38ce8cc0f22eb32391bfad9c4bf209" diff --git a/src/global_fns.rs b/src/global_fns.rs new file mode 100644 index 00000000..3205378a --- /dev/null +++ b/src/global_fns.rs @@ -0,0 +1,29 @@ +use std::collections::HashMap; +use std::path::{PathBuf}; + +use tera::{GlobalFn, Value, from_value, to_value, Result}; + +use page::Page; + + +pub fn make_get_page(all_pages: &HashMap) -> GlobalFn { + let mut pages = HashMap::new(); + for page in all_pages.values() { + pages.insert(page.relative_path.clone(), page.clone()); + } + + Box::new(move |args| -> Result { + match args.get("path") { + Some(val) => match from_value::(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()), + } + }) +} diff --git a/src/lib.rs b/src/lib.rs index 5965712e..6e79fbb2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,8 @@ mod section; mod pagination; /// Additional filters for Tera mod filters; +/// Global fns for Tera +mod global_fns; pub use site::{Site, GUTENBERG_TERA}; pub use config::{Config, get_config}; diff --git a/src/site.rs b/src/site.rs index 27d2373d..47a6b6cc 100644 --- a/src/site.rs +++ b/src/site.rs @@ -16,6 +16,7 @@ use utils::{create_file, create_directory}; use section::{Section}; use front_matter::{SortBy}; use filters; +use global_fns; lazy_static! { @@ -191,6 +192,8 @@ impl Site { self.populate_sections(); self.populate_tags_and_categories(); + self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages)); + Ok(()) }