diff options
author | mash <mashedcode@users.noreply.github.com> | 2018-06-29 15:36:12 +0000 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-07-10 14:29:29 +0100 |
commit | 953df2b3f3afe5c4a357a4353a4c9e8657b96114 (patch) | |
tree | ec2a6faa1ca3df6509151cb35657d2260b099c4b | |
parent | 9d2c6115dc7e4ec2c24ce30581d3bf00f787e441 (diff) | |
download | askama-953df2b3f3afe5c4a357a4353a4c9e8657b96114.tar.gz askama-953df2b3f3afe5c4a357a4353a4c9e8657b96114.tar.bz2 askama-953df2b3f3afe5c4a357a4353a4c9e8657b96114.zip |
Add partial support for multiple template dirs
-rw-r--r-- | askama/src/lib.rs | 8 | ||||
-rw-r--r-- | askama_shared/Cargo.toml | 6 | ||||
-rw-r--r-- | askama_shared/src/lib.rs | 4 | ||||
-rw-r--r-- | askama_shared/src/path.rs | 32 |
4 files changed, 44 insertions, 6 deletions
diff --git a/askama/src/lib.rs b/askama/src/lib.rs index f64fa68..aaa1c3b 100644 --- a/askama/src/lib.rs +++ b/askama/src/lib.rs @@ -393,7 +393,9 @@ fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> { /// that have templates, to make sure the crate gets rebuilt when template /// source code changes. pub fn rerun_if_templates_changed() { - visit_dirs(&path::template_dir(), &|e: &DirEntry| { - println!("cargo:rerun-if-changed={}", e.path().to_str().unwrap()); - }).unwrap(); + for template_dir in path::template_dirs().iter() { + visit_dirs(template_dir, &|e: &DirEntry| { + println!("cargo:rerun-if-changed={}", e.path().to_str().unwrap()); + }).unwrap(); + } } diff --git a/askama_shared/Cargo.toml b/askama_shared/Cargo.toml index 8b18805..cd052cf 100644 --- a/askama_shared/Cargo.toml +++ b/askama_shared/Cargo.toml @@ -10,10 +10,12 @@ workspace = ".." [features] default = [] -serde-json = ["serde", "serde_json"] +serde-json = ["serde_json"] iron = [] rocket = [] [dependencies] -serde = { version = "1.0", optional = true } +serde = "1.0" +serde_derive = "1.0" serde_json = { version = "1.0", optional = true } +toml = "0.4" diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs index 9427dff..64d8d25 100644 --- a/askama_shared/src/lib.rs +++ b/askama_shared/src/lib.rs @@ -1,9 +1,11 @@ #![cfg_attr(feature = "cargo-clippy", allow(unused_parens))] -#[cfg(feature = "serde-json")] extern crate serde; +#[macro_use] +extern crate serde_derive; #[cfg(feature = "serde-json")] extern crate serde_json; +extern crate toml; pub use error::{Error, Result}; pub use escaping::MarkupDisplay; diff --git a/askama_shared/src/path.rs b/askama_shared/src/path.rs index 8b92250..37fb261 100644 --- a/askama_shared/src/path.rs +++ b/askama_shared/src/path.rs @@ -3,6 +3,8 @@ use std::fs::File; use std::io::Read; use std::path::{Path, PathBuf}; +use toml; + pub fn get_template_source(tpl_path: &Path) -> String { let mut path = template_dir(); path.push(tpl_path); @@ -52,6 +54,36 @@ pub fn template_dir() -> PathBuf { path } +#[derive(Deserialize)] +struct Config { + dirs: Option<Vec<String>>, +} + +pub fn template_dirs() -> Vec<PathBuf> { + let root = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + let askama = root.join("askama.toml"); + let default = vec![root.join("templates")]; + if askama.exists() { + let mut contents = String::new(); + File::open(askama).unwrap().read_to_string(&mut contents).unwrap(); + let config: Config = toml::from_str(&contents).unwrap(); + if let Some(dirs) = config.dirs { + let paths: Vec<PathBuf> = dirs.into_iter().map(|directory| { + root.join(directory) + }).collect(); + if paths.len() > 0 { + paths + } else { + default + } + } else { + default + } + } else { + vec![root.join("templates")] + } +} + #[cfg(test)] mod tests { use super::Path; |