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