From 53a6c347852abbc404a7c630abcdcb78004f657a Mon Sep 17 00:00:00 2001 From: Nick Pegg Date: Tue, 6 May 2025 18:49:28 -0700 Subject: [PATCH] some fixes for album html rendering --- resources/skel/_templates/album.html | 7 +++---- src/generate.rs | 31 ++++++++++++++-------------- src/generate/album_dir.rs | 12 ++++++----- src/main.rs | 4 ++-- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/resources/skel/_templates/album.html b/resources/skel/_templates/album.html index 4489769..57a1a99 100644 --- a/resources/skel/_templates/album.html +++ b/resources/skel/_templates/album.html @@ -3,7 +3,6 @@ {% block content %} {% if root_path %}

- Home {% for crumb in breadcrumbs %} / {{crumb.name}} {% endfor %} @@ -12,9 +11,9 @@
{% endif %} - {% if album_dir.description %} + {% if description %}
- {{ album_dir.description | safe }} + {{ description | safe }}
{% endif %} @@ -26,7 +25,7 @@
+ src="{{child.name}}/{{child.cover_thumbnail_path}}" />
{{child.name}} diff --git a/src/generate.rs b/src/generate.rs index 33e810d..77690a0 100644 --- a/src/generate.rs +++ b/src/generate.rs @@ -7,7 +7,7 @@ use image::imageops::FilterType; use serde::Serialize; use std::collections::VecDeque; use std::env; -use std::ffi::{OsStr, OsString}; +use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; use tera::Tera; @@ -17,7 +17,7 @@ const IMG_RESIZE_FILTER: FilterType = FilterType::Lanczos3; #[derive(Serialize, Debug)] struct Breadcrumb { path: PathBuf, - name: OsString, + name: String, } /// A Tera context for album pages @@ -26,15 +26,13 @@ struct AlbumContext { // Path required to get back to the root album root_path: PathBuf, - // TODO: Do we actualy need the whole albumDir? Probably better off pulling data we need out of - // it. - // album_dir: AlbumDir, - name: OsString, + name: String, + description: String, // TODO: images - // Path to the cover image thumbnail within /slides/. Used when linking to an album from a - // parent album + // Path to the cover image thumbnail within /slides/, relative to the album dir. Used when + // linking to an album from a parent album cover_thumbnail_path: PathBuf, // list of: @@ -50,9 +48,9 @@ impl TryFrom<&AlbumDir> for AlbumContext { type Error = anyhow::Error; fn try_from(album: &AlbumDir) -> anyhow::Result { - let name: OsString = match album.path.file_name() { - Some(n) => n.to_os_string(), - None => OsString::new(), + let name: String = match album.path.file_name() { + Some(n) => n.to_string_lossy().to_string(), + None => String::new(), }; // Build breadcrumbs @@ -65,7 +63,7 @@ impl TryFrom<&AlbumDir> for AlbumContext { relpath.push(".."); breadcrumbs.push(Breadcrumb { path: relpath.clone(), - name: filename.into(), + name: filename.to_string_lossy().to_string(), }); } } @@ -81,7 +79,7 @@ impl TryFrom<&AlbumDir> for AlbumContext { // Pick a cover image thumbnail and build a path to it let cover_thumbnail_path = match &album.cover { - Some(i) => i.path.clone(), + Some(i) => Path::new("slides").join(&i.thumb_filename), None => PathBuf::from(""), }; @@ -93,6 +91,7 @@ impl TryFrom<&AlbumDir> for AlbumContext { Ok(AlbumContext { name, + description: album.description.clone(), breadcrumbs, root_path, children, @@ -121,7 +120,7 @@ pub fn generate(root_path: &PathBuf) -> anyhow::Result { env::set_current_dir(root_path)?; let album = AlbumDir::try_from(root_path)?; - fs::create_dir(&config.output_dir)?; + fs::create_dir_all(&config.output_dir)?; copy_static(&config)?; generate_images(&config, &album)?; generate_html(&config, &album)?; @@ -136,7 +135,9 @@ fn copy_static(config: &Config) -> anyhow::Result<()> { fs_extra::dir::copy( "_static", dst, - &fs_extra::dir::CopyOptions::new().content_only(true), + &fs_extra::dir::CopyOptions::new() + .content_only(true) + .overwrite(true), )?; Ok(()) } diff --git a/src/generate/album_dir.rs b/src/generate/album_dir.rs index 40d7963..a1612b0 100644 --- a/src/generate/album_dir.rs +++ b/src/generate/album_dir.rs @@ -45,12 +45,14 @@ impl AlbumDir { let _conents = fs::read_to_string(entry_path)?; // TODO: render markdown todo!(); - } else if filename.to_string_lossy().starts_with("cover") { - cover = Some(Image::new( - entry_path.strip_prefix(&root)?.to_path_buf(), - String::new(), - )?); } else { + if filename.to_string_lossy().starts_with("cover") { + cover = Some(Image::new( + entry_path.strip_prefix(&root)?.to_path_buf(), + String::new(), + )?); + } + let reader = ImageReader::open(&entry_path)?.with_guessed_format()?; if reader.format().is_some() { // Found an image diff --git a/src/main.rs b/src/main.rs index 5b53e23..454e48c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,8 @@ fn main() -> anyhow::Result<()> { println!("Album created in {}", album_path.display()); } Commands::Generate { quick } => { - println!("Generate, quick: {quick}"); - generate(&album_path.to_path_buf())?; + let path = generate(&album_path.to_path_buf())?; + println!("Album site generated in {}", path.display()); } }