From ae916eb6c5347ed3e202a3180b54d2fe43e75a46 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Mon, 17 May 2021 20:26:50 +0200 Subject: [PATCH] Some more tweaks --- CHANGELOG.md | 4 +++- components/templates/src/global_fns/content.rs | 4 ++++ components/templates/src/global_fns/files.rs | 4 ++++ components/templates/src/global_fns/helpers.rs | 1 + components/templates/src/global_fns/images.rs | 10 ++++++++++ 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02a0f0e3..80ec6cce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ ### Breaking - Newlines are now required after the closing `+++` of front-matter -- `resize_image` now returns a map: `{url, static_path}` instead of just the URL so you can follow up with other functions +- `resize_image` now returns an object: `{url, static_path}` instead of just the URL so you can follow up with other functions on the new file if needed - `get_file_hash` now has the `base64` option set to `true` by default (from `false`) since it's mainly used for integrity hashes which are base64 - `get_url` does not automatically strip leading `/` from paths anymore - i18n rework: languages now have their sections in `config.toml` to set up all their options @@ -30,6 +30,8 @@ - Allow using POST for `load_data`, along with a body to POST and allow it to fail - Add Zig and Protobuf syntax highlighting - Footnotes links are now stripped from summaries - they were not linking to anything. +- `get_url` and `get_taxonomy_url` are now marked as safe, no need to call `| safe` on their output +- Add `allow_missing` optional argument to `get_image_metadata` to not error if the file is not found ## 0.13.0 (2021-01-09) diff --git a/components/templates/src/global_fns/content.rs b/components/templates/src/global_fns/content.rs index 91f2387f..688465a0 100644 --- a/components/templates/src/global_fns/content.rs +++ b/components/templates/src/global_fns/content.rs @@ -58,6 +58,10 @@ impl TeraFn for GetTaxonomyUrl { Err(format!("`get_taxonomy_url`: couldn't find `{}` in `{}` taxonomy", name, kind).into()) } + + fn is_safe(&self) -> bool { + true + } } #[derive(Debug)] diff --git a/components/templates/src/global_fns/files.rs b/components/templates/src/global_fns/files.rs index 3c33d027..34ece031 100644 --- a/components/templates/src/global_fns/files.rs +++ b/components/templates/src/global_fns/files.rs @@ -130,6 +130,10 @@ impl TeraFn for GetUrl { Ok(to_value(permalink).unwrap()) } } + + fn is_safe(&self) -> bool { + true + } } #[derive(Debug)] diff --git a/components/templates/src/global_fns/helpers.rs b/components/templates/src/global_fns/helpers.rs index 4ea5c26f..836c0e67 100644 --- a/components/templates/src/global_fns/helpers.rs +++ b/components/templates/src/global_fns/helpers.rs @@ -6,6 +6,7 @@ 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/` 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("@/") { diff --git a/components/templates/src/global_fns/images.rs b/components/templates/src/global_fns/images.rs index 5773d755..c851cec4 100644 --- a/components/templates/src/global_fns/images.rs +++ b/components/templates/src/global_fns/images.rs @@ -123,9 +123,19 @@ impl TeraFn for GetImageMetadata { args.get("path"), "`get_image_metadata` requires a `path` argument with a string value" ); + let allow_missing = optional_arg!( + bool, + args.get("allow_missing"), + "`get_image_metadata`: `allow_missing` must be a boolean (true or false)" + ) + .unwrap_or(false); let src_path = match search_for_file(&self.base_path, &path) { Some(f) => f, None => { + if allow_missing { + println!("Image at path {} could not be found or loaded", path); + return Ok(Value::Null); + } return Err(format!("`resize_image`: Cannot find path: {}", path).into()); } };