Automatically trim / on Tera fn calls dealing with files

This commit is contained in:
Vincent Prouillet 2021-06-03 21:00:44 +02:00
parent ae916eb6c5
commit 009d105210
3 changed files with 29 additions and 19 deletions

View file

@ -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<PathBuf> {
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();

View file

@ -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();

View file

@ -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]