File: topgallery.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 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 # Emit a self-contained picture gallery web-page using all top-level pictures
  29 # found in the folder given, or the current folder by default. The page title
  30 # is optional: if the first argument is an existing folder, a default title
  31 # is used instead.
  32 #
  33 # The options are, available both in single and double-dash versions
  34 #
  35 #   -h, -help    show this help message
  36 
  37 
  38 case "$1" in
  39     -h|--h|-help|--help)
  40         awk '/^# +topgallery /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  41         exit 0
  42     ;;
  43 
  44     -) ;;
  45 
  46     --) shift ;;
  47 
  48     -*)
  49         printf "unsupported option '%s'\n" "$1" >&2
  50         exit 1
  51     ;;
  52 esac
  53 
  54 # get the page title, and the folder with the pictures
  55 if [ $# -eq 1 ] && [ -d "$1" ]; then
  56     # title="$(realpath .) - Pictures"
  57     title="Picture Gallery"
  58     folder="$(realpath "$1")"
  59 else
  60     title="${1:-Picture Gallery}"
  61     [ $# -gt 0 ] && shift
  62     folder="$(realpath "${1:-.}")"
  63     if [ ! -d "${folder}" ]; then
  64         printf "no folder named \"%s\"\n" "${folder}"
  65         exit 1
  66     fi
  67 fi
  68 
  69 # HTML-escape the page title
  70 title="$(echo "${title}" | sed 's-\&-\&amp;-g; s-<-\&lt;-g; s->-\&gt-g')"
  71 
  72 cat << 'EOF'
  73 <!DOCTYPE html>
  74 <html lang="en">
  75 
  76 <head>
  77     <meta charset="UTF-8">
  78     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  79     <meta http-equiv="X-UA-Compatible" content="ie=edge">
  80     <link rel="icon" href="data:,">
  81 EOF
  82 
  83 # emit page title
  84 printf "    <title>%s</title>\n" "${title}"
  85 
  86 cat << 'EOF'
  87     <style>
  88         body {
  89             margin: auto;
  90         }
  91 
  92         img {
  93             width: 8rem;
  94             box-sizing: border-box;
  95             background-color: white;
  96         }
  97 
  98     </style>
  99 </head>
 100 
 101 <body>
 102 EOF
 103 
 104 find "${folder}" -maxdepth 1 -type f | awk '
 105     tolower($0) ~ /\.(gif|jpeg|jpg|png|webp)$/ {
 106         gsub(/%/, "%25")
 107         gsub(/"/, "%22")
 108         path = "file://" $0
 109         printf "<a href=\"%s\"><img src=\"%s\"></img></a>\n", path, path
 110     }
 111 '
 112 
 113 cat << 'EOF'
 114 </body>
 115 
 116 </html>
 117 EOF