stapix

Yet another static page generator for photo galleries

Commit
7d79b58a568bf5696c8958abb9d908557fc8aa24
Parent
ef94eff8bd5a1527af5dd85f3863f2935ae29ce8
Author
Pablo <pablo-escobar@riseup.net>
Date

Added code to render the alt attribute of images

Also updated the code to parse the config file to parse the alt attribute as well

Diffstat

2 files changed, 20 insertions, 12 deletions

Status File Name N° Changes Insertions Deletions
Modified src/main.rs 17 9 8
Modified src/types.rs 15 11 4
diff --git a/src/main.rs b/src/main.rs
@@ -25,16 +25,16 @@ fn main() -> io::Result<()> {
         },
     };
 
-    let f = File::open(config);
+    let f = File::open(&config);
     match f.map(serde_yaml::from_reader::<_, Vec<PictureInfo>>) {
         // Error opening the config file
         Err(err) => {
-            eprintln!("ERROR: {err}");
+            eprintln!("ERROR: Couldn't open {config:?}: {err}");
             Err(err)
         },
         // Error parsing the config file
         Ok(Err(err)) => {
-            eprintln!("ERROR: {err}");
+            eprintln!("ERROR: Couldn't parse {config:?}: {err}");
             exit(1)
         },
         Ok(Ok(pic_infos)) => render_gallery(pic_infos),
@@ -93,8 +93,8 @@ fn render_index(pic_infos: &Vec<PictureInfo>) -> io::Result<()> {
         writeln!(f, "<a aria-label=\"{name}\" href=\"/photos/{name}.html\">",
                  name = Escaped(&pic.file_name))?;
         // TODO: Render a compressed thumbnail instead
-        // TODO: Render alt text
-        writeln!(f, "<img src=\"/assets/photos/{name}\">",
+        writeln!(f, "<img alt=\"{alt}\" src=\"/assets/photos/{name}\">",
+                 alt = Escaped(&pic.alt),
                  name = Escaped(&pic.file_name))?;
         writeln!(f, "</a>\n</li>")?;
     }
@@ -134,11 +134,12 @@ fn render_pic_page(pic: &PictureInfo) -> io::Result<()> {
 
     writeln!(f, "<body>")?;
     write_nav(&mut f)?;
+
     writeln!(f, "<main>")?;
     writeln!(f, "<figure>")?;
     writeln!(f, "<div id=\"picture-container\">")?;
-    // TODO: Add alt text
-    writeln!(f, "<img src=\"/assets/photos/{file_name}\">",
+    writeln!(f, "<img alt=\"{alt}\" src=\"/assets/photos/{file_name}\">",
+             alt = Escaped(&pic.alt),
              file_name = Escaped(&pic.file_name))?;
     writeln!(f, "</div>")?;
     writeln!(f, "</figure>")?;
@@ -152,7 +153,7 @@ fn render_pic_page(pic: &PictureInfo) -> io::Result<()> {
 fn write_nav(f: &mut File) -> io::Result<()> {
     writeln!(f, "<header>")?;
     writeln!(f, "<nav>")?;
-    writeln!(f, "<img aria-hidden=\"true\" width=\"24\" height=\"24\" src=\"/assets/icon.svg\">")?;
+    writeln!(f, "<img aria-hidden=\"true\" alt=\"Website icon\" width=\"24\" height=\"24\" src=\"/assets/icon.svg\">")?;
     writeln!(f, "<a href=\"/index.html\">photos.pablopie.xyz</a>")?;
     writeln!(f, "</nav>")?;
     writeln!(f, "</header>")
diff --git a/src/types.rs b/src/types.rs
@@ -1,4 +1,4 @@
-use std::path::{PathBuf};
+use std::path::PathBuf;
 use std::fmt::{self, Display};
 use serde::{Deserialize, de::{Deserializer, Error, Unexpected}};
 
@@ -6,6 +6,7 @@ use serde::{Deserialize, de::{Deserializer, Error, Unexpected}};
 pub struct PictureInfo {
     pub path: PathBuf,
     pub file_name: String,
+    pub alt: String,
 }
 
 pub struct Escaped<'a>(pub &'a str);
@@ -15,14 +16,20 @@ impl<'de> Deserialize<'de> for PictureInfo {
     where D: Deserializer<'de>
     {
         #[derive(Deserialize)]
-        struct Info { path: String, }
-        let Info { path: path_str } = Info::deserialize(deserializer)?;
+        struct Info { path: String, alt: String, }
+        let Info { path: path_str, alt } = Info::deserialize(deserializer)?;
 
         let mut path = PathBuf::new();
         path.push(path_str.clone());
 
         if let Some(file_name) = path.file_name().and_then(|s| s.to_str()) {
-            Ok(Self { path: path.clone(), file_name: String::from(file_name) })
+            Ok(
+                Self {
+                    path: path.clone(),
+                    alt: alt.clone(),
+                    file_name: String::from(file_name)
+                }
+            )
         } else {
             Err(
                 D::Error::invalid_value(