Skip to content

Commit 40ecb11

Browse files
committed
fix: loading templates which has multiple extensions
1 parent 9a8335c commit 40ecb11

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

src/registry.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,13 @@ impl<'reg> Registry<'reg> {
298298
{
299299
let dir_path = dir_path.as_ref();
300300

301-
// Allowing dots at the beginning as to not break old
302-
// applications.
303-
let tpl_extension = tpl_extension.strip_prefix('.').unwrap_or(tpl_extension);
304-
305301
let walker = WalkDir::new(dir_path);
306302
let dir_iter = walker
307303
.min_depth(1)
308304
.into_iter()
309305
.filter_map(|e| e.ok().map(|e| e.into_path()))
310306
// Checks if extension matches
311-
.filter(|tpl_path| {
312-
tpl_path
313-
.extension()
314-
.map(|extension| extension == tpl_extension)
315-
.unwrap_or(false)
316-
})
307+
.filter(|tpl_path| tpl_path.to_string_lossy().ends_with(tpl_extension))
317308
// Rejects any hidden or temporary files.
318309
.filter(|tpl_path| {
319310
tpl_path
@@ -327,12 +318,16 @@ impl<'reg> Registry<'reg> {
327318
.strip_prefix(dir_path)
328319
.ok()
329320
.map(|tpl_canonical_name| {
330-
tpl_canonical_name
331-
.with_extension("")
321+
let tpl_name = tpl_canonical_name
332322
.components()
333323
.map(|component| component.as_os_str().to_string_lossy())
334324
.collect::<Vec<_>>()
335-
.join("/")
325+
.join("/");
326+
327+
tpl_name
328+
.strip_suffix(tpl_extension)
329+
.map(|s| s.to_owned())
330+
.unwrap_or(tpl_name)
336331
})
337332
.map(|tpl_canonical_name| (tpl_canonical_name, tpl_path))
338333
});
@@ -908,6 +903,31 @@ mod test {
908903
drop(file1);
909904
dir.close().unwrap();
910905
}
906+
907+
{
908+
let dir = tempdir().unwrap();
909+
let mut r = Registry::new();
910+
911+
let file1_path = dir.path().join("t11.hbs.html");
912+
let mut file1: File = File::create(&file1_path).unwrap();
913+
writeln!(file1, "<h1>Bonjour {{world}}!</h1>").unwrap();
914+
915+
let mut dir_path = dir
916+
.path()
917+
.to_string_lossy()
918+
.replace(std::path::MAIN_SEPARATOR, "/");
919+
if !dir_path.ends_with("/") {
920+
dir_path.push('/');
921+
}
922+
r.register_templates_directory(".hbs.html", dir_path)
923+
.unwrap();
924+
925+
assert_eq!(r.templates.len(), 1);
926+
assert_eq!(r.templates.contains_key("t11"), true);
927+
928+
drop(file1);
929+
dir.close().unwrap();
930+
}
911931
}
912932

913933
#[test]

0 commit comments

Comments
 (0)