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