stevengibone.com/gmirator.sh

159 lines
2.9 KiB
Bash
Executable file

#! /usr/bin/env bash
source ./gmirator.conf
COMMAND=$1
PARAM=$2
make_gemlogs_index() {
rm articles.gmi.temp
echo "generating articles.gmi.temp"
ls $LGMI/gemlog | grep -v 'index' | sort -r \
| while read fname; do
local date=$(echo "$fname" | sed "s/_.*//")
local title=$(head -1 $LGMI/gemlog/$fname | sed "s/# //")
echo "=> gemlog/$fname $date - $title" >> $WORKDIR/articles.gmi.temp
done
echo "generating gemlog/index.gmi"
sed -i -e "/^## logs/,//{
/^#/!d
}" -e "/^## logs/{
a
r $WORKDIR/articles.gmi.temp
}" $LGMI/gemlog/index.gmi
echo "generating index.gmi"
printf '
=> tinylog.gmi tinylogs go here
=> gemlog/index.gmi full gemlogs here
\n%s' "$(head $WORKDIR/articles.gmi.temp)" \
| cat $WORKDIR/in.gmi - > $LGMI/index.gmi
echo "" >> $LGMI/index.gmi
echo "DONE :)"
}
gmi2html() {
local in_list=no
local in_preformated=no
while read -r line; do
if [[ "$in_list" == yes ]]; then
if [[ "$line" == '* '* ]]; then
printf "<li>%s</li>\n" "${line/\* /}"
continue
else
in_list=no
printf "</ul>\n"
fi
elif [[ "$in_preformated" == yes ]]; then
if [[ "$line" == '```'* ]]; then
in_preformated=no
printf "</pre>\n"
else
printf "%s\n" "$line"
fi
continue
fi
case "$line" in
'')
;;
'* '*)
in_list=yes
printf "<ul>\n<li>%s</li>\n" "${line/\* }"
;;
'# '*)
printf "<h1>%s</h1>\n" "$line"
;;
'## '*)
printf "<h2>%s</h2>\n" "$line"
;;
'### '*)
printf "<h3>%s</h3>\n" "$line"
;;
'=> '*)
fname=$(echo "$line" | sed 's/=> \([^ ]*\).*/\1/')
fname=${fname/.gmi/.html}
desc=$(echo "$line" | sed 's/=> [^ ]* \(.*\)/=> \1/')
printf "<a href=\"%s\">%s</a><br>\n" "$fname" "$desc"
;;
'```'*)
in_preformated=yes
printf "<pre>\n"
;;
*)
printf "<p>%s</p>\n" "$line"
;;
esac
done
}
make_html() {
while read -r file; do
local dest=${file/.gmi/.html}
dest=${dest/\/content\//\/html-output\/}
printf "... %s\n" $dest
cat $HTML_HEADER > $dest
gmi2html < "$file" >> $dest
cat $HTML_FOOTER >> $dest
done < <(find $LGMI -type f -name \*.gmi)
echo "Done generating HTML"
echo "Copying statis assets"
cp $ASSETS/* $LHTML
}
synchronise() {
if [[ $PARAM == "html" ]]; then
echo "Synchronising HTML content with remote"
rsync -aP -e "ssh -p 1312" $LHTML $RHTML
elif [[ $PARAM == "gmi" ]]; then
echo "Synchtonising GMI content with remote"
rsync -aP -e "ssh -p 1312" $LGMI $RGMI
else
echo "Synchtonising GMI and HTML content with remote"
rsync -aP -e "ssh -p 1312" $LHTML $RHTML
rsync -aP -e "ssh -p 1312" $LGMI $RGMI
fi
}
generate() {
case $1 in
gmi)
make_gemlogs_index
;;
html)
make_html
;;
esac
}
usage() {
echo "\
Usage: $0 <COMMAND>
Commands:
gen generates output
sync synchronises files with server with rsync
"
}
main() {
case $COMMAND in
gen)
generate $PARAM
;;
sync)
synchronise
;;
*)
usage
;;
esac
}
main