sgg

static gallery generator written in shell
git clone http://git.hanabi.in/repos/sgg.git
Log | Files | Refs

sgg (3249B)


      1 #!/bin/sh -e
      2 
      3 # Copyright (c) 2021 acagastya <me+code@hanabi.in>
      4 # This software is licensed under GNU AGPLv3.
      5 # See LICENSE file for what you can do with this.
      6 
      7 main() {
      8   test -n "$1" || usage # img dir was passed?
      9   test -n "$2" || usage # consts dir was passed?
     10 
     11   test -d "$1" || no_dir "$1" # $1 is a dir?
     12   test -d "$2" || no_dir "$2" # $2 is a dir?
     13 
     14   src=$1
     15   consts=$2
     16 
     17   test -e $src/avtaar.jpg && rm $src/avtaar.jpg # rm avtaar if exists; readd after building index.html
     18   test -e $src/index.html && rm $src/index.html # rm index.html if exists; readd after building index.html
     19   test -e $src/rss.xml && rm $src/rss.xml # rm rss.xml if exists; readd after building index.html
     20   test -e $src/favicon.png && rm $src/favicon.png # rm favicon.png if exists; readd after building index.html
     21   test -e $src/style.css && rm $src/style.css # rm style.css if exists; readd after building index.html
     22 
     23   files=$(find $src -type f | sed "s/${src}//g" | sort -ru) # get all files, rm "src/" are order in reverse
     24   touch index.html
     25   touch rss.xml
     26   cat $consts/_header.html >> index.html
     27   cat $consts/_rss-header.xml >> rss.xml
     28   for i in $files; do
     29     title=$(echo $i | sed "s/\///;s/\..*$//") # for rss
     30     ymd=$(echo $title | cut -c -10) # for rss
     31     formattedDate=$(date -j -f "%Y/%m/%d" $ymd "+%a, %d %b %Y %T %Z%z") # for BSD date
     32     # formattedDate=$(date -d $ymd "+%a, %d %b %Y %T %Z%z") # for GNU date
     33     rssEntry='<item><title>'$title'</title><link>___http://example.com___'$i'</link><description>New photo added on '$formattedDate'.</description><pubDate>'$formattedDate'</pubDate><guid>___http://example.com___'$i'</guid></item>'
     34     echo $rssEntry >> rss.xml
     35     case $i in
     36       *.mp4) # handle video.mp4
     37         item='       <li><video class="gallery" controls><source src="'$i'"></video></li>'
     38         ;;
     39       *.jpg) # handle img.jpg
     40         item='       <li><a target="_blank" rel="noopener noreferrer" href="'$i'"><img class="gallery" src="'$i'"/></a></li>'
     41         ;;
     42       *) # ignore the rest
     43         continue
     44         ;;
     45     esac
     46     echo $item >> index.html
     47   done
     48   cat $consts/_footer.html >> index.html
     49   cat $consts/_rss-footer.xml >> rss.xml
     50   year=$(date +"%Y") # for the purpose of copyright
     51   now=$(date "+%a, %d %b %Y %T %Z%z") # for build date
     52   sed -i "" "s/copy;/copy; $year /" index.html # for BSD sed
     53   sed -i "" "s|<copyright></copyright>|<copyright>$year ___RSS-COPYRIGHT___</copyright>|" rss.xml # for BSD sed
     54   sed -i "" "s|<lastBuildDate></lastBuildDate>|<lastBuildDate>$now</lastBuildDate>|" rss.xml # for BSD sed
     55   # sed -i "s|<copyright></copyright>|<copyright>$year ___RSS-COPYRIGHT___</copyright>|" rss.xml # for GNU sed
     56   # sed -i "s/copy;/copy; $year /" index.html # for GNU sed
     57   # sed -i "s|<lastBuildDate></lastBuildDate>|<lastBuildDate>$now</lastBuildDate>|" rss.xml # for GNU sed
     58 
     59   mv index.html $src
     60   mv rss.xml $src/rss.xml
     61 
     62   test -e $consts/style.css && cp $consts/style.css $src/style.css
     63   test -e $consts/avtaar.jpg && cp $consts/avtaar.jpg $src/avtaar.jpg
     64   test -e $consts/favicon.png && cp $consts/favicon.png $src/favicon.png
     65   exit 0
     66 }
     67 
     68 usage() {
     69   echo "usage: ${0##*/} src consts" >&2
     70   exit 1
     71 }
     72 
     73 no_dir() {
     74   echo "${0##*/}: $1: No such directory" >&2
     75   exit 2
     76 }
     77 
     78 main "$@"