Merge pull request #56 from Keats/get_page

Add global get_page tera fn
This commit is contained in:
Vincent Prouillet 2017-05-10 12:44:08 +09:00 committed by GitHub
commit 8821ec17d7
4 changed files with 38 additions and 4 deletions

8
Cargo.lock generated
View file

@ -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"

29
src/global_fns.rs Normal file
View file

@ -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<PathBuf, Page>) -> 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<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()),
}
})
}

View file

@ -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};

View file

@ -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(())
}