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 # 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 case "$1" in 38 -h|--h|-help|--help) 39 awk '/^# +topgallery /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 40 exit 0 41 ;; 42 esac 43 44 [ "$1" = '--' ] && shift 45 46 # get the page title, and the folder with the pictures 47 if [ $# -eq 1 ] && [ -d "$1" ]; then 48 # title="$(realpath .) - Pictures" 49 title="Picture Gallery" 50 folder="$(realpath "$1")" 51 else 52 title="${1:-Picture Gallery}" 53 [ $# -gt 0 ] && shift 54 folder="$(realpath "${1:-.}")" 55 if [ ! -d "${folder}" ]; then 56 printf "no folder named \"%s\"\n" "${folder}" 57 exit 1 58 fi 59 fi 60 61 # HTML-escape the page title 62 title="$(echo "${title}" | sed 's-\&-\&-g; s-<-\<-g; s->-\>-g')" 63 64 cat << 'EOF' 65 <!DOCTYPE html> 66 <html lang="en"> 67 68 <head> 69 <meta charset="UTF-8"> 70 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 71 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 72 <link rel="icon" href="data:,"> 73 EOF 74 75 # emit page title 76 printf " <title>%s</title>\n" "${title}" 77 78 cat << 'EOF' 79 <style> 80 body { 81 margin: auto; 82 } 83 84 img { 85 width: 8rem; 86 box-sizing: border-box; 87 background-color: white; 88 } 89 90 </style> 91 </head> 92 93 <body> 94 EOF 95 96 find "${folder}" -maxdepth 1 -type f | awk ' 97 tolower($0) ~ /\.(gif|jpeg|jpg|png|webp)$/ { 98 gsub(/%/, "%25") 99 gsub(/"/, "%22") 100 path = "file://" $0 101 printf "<a href=\"%s\"><img src=\"%s\"></img></a>\n", path, path 102 } 103 ' 104 105 cat << 'EOF' 106 </body> 107 108 </html> 109 EOF