File: topgallery.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2020-2025 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # topgallery [options...] [title...] [folder...]
  27 #
  28 #
  29 # Emit a self-contained picture gallery web-page using all top-level pictures
  30 # found in the folder given, or the current folder by default. The page title
  31 # is optional: if the first argument is an existing folder, a default title
  32 # is used instead.
  33 #
  34 # The help option is `-h`, `--h`, `-help`, or `--help`.
  35 
  36 
  37 # handle leading options
  38 case "$1" in
  39     -h|--h|-help|--help)
  40         awk '/^# +topgallery /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  41         exit 0
  42     ;;
  43 esac
  44 
  45 # get the page title, and the folder with the pictures
  46 if [ $# -eq 1 ] && [ -d "$1" ]; then
  47     # title="$(realpath .) - Pictures"
  48     title="Picture Gallery"
  49     folder="$(realpath "$1")"
  50 else
  51     title="${1:-Picture Gallery}"
  52     [ $# -gt 0 ] && shift
  53     folder="$(realpath "${1:-.}")"
  54     if [ ! -d "${folder}" ]; then
  55         printf "\e[31mno folder named \"%s\"\e[0m\n" "${folder}"
  56         exit 1
  57     fi
  58 fi
  59 
  60 # HTML-escape the page title
  61 title="$(echo "${title}" | sed 's-\&-\&amp;-g; s-<-\&lt;-g; s->-\&gt-g')"
  62 
  63 cat << 'EOF'
  64 <!DOCTYPE html>
  65 <html lang="en">
  66 
  67 <head>
  68     <meta charset="UTF-8">
  69     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  70     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  71     <link rel="icon" href="data:,">
  72 EOF
  73 
  74 # emit page title
  75 printf "    <title>%s</title>\n" "${title}"
  76 
  77 cat << 'EOF'
  78     <style>
  79         body {
  80             margin: auto;
  81         }
  82 
  83         img {
  84             width: 8rem;
  85             box-sizing: border-box;
  86             background-color: white;
  87         }
  88 
  89     </style>
  90 </head>
  91 
  92 <body>
  93 EOF
  94 
  95 find "${folder}" -maxdepth 1 -type f | awk '
  96     tolower($0) ~ /\.(gif|jpeg|jpg|png|webp)$/ {
  97         gsub(/%/, "%25")
  98         gsub(/"/, "%22")
  99         path = "file://" $0
 100         printf "<a href=\"%s\"><img src=\"%s\"></img></a>\n", path, path
 101     }
 102 '
 103 
 104 cat << 'EOF'
 105 </body>
 106 
 107 </html>
 108 EOF