#!/usr/bin/env bash
# Blop
# By Drew
# uoou@posteo.net
# https://gitlab.com/uoou/blop
# GPL v.2
#######################
# Stuff you should edit
#######################
blogdir="$PWD" # no trailing slash
default_author="Drew"
date_format="%B %-d, %Y"
listing_format=".html\"> by on " # rejig this and/or remove elements to taste. You may need to change delimeter when sed is used on this later if any of the fields contain slashes
#######################
do_error () {
if [[ ! -z "$1" ]];then
echo "$1"
fi
exit
}
# Check for missing stuff and that
if ! hash pandoc 2>/dev/null; then
do_error "You need pandoc. Install it!"
fi
if [[ ! -d "$blogdir/markdown/" ]]; then
do_error "Directory: $blogdir/markdown/ does not exist. It needs to exist."
fi
count_mds=$(ls -t1 "$blogdir/markdown/"*.md 2> /dev/null | wc -l)
if [[ "$count_mds" -eq "0" ]];then
do_error "There are no markdown files in $blogdir/markdown/. There's nowt to do, pal."
fi
if [[ ! -f "$blogdir/url.txt" ]]; then
do_error "Create the file $blogdir/url.txt containing the blog's base url (without trailing slash e.g. http://example.com )"
elif [[ ! -s "$blogdir/url.txt" ]]; then
do_error "The file $blogdir/url.txt must contain the blog's base url (without trailing slash e.g. http://example.com )"
fi
#Do the things
if [[ ! -d "$blogdir/posts/" ]]; then
echo "$blogdir/posts/ doesn't exist, creating it..."
mkdir "$blogdir/posts/"
else
echo "Deleting old HTML files in $blogdir/posts/"
find "$blogdir/posts/" -name "*.html" -type f -exec rm -f {} +
rm -f "$blogdir/index.html" "$blogdir/rss.xml"
fi
url=$(cat "$blogdir/url.txt")
postlist="
\n"
temp=$(mktemp -d)
for file in $(ls -t1 "$blogdir/markdown/"*.md); do
meta=$(sed -ne '/^---$/,/^---$/p' $file | sed -e '/---/d')
title=$(echo "$meta" | sed -n 's/^title:\s\?\(.*\)$/\1/p' | sed 's/"//g')
if [[ -z "$title" ]]; then
filebase=$(basename "$file")
title=$(echo "${filebase%.md}" | sed 's/[-_]/ /g')
fi
title_nospaces=$(echo "$title" | sed 's/\s/-/g')
cdate=$(date -d "`stat -c %w "$file"`" +"%s")
mdate=$(date -d "`stat -c %y "$file"`" +"%s")
if [[ $mdate -le $cdate ]]; then
udate=$mdate
else
udate=$cdate
fi
date=$(date -d "@$udate" +"$date_format")
rssdate=$(date -d "@$udate" +"%a, %d %b %Y %H:%M:%S %z")
author=$(echo "$meta" | sed -n 's/^author:\s\?\(.*\)$/\1/p')
if [[ -z "$author" ]]; then
author=$default_author
fi
echo "Making \"$title\""
pandoc $file > "$temp"/post
sed "//r $temp/post" "$blogdir/post_template.html" | sed '//d' | sed "s//$author/g" | sed "s//$date/g" | sed "s//$title/g" > "$blogdir/posts/$title_nospaces.html"
listitem=$(echo "$listing_format" | sed -e "s//$title_nospaces/g" -e "s//$title/g" -e "s@@$date@g" -e "s//$author/g")
postlist="$postlist- $listitem
\n"
# prepare rss
echo -e "- \n$title\n$url/$title_nospaces.html\n$rssdate\n
\n\n" >> $temp/rss
done
echo "Creating index.html"
postlist="$postlist
"
echo -e "$postlist" > "$temp"/list
sed "//r $temp/list" "$blogdir/index_template.html" | sed '//d' > "$blogdir/index.html"
echo "Creating RSS"
sed "//r $temp/rss" "$blogdir/rss_template.xml" | sed "s@@$url@g" | sed '//d' > "$blogdir/rss.xml" # we use @ as delimeter here because replacement text contains slashes. If your sites url contains an @ for some reason, use a delimeter that doesn't appear in the url
echo "Done"