diff options
| author | Ben Bridle <ben@derelict.engineering> | 2025-12-12 20:21:57 +1300 |
|---|---|---|
| committer | Ben Bridle <ben@derelict.engineering> | 2025-12-12 20:22:08 +1300 |
| commit | dd0aff0b170a71ef2962fcd38b710c581d90f9da (patch) | |
| tree | cf969477d6209cad053821b0aaf7ab9e1b930190 | |
| parent | 50df287852367d3e50779155c6e92b6e2a388c9d (diff) | |
| download | toaster-dd0aff0b170a71ef2962fcd38b710c581d90f9da.zip | |
Galleries previously required images to be placed in /images/large/..
and /images/thumb/.., but this was a chore if there was only a single
average-quality version of the image to display. The path /images/..
is now used as a fallback if these more specialised paths do not exist.
| -rw-r--r-- | src/collect_files.rs | 24 | ||||
| -rw-r--r-- | src/generate_html.rs | 26 |
2 files changed, 34 insertions, 16 deletions
diff --git a/src/collect_files.rs b/src/collect_files.rs index 30dcb98..e3d3a11 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -75,6 +75,11 @@ pub struct Highlighters { pub highlighters: Vec<Highlighter>, } +pub struct ImagePaths { + pub thumb: String, + pub large: String, +} + impl Page { pub fn root(&self) -> String { let mut root = String::new(); @@ -408,9 +413,22 @@ impl Website { return None; } - pub fn has_image(&self, file_name: &str) -> bool { - let image_path = format!("images/thumb/{file_name}"); - self.static_files.iter().any(|s| s.full_url == image_path) + pub fn has_image(&self, file_name: &str, root: &str) -> Option<ImagePaths> { + let check = |path: String| + match self.static_files.iter().any(|s| s.full_url == path) { + true => Some(format!("{root}{path}")), + false => None, + }; + let thumb_path = check(format!("images/thumb/{file_name}")); + let large_path = check(format!("images/large/{file_name}")); + let fallback_path = check(format!("images/{file_name}")) + .or(large_path.clone()) + .or(thumb_path.clone()); + + Some(ImagePaths { + thumb: thumb_path.or(fallback_path.clone())?, + large: large_path.or(fallback_path.clone())?, + }) } pub fn get_config(&self, key: &str) -> String { diff --git a/src/generate_html.rs b/src/generate_html.rs index 306892b..2b979ea 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -257,27 +257,27 @@ pub fn document_to_html(page: &Page, website: &Website) -> String { html!("</div>"); }, "gallery" => wrap!("div", "class='gallery'", for line in content.lines() { - let file = line.trim(); - if !website.has_image(file) { - warn!("Gallery on page {from:?} references nonexistent image {file:?}"); - continue; + let file_name = line.trim(); + if let Some(image_paths) = website.has_image(file_name, &root) { + let large = sanitize_text(&image_paths.large, false); + let thumb = sanitize_text(&image_paths.thumb, false); + html!("<a href='{large}'><img src='{thumb}' /></a>"); + } else { + warn!("Gallery on page {from:?} references nonexistent image {file_name:?}"); } - let large = sanitize_text(&format!("{root}images/large/{file}"), false); - // let small = sanitize_text(&format!("{root}images/small/{file}"), false); - let thumb = sanitize_text(&format!("{root}images/thumb/{file}"), false); - html!("<a href='{large}'><img src='{thumb}' /></a>"); }), "gallery-nav" => wrap!("div", "class='gallery-nav'", for line in content.lines() { let line = line.trim(); - if let Some((name, image)) = line.split_once("::") { + if let Some((name, file_name)) = line.split_once("::") { let name = name.trim(); - let image = image.trim(); + let file_name = file_name.trim(); let ParsedLink { path, class, label } = parse_internal_link(name, page, website); - if website.has_image(image) { - let thumb = sanitize_text(&format!("{root}images/thumb/{image}"), false); + if let Some(image_paths) = website.has_image(file_name, &root) { + let thumb = sanitize_text(&image_paths.thumb, false); html!("<a href='{path}' class='{class}'><img src='{thumb}'/><p>{label}</p></a>") } else { - warn!("Gallery-nav on page {from:?} references nonexistent image {image:?}"); + warn!("Gallery on page {from:?} references nonexistent image {file_name:?}"); + warn!("Gallery-nav on page {from:?} references nonexistent image {file_name:?}"); } } else { warn!("Gallery-nav on page {from:?} has line without a '::' separator"); |
