From 009d10521026c30f9f7adb048a9e63ab26b4fca3 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 3 Jun 2021 21:00:44 +0200 Subject: [PATCH] Automatically trim `/` on Tera fn calls dealing with files --- .../templates/src/global_fns/helpers.rs | 5 +-- components/templates/src/global_fns/images.rs | 32 ++++++++++++------- .../templates/src/global_fns/load_data.rs | 11 ++++--- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/components/templates/src/global_fns/helpers.rs b/components/templates/src/global_fns/helpers.rs index 836c0e67..2e8cb459 100644 --- a/components/templates/src/global_fns/helpers.rs +++ b/components/templates/src/global_fns/helpers.rs @@ -6,13 +6,14 @@ use std::path::{Path, PathBuf}; /// 1. base_path + path /// 2. base_path + static + path /// 3. base_path + content + path -/// A path starting with @/ will replace it with `content/` +/// A path starting with @/ will replace it with `content/` and a path starting with `/` will have +/// it removed. pub fn search_for_file(base_path: &Path, path: &str) -> Option { let search_paths = [base_path.join("static"), base_path.join("content")]; let actual_path = if path.starts_with("@/") { Cow::Owned(path.replace("@/", "content/")) } else { - Cow::Borrowed(path) + Cow::Borrowed(path.trim_start_matches('/')) }; let mut file_path = base_path.join(&*actual_path); let mut file_exists = file_path.exists(); diff --git a/components/templates/src/global_fns/images.rs b/components/templates/src/global_fns/images.rs index c851cec4..254db728 100644 --- a/components/templates/src/global_fns/images.rs +++ b/components/templates/src/global_fns/images.rs @@ -211,19 +211,29 @@ mod tests { to_value("http://a-website.com/processed_images/32454a1e0243976c00.jpg").unwrap() ); - // 3. resizing an image in content starting with `@/` - args.insert("path".to_string(), to_value("@/gutenberg.jpg").unwrap()); - let data = static_fn.call(&args).unwrap().as_object().unwrap().clone(); + // 3. resizing with an absolute path is the same as the above + args.insert("path".to_string(), to_value("/content/gutenberg.jpg").unwrap()); assert_eq!( data["static_path"], - to_value(&format!("{}", static_path.join("074e171855ee541800.jpg").display())).unwrap() + to_value(&format!("{}", static_path.join("32454a1e0243976c00.jpg").display())).unwrap() ); assert_eq!( data["url"], - to_value("http://a-website.com/processed_images/074e171855ee541800.jpg").unwrap() + to_value("http://a-website.com/processed_images/32454a1e0243976c00.jpg").unwrap() ); - // 4. resizing an image with a relative path not starting with static or content + // 4. resizing an image in content starting with `@/` is the same as 2 and 3 + args.insert("path".to_string(), to_value("@/gutenberg.jpg").unwrap()); + assert_eq!( + data["static_path"], + to_value(&format!("{}", static_path.join("32454a1e0243976c00.jpg").display())).unwrap() + ); + assert_eq!( + data["url"], + to_value("http://a-website.com/processed_images/32454a1e0243976c00.jpg").unwrap() + ); + + // 5. resizing an image with a relative path not starting with static or content args.insert("path".to_string(), to_value("gallery/asset.jpg").unwrap()); let data = static_fn.call(&args).unwrap().as_object().unwrap().clone(); assert_eq!( @@ -234,10 +244,6 @@ mod tests { data["url"], to_value("http://a-website.com/processed_images/c8aaba7b0593a60b00.jpg").unwrap() ); - - // 5. resizing with an absolute path - args.insert("path".to_string(), to_value("/content/gutenberg.jpg").unwrap()); - assert!(static_fn.call(&args).is_err()); } // TODO: consider https://github.com/getzola/zola/issues/1161 @@ -256,10 +262,12 @@ mod tests { assert_eq!(data["height"], to_value(380).unwrap()); assert_eq!(data["width"], to_value(300).unwrap()); - // 2. a call to something in `static` with an absolute path is not handled currently + // 2. a call to something in `static` with an absolute path is handled currently the same as the above let mut args = HashMap::new(); args.insert("path".to_string(), to_value("/static/gutenberg.jpg").unwrap()); - assert!(static_fn.call(&args).is_err()); + let data = static_fn.call(&args).unwrap().as_object().unwrap().clone(); + assert_eq!(data["height"], to_value(380).unwrap()); + assert_eq!(data["width"], to_value(300).unwrap()); // 3. a call to something in `content` with a relative path let mut args = HashMap::new(); diff --git a/components/templates/src/global_fns/load_data.rs b/components/templates/src/global_fns/load_data.rs index 7992b695..d24e9e86 100644 --- a/components/templates/src/global_fns/load_data.rs +++ b/components/templates/src/global_fns/load_data.rs @@ -622,14 +622,15 @@ mod tests { let data = static_fn.call(&args).unwrap().as_str().unwrap().to_string(); assert_eq!(data, val); - // 3. path starting with @/ - args.insert("path".to_string(), to_value("@/test.css").unwrap()); + // 3. absolute path is the same + args.insert("path".to_string(), to_value("/content/test.css").unwrap()); let data = static_fn.call(&args).unwrap().as_str().unwrap().to_string(); assert_eq!(data, val); - // 4. absolute path does not work - args.insert("path".to_string(), to_value("/test.css").unwrap()); - assert!(static_fn.call(&args).is_err()); + // 4. path starting with @/ + args.insert("path".to_string(), to_value("@/test.css").unwrap()); + let data = static_fn.call(&args).unwrap().as_str().unwrap().to_string(); + assert_eq!(data, val); } #[test]