Skip to content

Commit

Permalink
Merge pull request #601 from yhakbar/adding_hidden_flag
Browse files Browse the repository at this point in the history
Add hidden flag to control when hidden files are ignored
  • Loading branch information
sunng87 authored Aug 31, 2023
2 parents 683f521 + 8de1ca7 commit 01dd891
Showing 1 changed file with 80 additions and 15 deletions.
95 changes: 80 additions & 15 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ fn rhai_engine() -> Engine {
Engine::new()
}

#[cfg(feature = "dir_source")]
pub struct DirectorySourceOptions {
tpl_extension: String,
hidden: bool,
temporary: bool,
}

#[cfg(feature = "dir_source")]
impl Default for DirectorySourceOptions {
fn default() -> Self {
DirectorySourceOptions {
tpl_extension: ".hbs".to_owned(),
hidden: false,
temporary: false,
}
}
}

impl<'reg> Registry<'reg> {
pub fn new() -> Registry<'reg> {
let r = Registry {
Expand Down Expand Up @@ -279,22 +297,23 @@ impl<'reg> Registry<'reg> {
/// * `tpl_extension`: the template file extension
/// * `dir_path`: the path of directory
///
/// Hidden files and tempfile (starts with `#`) will be ignored. All registered
/// will use their relative name as template name. For example, when `dir_path` is
/// `templates/` and `tpl_extension` is `.hbs`, the file
/// Hidden files and tempfile (starts with `#`) will be ignored by default.
/// Set `DirectorySourceOptions` to something other than `DirectorySourceOptions::default()` to adjust this.
/// All registered templates will use their relative path to determine their template name.
/// For example, when `dir_path` is `templates/` and `DirectorySourceOptions.tpl_extension` is `.hbs`, the file
/// `templates/some/path/file.hbs` will be registered as `some/path/file`.
///
/// This method is not available by default.
/// You will need to enable the `dir_source` feature to use it.
///
/// When dev_mode enabled, like `register_template_file`, templates is reloaded
/// from file system everytime it's visied.
/// When dev_mode is enabled, like with `register_template_file`, templates are reloaded
/// from the file system every time they're visited.
#[cfg(feature = "dir_source")]
#[cfg_attr(docsrs, doc(cfg(feature = "dir_source")))]
pub fn register_templates_directory<P>(
&mut self,
tpl_extension: &str,
dir_path: P,
options: DirectorySourceOptions,
) -> Result<(), TemplateError>
where
P: AsRef<Path>,
Expand All @@ -307,13 +326,20 @@ impl<'reg> Registry<'reg> {
.into_iter()
.filter_map(|e| e.ok().map(|e| e.into_path()))
// Checks if extension matches
.filter(|tpl_path| tpl_path.to_string_lossy().ends_with(tpl_extension))
.filter(|tpl_path| {
tpl_path
.to_string_lossy()
.ends_with(options.tpl_extension.as_str())
})
// Rejects any hidden or temporary files.
.filter(|tpl_path| {
tpl_path
.file_stem()
.map(|stem| stem.to_string_lossy())
.map(|stem| !(stem.starts_with('.') || stem.starts_with('#')))
.map(|stem| {
(!stem.starts_with('#') || options.temporary)
&& (!stem.starts_with('.') || options.hidden)
})
.unwrap_or(false)
})
.filter_map(|tpl_path| {
Expand All @@ -328,7 +354,7 @@ impl<'reg> Registry<'reg> {
.join("/");

tpl_name
.strip_suffix(tpl_extension)
.strip_suffix(options.tpl_extension.as_str())
.map(|s| s.to_owned())
.unwrap_or(tpl_name)
})
Expand Down Expand Up @@ -826,6 +852,8 @@ mod test {
fn test_register_templates_directory() {
use std::fs::DirBuilder;

use crate::registry::DirectorySourceOptions;

let mut r = Registry::new();
{
let dir = tempdir().unwrap();
Expand All @@ -848,7 +876,8 @@ mod test {
let mut file4: File = File::create(&file4_path).unwrap();
writeln!(file4, "<h1>Hallo {{world}}!</h1>").unwrap();

r.register_templates_directory(".hbs", dir.path()).unwrap();
r.register_templates_directory(dir.path(), DirectorySourceOptions::default())
.unwrap();

assert_eq!(r.templates.len(), 3);
assert_eq!(r.templates.contains_key("t1"), true);
Expand Down Expand Up @@ -878,7 +907,8 @@ mod test {
let mut file3: File = File::create(&file3_path).unwrap();
writeln!(file3, "<h1>Hello world!</h1>").unwrap();

r.register_templates_directory(".hbs", dir.path()).unwrap();
r.register_templates_directory(dir.path(), DirectorySourceOptions::default())
.unwrap();

assert_eq!(r.templates.len(), 4);
assert_eq!(r.templates.contains_key("t4"), true);
Expand Down Expand Up @@ -913,7 +943,8 @@ mod test {
let mut file3: File = File::create(&file3_path).unwrap();
writeln!(file3, "<h1>Ciao {{world}}!</h1>").unwrap();

r.register_templates_directory(".hbs", dir.path()).unwrap();
r.register_templates_directory(dir.path(), DirectorySourceOptions::default())
.unwrap();

assert_eq!(r.templates.len(), 7);
assert_eq!(r.templates.contains_key("french/t7"), true);
Expand Down Expand Up @@ -941,7 +972,8 @@ mod test {
if !dir_path.ends_with("/") {
dir_path.push('/');
}
r.register_templates_directory(".hbs", dir_path).unwrap();
r.register_templates_directory(dir_path, DirectorySourceOptions::default())
.unwrap();

assert_eq!(r.templates.len(), 8);
assert_eq!(r.templates.contains_key("t10"), true);
Expand All @@ -965,15 +997,48 @@ mod test {
if !dir_path.ends_with("/") {
dir_path.push('/');
}
r.register_templates_directory(".hbs.html", dir_path)
.unwrap();
r.register_templates_directory(
dir_path,
DirectorySourceOptions {
tpl_extension: ".hbs.html".to_owned(),
..Default::default()
},
)
.unwrap();

assert_eq!(r.templates.len(), 1);
assert_eq!(r.templates.contains_key("t11"), true);

drop(file1);
dir.close().unwrap();
}

{
let dir = tempdir().unwrap();
let mut r = Registry::new();

assert_eq!(r.templates.len(), 0);

let file1_path = dir.path().join(".t12.hbs");
let mut file1: File = File::create(&file1_path).unwrap();
writeln!(file1, "<h1>Hello {{world}}!</h1>").unwrap();

r.register_templates_directory(
dir.path(),
DirectorySourceOptions {
hidden: true,
..Default::default()
},
)
.unwrap();

assert_eq!(r.templates.len(), 1);
assert_eq!(r.templates.contains_key(".t12"), true);

drop(file1);

dir.close().unwrap();
}
}

#[test]
Expand Down

0 comments on commit 01dd891

Please sign in to comment.