Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: loading templates which has multiple extensions #551

Merged
merged 1 commit into from
Dec 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 33 additions & 13 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,13 @@ impl<'reg> Registry<'reg> {
{
let dir_path = dir_path.as_ref();

// Allowing dots at the beginning as to not break old
// applications.
let tpl_extension = tpl_extension.strip_prefix('.').unwrap_or(tpl_extension);

let walker = WalkDir::new(dir_path);
let dir_iter = walker
.min_depth(1)
.into_iter()
.filter_map(|e| e.ok().map(|e| e.into_path()))
// Checks if extension matches
.filter(|tpl_path| {
tpl_path
.extension()
.map(|extension| extension == tpl_extension)
.unwrap_or(false)
})
.filter(|tpl_path| tpl_path.to_string_lossy().ends_with(tpl_extension))
// Rejects any hidden or temporary files.
.filter(|tpl_path| {
tpl_path
Expand All @@ -327,12 +318,16 @@ impl<'reg> Registry<'reg> {
.strip_prefix(dir_path)
.ok()
.map(|tpl_canonical_name| {
tpl_canonical_name
.with_extension("")
let tpl_name = tpl_canonical_name
.components()
.map(|component| component.as_os_str().to_string_lossy())
.collect::<Vec<_>>()
.join("/")
.join("/");

tpl_name
.strip_suffix(tpl_extension)
.map(|s| s.to_owned())
.unwrap_or(tpl_name)
})
.map(|tpl_canonical_name| (tpl_canonical_name, tpl_path))
});
Expand Down Expand Up @@ -909,6 +904,31 @@ mod test {
drop(file1);
dir.close().unwrap();
}

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

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

let mut dir_path = dir
.path()
.to_string_lossy()
.replace(std::path::MAIN_SEPARATOR, "/");
if !dir_path.ends_with("/") {
dir_path.push('/');
}
r.register_templates_directory(".hbs.html", dir_path)
.unwrap();

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

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

#[test]
Expand Down