diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-01-15 15:54:27 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-01-15 16:22:59 +1300 |
commit | bc3e25f8c1edbd233ed0c266c574892e88644d9c (patch) | |
tree | 5da6e11aa9738b11a55190950e8bb335cc30ad7f | |
parent | 01cd799f688ad39cfb03ede3e149512d1c8faef6 (diff) | |
download | toaster-bc3e25f8c1edbd233ed0c266c574892e88644d9c.zip |
Implement image galleries
Each gallery image must be kept in the three directories images/large,
images/small, and images/thumb. A gallery is a fragment with the
language 'gallery' containing a list of image filenames, one per line.
-rw-r--r-- | Cargo.lock | 4 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/collect_files.rs | 14 | ||||
-rw-r--r-- | src/generate_html.rs | 11 |
4 files changed, 28 insertions, 3 deletions
@@ -24,8 +24,8 @@ dependencies = [ [[package]] name = "vagabond" -version = "1.0.2" -source = "git+git://benbridle.com/vagabond?tag=v1.0.2#740df75e2bf62d067ca76bd661f9c46b4a228678" +version = "1.0.3" +source = "git+git://benbridle.com/vagabond?tag=v1.0.3#fb80ee168e1977230680115304103b80ccf7f1ac" [[package]] name = "xflags" @@ -4,7 +4,7 @@ version = "1.5.0" edition = "2021" [dependencies] -vagabond = { git = "git://benbridle.com/vagabond", tag = "v1.0.2" } +vagabond = { git = "git://benbridle.com/vagabond", tag = "v1.0.3" } markdown = { git = "git://benbridle.com/markdown", tag = "v2.1.1" } recipe = { git = "git://benbridle.com/recipe", tag = "v1.4.0" } xflags = "0.4.0-pre.1" diff --git a/src/collect_files.rs b/src/collect_files.rs index b0e24ed..cf39ebe 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -121,6 +121,15 @@ impl Website { // Process each entry. if entry.is_directory() { if let Some(stripped) = entry.name.strip_prefix("!") { + // Track all files inside the static directory. + for child in traverse_directory(&entry).unwrap() { + let source_path = child.original_path; + let relative_path = source_path.strip_prefix(&entry.original_path).unwrap_or_else( + |_| error!("Path doesn't start with {prefix:?}: {source_path:?}")) + .as_os_str().to_string_lossy().to_string(); + let full_url = format!("{stripped}/{relative_path}"); + self.static_files.push(StaticItem { full_url, source_path }) + } let full_url = make_url_safe(stripped); self.static_dirs.push(StaticItem { full_url, source_path }); } else { @@ -299,6 +308,11 @@ 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 get_config(&self, key: &str) -> String { self.config.get(key).map(String::to_owned).unwrap_or_else(String::new) } diff --git a/src/generate_html.rs b/src/generate_html.rs index 3486352..0210986 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -188,6 +188,17 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs for paragraph in recipe.process { html!("<p>{paragraph}</p>") } 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 {:?} references nonexistent image {file:?}", page.name); + continue; + } + let large = format!("{root}images/large/{file}"); + // let small = format!("{root}images/small/{file}"); + let thumb = format!("{root}images/thumb/{file}"); + html!("<a href='{large}'><img src='{thumb}'></a>"); + }), _ => { html!("<pre class='{}'>", language); html!("{}", sanitize_text(content)); |