Skip to content

Commit

Permalink
Implement register_template_directory to Registry
Browse files Browse the repository at this point in the history
Allow a caller to specify a directory path and an extension, then
register all files in that directory.

Closes sunng87#11
  • Loading branch information
gamebox committed Jun 28, 2018
1 parent 1d5ffea commit 519d4e4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ serde = "^1.0.0"
serde_json = "^1.0.0"
regex = "^1.0.0"
lazy_static = "^1.0.0"
walkdir = "^1.0.0"

[dev-dependencies]
env_logger = "^0.4.0"
Expand Down
3 changes: 2 additions & 1 deletion examples/render-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ fn main() {
let mut handlebars = Handlebars::new();

handlebars
.register_template_file(&filename, &filename)
.register_templates_directory()
.ok()
.unwrap();

match handlebars.render(&filename, &data) {
Ok(data) => {
println!("{}", data);
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::From;
use std::io::Error as IOError;
use std::error::Error;
use std::fmt;
Expand Down Expand Up @@ -242,3 +243,4 @@ impl TemplateRenderError {
}
}
}

1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ extern crate serde;
#[allow(unused_imports)]
#[macro_use]
extern crate serde_json;
extern crate walkdir;

pub use self::template::Template;
pub use self::error::{RenderError, TemplateError, TemplateFileError, TemplateRenderError};
Expand Down
42 changes: 41 additions & 1 deletion src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::io::prelude::*;
use std::fs::File;
use std::io;
use std::fs::{File, read_dir};
use std::path::Path;
use std::fmt::{self, Debug, Formatter};

Expand All @@ -16,6 +17,8 @@ use directives::{self, DirectiveDef};
use support::str::StringWriter;
use error::{RenderError, TemplateError, TemplateFileError, TemplateRenderError};

use walkdir::{WalkDir, DirEntry};

lazy_static!{
static ref DEFAULT_REPLACE: Regex = Regex::new(">|<|\"|&").unwrap();
}
Expand Down Expand Up @@ -174,6 +177,43 @@ impl Registry {
self.register_template_source(name, &mut file)
}

/// Register a template from a directory
pub fn register_templates_directory<P>(
&mut self,
tpl_extension: &'static str,
dir_path: P,
) -> Result<(), TemplateFileError>
where
P: AsRef<Path>,
{
let dir_path = dir_path.as_ref();
let prefix_len = if dir_path.to_string_lossy().ends_with("/") {
dir_path.to_string_lossy().len()
} else {
dir_path.to_string_lossy().len() + 1
};

let walker = WalkDir::new(dir_path);
let mut dir_iter = walker.min_depth(1).into_iter();
match dir_iter.find(|e| e.is_err()) {
Some(Err(err)) => {
let path_string: String = dir_path.to_string_lossy().to_owned().to_string();
Err(TemplateFileError::IOError(io::Error::from(err), path_string))
},
_ => {
for p in dir_iter.filter_map(|e| e.ok()) {
let tpl_path = p.path();
let tpl_file_path = p.path().to_string_lossy();
let tpl_name = &tpl_file_path[prefix_len..tpl_file_path.len() - tpl_extension.len()];
let tpl_canonical_name = tpl_name.replace("\\", "/");
self.register_template_file(&tpl_canonical_name, &tpl_path)?;
}

Ok(())
},
}
}

/// Register a template from `std::io::Read` source
pub fn register_template_source(
&mut self,
Expand Down

0 comments on commit 519d4e4

Please sign in to comment.