stapix

Yet another static page generator for photo galleries

Commit
3aa8447ae0df0e8cfa086b69ca44c25df1710ac0
Parent
d80dca9cf14241731b8b125db2f31371ed7fac03
Author
Pablo <pablo-escobar@riseup.net>
Date

Added code too render the pages on individual photos

Diffstat

1 file changed, 84 insertions, 31 deletions

Status File Name N° Changes Insertions Deletions
Modified src/main.rs 115 84 31
diff --git a/src/main.rs b/src/main.rs
@@ -68,28 +68,11 @@ fn render_index(pic_infos: &Vec<PictureInfo>) -> io::Result<()> {
     let mut f = File::create("index.html")?;
 
     writeln!(f, "<!DOCTYPE html>")?;
-    writeln!(f, "<!-- This program is free software: you can redistribute it and/or modify")?;
-    writeln!(f, "     it under the terms of the GNU General Public License as published by")?;
-    writeln!(f, "     the Free Software Foundation, either version 3 of the License, or")?;
-    writeln!(f, "     (at your option) any later version.\n")?;
-    writeln!(f, "     This program is distributed in the hope that it will be useful,")?;
-    writeln!(f, "     but WITHOUT ANY WARRANTY; without even the implied warranty of")?;
-    writeln!(f, "     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the")?;
-    writeln!(f, "     GNU General Public License for more details.\n")?;
-    writeln!(f, "     You should have received a copy of the GNU General Public License")?;
-    writeln!(f, "     along with this program. If not, see <https://www.gnu.org/licenses/>. -->")?;
-
+    print_license(&mut f)?;
     writeln!(f, "<html lang=\"en\">")?;
     writeln!(f, "<head>")?;
-
     writeln!(f, "<title>Pablo&apos;s Photo Gallery</title>")?;
-
-    writeln!(f, "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")?;
-    writeln!(f, "<meta name=\"author\" content=\"Pablo\">")?;
-    writeln!(f, "<meta name=\"copyright\" content=\"GPLv2\">")?;
-    writeln!(f, "<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">")?;
-    writeln!(f, "<link rel=\"icon\" href=\"/assets/favicon.ico\" type=\"image/x-icon\" sizes=\"16x16 24x24 32x32\">")?;
-    writeln!(f, "<link rel=\"stylesheet\" href=\"/styles.css\">")?;
+    print_head(&mut f)?;
 
     for pic in pic_infos {
         // TODO: Preload a compressed thumbnail instead
@@ -101,13 +84,8 @@ fn render_index(pic_infos: &Vec<PictureInfo>) -> io::Result<()> {
     writeln!(f, "</head>")?;
 
     writeln!(f, "<body>")?;
+    print_nav(&mut f)?;
     writeln!(f, "<main>")?;
-    writeln!(f, "<header>")?;
-    writeln!(f, "<nav>")?;
-    writeln!(f, "<img aria-hidden=\"true\" width=\"24\" height=\"24\" src=\"/assets/icon.svg\">")?;
-    writeln!(f, "<a href=\"/index.html\">photos.pablopie.xyz</a>")?;
-    writeln!(f, "</nav>")?;
-    writeln!(f, "</header>")?;
     writeln!(f, "<ul id=\"gallery\">")?;
 
     for pic in pic_infos {
@@ -123,15 +101,90 @@ fn render_index(pic_infos: &Vec<PictureInfo>) -> io::Result<()> {
 
     writeln!(f, "</ul>")?;
     writeln!(f, "</main>")?;
+
+    print_footer(&mut f)?;
+    writeln!(f, "</body>")?;
+    writeln!(f, "</html>")
+}
+
+fn render_pic_page(pic: &PictureInfo) -> io::Result<()> {
+    let mut path = PathBuf::new();
+    path.push("./photos/");
+    path.push(pic.file_name.clone() + ".html");
+
+    let mut f = match File::create(&path) {
+        Ok(file) => file,
+        Err(err) => {
+            eprintln!("ERROR: Could not open file {path:?}: {err}");
+            exit(1);
+        }
+    };
+
+    writeln!(f, "<!DOCTYPE html>")?;
+    print_license(&mut f)?;
+    writeln!(f, "<html lang=\"en\">")?;
+    writeln!(f, "<head>")?;
+    writeln!(f, "<title>Pablo&apos;s Photo Gallery &dash; {name}</title>",
+             name = Escaped(&pic.file_name))?;
+    print_head(&mut f)?;
+    writeln!(f,
+             "<link as=\"image\" rel=\"preload\" href=\"/assets/photos/{n}\">",
+             n = Escaped(&pic.file_name))?;
+    writeln!(f, "</head>")?;
+
+    writeln!(f, "<body>")?;
+    print_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}\">",
+             file_name = Escaped(&pic.file_name))?;
+    writeln!(f, "</div>")?;
+    writeln!(f, "</figure>")?;
+
+    writeln!(f, "</main>")?;
+
+    print_footer(&mut f)?;
+    writeln!(f, "</body>")?;
+    writeln!(f, "</html>")
+}
+
+fn print_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, "<a href=\"/index.html\">photos.pablopie.xyz</a>")?;
+    writeln!(f, "</nav>")?;
+    writeln!(f, "</header>")
+}
+
+fn print_footer(f: &mut File) -> io::Result<()> {
     writeln!(f, "<footer>")?;
     writeln!(f, "made with 💛 by <a role=\"author\" href=\"https://pablopie.xyz\">@pablo</a>")?;
-    writeln!(f, "</footer>")?;
-    writeln!(f, "</body>")?;
-    writeln!(f, "</html>")?;
+    writeln!(f, "</footer>")
+}
 
-    Ok(())
+/// Prints the common head elements to a given file
+fn print_head(f: &mut File) -> io::Result<()> {
+    writeln!(f, "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">")?;
+    writeln!(f, "<meta name=\"author\" content=\"Pablo\">")?;
+    writeln!(f, "<meta name=\"copyright\" content=\"GPLv2\">")?;
+    writeln!(f, "<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">")?;
+    writeln!(f, "<link rel=\"icon\" href=\"/assets/favicon.ico\" type=\"image/x-icon\" sizes=\"16x16 24x24 32x32\">")?;
+    writeln!(f, "<link rel=\"stylesheet\" href=\"/styles.css\">")
 }
 
-fn render_pic_page(_pic: &PictureInfo) -> io::Result<()> {
-    todo!()
+/// Prints a HTML comment with GPL licensing info
+fn print_license(f: &mut File) -> io::Result<()> {
+    writeln!(f, "<!-- This program is free software: you can redistribute it and/or modify")?;
+    writeln!(f, "     it under the terms of the GNU General Public License as published by")?;
+    writeln!(f, "     the Free Software Foundation, either version 3 of the License, or")?;
+    writeln!(f, "     (at your option) any later version.\n")?;
+    writeln!(f, "     This program is distributed in the hope that it will be useful,")?;
+    writeln!(f, "     but WITHOUT ANY WARRANTY; without even the implied warranty of")?;
+    writeln!(f, "     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the")?;
+    writeln!(f, "     GNU General Public License for more details.\n")?;
+    writeln!(f, "     You should have received a copy of the GNU General Public License")?;
+    writeln!(f, "     along with this program. If not, see <https://www.gnu.org/licenses/>. -->")
 }