blop/blop

98 lines
3.6 KiB
Plaintext
Raw Normal View History

2019-09-25 09:42:22 +00:00
#!/usr/bin/env bash
2019-09-26 05:26:09 +00:00
# Blop
# By Drew
# uoou@posteo.net
# https://gitlab.com/uoou/blop
# GPL v.2
2019-09-25 09:42:22 +00:00
#######################
# Stuff you should edit
#######################
2019-09-26 05:26:09 +00:00
blogdir="$PWD" # no trailing slash
2019-09-25 09:42:22 +00:00
default_author="Drew"
date_format="%B %-d, %Y"
2019-09-26 05:56:52 +00:00
listing_format="<a href=\"posts/<!--title-nospaces-->.html\"><!--title--></a> by <!--author--> on <!--date-->" # 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
2019-09-25 09:42:22 +00:00
#######################
do_error () {
2019-09-26 05:26:09 +00:00
if [[ ! -z "$1" ]];then
echo "$1"
2019-09-25 09:42:22 +00:00
fi
exit
}
2019-09-26 05:26:09 +00:00
# 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="<ul id=\"postlist\">\n"
temp=$(mktemp -d)
for file in $(ls -t1 "$blogdir/markdown/"*.md); do
2019-09-25 09:42:22 +00:00
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
2019-09-26 05:26:09 +00:00
filebase=$(basename "$file")
title=$(echo "${filebase%.md}" | sed 's/[-_]/ /g')
2019-09-25 09:42:22 +00:00
fi
title_nospaces=$(echo "$title" | sed 's/\s/-/g')
2019-09-26 05:26:09 +00:00
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 "/<!--post-->/r $temp/post" "$blogdir/post_template.html" | sed '/<!--post-->/d' | sed "s/<!--author-->/$author/g" | sed "s/<!--date-->/$date/g" | sed "s/<!--title-->/$title/g" > "$blogdir/posts/$title_nospaces.html"
listitem=$(echo "$listing_format" | sed -e "s/<!--title-nospaces-->/$title_nospaces/g" -e "s/<!--title-->/$title/g" -e "s@<!--date-->@$date@g" -e "s/<!--author-->/$author/g")
postlist="$postlist<li>$listitem</li>\n"
# prepare rss
echo -e "<item>\n<title>$title</title>\n<guid>$url/$title_nospaces.html</guid>\n<pubDate>$rssdate</pubDate><description><![CDATA[$(cat $temp/post)]]></description>\n</item>\n\n" >> $temp/rss
2019-09-25 09:42:22 +00:00
done
2019-09-26 05:26:09 +00:00
echo "Creating index.html"
postlist="$postlist</ul>"
echo -e "$postlist" > "$temp"/list
sed "/<!--post list-->/r $temp/list" "$blogdir/index_template.html" | sed '/<!--post list-->/d' > "$blogdir/index.html"
echo "Creating RSS"
sed "/<!--rss-->/r $temp/rss" "$blogdir/rss_template.xml" | sed "s@<!--url-->@$url@g" | sed '/<!--rss-->/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
2019-09-25 09:42:22 +00:00
2019-09-26 05:26:09 +00:00
echo "Done"