html_gallery_newest.sh (2247B)
1 #!/bin/sh 2 set -e 3 4 version=0.1.0 5 6 show_help() { 7 echo "usage: ${0##*/} [OPTIONS] DIR ..." 8 echo "outputs a html page with the newest image file from each DIR." 9 echo 10 echo "OPTIONS are one or more of the following:" 11 echo " -h, --help show this message" 12 echo " -v, --version show version and license information" 13 echo " -V, --verbose show verbose information" 14 echo " -t, --title <value> set page title" 15 echo " -s, --stylesheet <path> embed custom css stylesheet" 16 echo " -- do not consider any following args as options" 17 } 18 19 show_version() { 20 echo "${0##*/} version $version" 21 echo "Licensed under the ISC License" 22 echo "written by Anders Damsgaard, anders@adamsgaard.dk" 23 echo "https://src.adamsgaard.dk/dotfiles" 24 } 25 26 die() { 27 printf '%s\n' "$1" >&2 28 exit 1 29 } 30 31 generate_html_header() { 32 printf "<!DOCTYPE html>\n" 33 printf "<html lang=\"en\">\n" 34 printf "<meta charset=\"utf-8\">\n" 35 printf "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" 36 if [ -n "$title" ]; then 37 printf "<title>%s</title>\n" "$title" 38 fi 39 if [ -n "$stylesheet" ]; then 40 printf "<style>\n%s</style>\n" "$(cat "$stylesheet")" 41 fi 42 printf "<article>\n" 43 if [ -n "$title" ]; then 44 printf " <header>\n" 45 printf " <h1>%s</h1>\n" "$title" 46 printf " </header>\n\n" 47 fi 48 } 49 50 generate_html_footer() { 51 printf "</article>\n" 52 printf "</html>\n" 53 } 54 55 get_newest_image() { 56 ls -1t "$1" | head -1 57 } 58 59 generate_html() { 60 local _i, _dir 61 generate_html_header 62 for _dir in "$@"; do 63 _dir="${_dir%/}" 64 _i="$(get_newest_image "$_dir")" 65 printf " <h2>%s</h2>\n" "$_dir" 66 printf " <a href=\"%s.html\"><img src=\"%s/%s\" alt=\"%s\"/></a>\n" \ 67 "$_dir" "$_dir" "$_i" "$_dir" 68 done 69 generate_html_footer 70 } 71 72 verbose=0 73 while :; do 74 case "$1" in 75 -h|-\?|--help) 76 show_help 77 exit 0 78 ;; 79 -v|--version) 80 show_version 81 exit 0 82 ;; 83 -V|--verbose) 84 verbose=1 85 ;; 86 -t|--title) 87 title="$2" 88 shift 89 ;; 90 -s|--stylesheet) 91 stylesheet="$2" 92 shift 93 ;; 94 --) # end all options 95 shift 96 break 97 ;; 98 -?*) 99 die 'Error: Unknown option specified' 100 ;; 101 *) # No more options 102 break 103 esac 104 shift 105 done 106 107 if [ $# -lt 1 ]; then 108 show_help() 109 exit 1 110 fi 111 112 generate_html "$@"