################################################################################################################################################################
#
# SBWG - steeph's bash website generator
# 2020-01-05
# Version 0.4.19
# https://log.steeph.de/entries/SBWG.html
#
################################################################################################################################################################
#!/bin/bash
# the directory that contains the source files/file structure - no trailing slash - can be overwritten with option -i
shellbase="/home/stph/weblog"
# base directory of the webserver - no trailing slash - "/" for web root
webbase="/"
# default output directory (where the generated web site is written to) - can be overwritten with option -o
odir="$shellbase/html"
# default width/height for image gallery thumbnails in pixels - I say default, but there's currently no option to overwrite this (except with styles).
thumbsize="250"
prepare() {
echo "" > "$shellbase/tagslist" # Empty the list of tags found in the previous run (if any)
rm "$shellbase"/tags/* # Remove all tag pages created in the previous run (if any)
IFS=$'\n'
echo "Finished preparations."
}
copy_styles() {
mkdir -p "$odir/css"
cp "$shellbase/style.css" "$odir/css/style.css"
echo "Finished copying stylesheets."
}
copy_files() {
mkdir -p "$odir/files"
shopt -s dotglob # So that dotfiles (such as .htaccess) are copied by cp.
cp "$shellbase"/files/* "$odir/files/"
cp "$shellbase/logo.svg" "$odir/logo.svg"
echo "Finished copying files and downloads."
}
# CREATE IMAGE GALLERY PAGES
gen_galleries() {
cp -r "$shellbase/galleries/" "$odir/" # Copy all the stuff. Nothing but supported image files should be in there.
shopt -s nocaseglob # Should already be set, but to be save, set it again. (Because .jpg/.JPG etc.)
for gallerypath in $(ls -td "$shellbase"/galleries/*/); do
gallery=$(basename $gallerypath)
images=""
images=$(ls -td "$gallerypath"*.{jpeg,jpg,png} 2> /dev/null)
if [ -n "$images" ]; # If $images is not still empty
then # (In other words: if there is at least one file with one of the extensions)
cat "$shellbase/head" "$shellbase/header" "$shellbase/sidebar" > "$odir/galleries/$gallery.html"
for image in $images; do
img=$(basename "$image")
# echo "$gallery: $img"
mkdir -p "$odir/galleries/$gallery/thumbnails/"
convert "$image" -resize "$thumbsize"x"$thumbsize" "$odir/galleries/$gallery/thumbnails/$img"
echo "" >> "$odir/galleries/$gallery.html"
done
cat "$shellbase/footer" >> "$odir/galleries/$gallery.html"
else # If there is no file with one of the supported extensions in the directory
echo "The following gallery does not contain any supported image files: $gallery"
fi
done
echo "Finished generating image gallery pages."
}
# CREATE TAGSLIST
gen_tagslist() {
for entry in $(ls -t "$shellbase/entries/"); do # Look into each entry source file
tags=$(sed -n '/-->/q;p' "$shellbase/entries/$entry"|tail -n +3) # Tag lines start at line 3 and end before the HTML comment is closed.
for tag in $tags; do
echo "$tag" >> "$shellbase/tagslist" # Add each found tag to the tagslist file
done
done
sort -fn "$shellbase/tagslist" | uniq > "$shellbase/tagslist_sorted" # Sort the found tags and remove duplicates
echo "Finished generating tagslist."
}
# CREATE SIDEBAR
gen_sidebar() {
sed -i '0,/^cat:.*/s/^cat:.*/Categories\n&/' "$shellbase/tagslist_sorted" # Insert "Categories" before the first occurence of "cat:".
sed -i '0,/^top:.*/s/^top:.*/Topics\n&/' "$shellbase/tagslist_sorted" # Insert "Topics" before the first occurence of "top:".
sed -i '0,/^lang:.*/s/^lang:.*/By Language\n&/' "$shellbase/tagslist_sorted" # Insert "By Language" before the first occurence of "lang:".
# Turn the sorted list into HTML code for the sidebar
echo "" >> $shellbase/sidebar
echo "Finished generating sidebar file."
}
# CREATE TAGPAGES
gen_tagpages() {
for entry in $(ls -t "$shellbase/entries/"); do # Look into each entry source file.
tags=$(sed -n '/-->/q;p' "$shellbase/entries/$entry"|tail -n +3) # Tag lines start at line 3 and end before the HTML comment is closed.
title="$(sed -n '2p' $shellbase/entries/$entry)" # The entry's title is the second line of the source file.
# For each tag found in a source file, add the source file's title and
for tag in $tags; do # content to the corrosponding tagpage.
echo "" >> "$shellbase/tags/$tag"
# print entry content and stuff to tagpage
echo "
$title
" >> "$shellbase/tags/$tag"
echo >> "$shellbase/tags/$tag"
cat "$shellbase/entries/$entry" >> "$shellbase/tags/$tag"
echo >> "$shellbase/tags/$tag"
echo >> "$shellbase/tags/$tag"
done
done
mkdir -p "$odir/tags"
# Create a HTML tagpages in the output directory from each source tagpage.
for tagpage in $(ls $shellbase/tags/); do
cat "$shellbase/head" "$shellbase/header" "$shellbase/sidebar" > "$odir/tags/$tagpage.html"
echo "