blop/blop

180 lines
6.9 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
2019-09-30 13:29:33 +00:00
# Amazing logo by HexDSL
2019-09-26 05:26:09 +00:00
# uoou@posteo.net
# https://gitlab.com/uoou/blop
# GPL v.2
2019-09-25 09:42:22 +00:00
#######################
# Stuff you should edit
#######################
2019-10-01 13:40:59 +00:00
blogdir="$PWD" # Default behaviour is to act on the current directory. You can speicify a directory if you prefer. No trailing slash
# The following settings can be overridden by placing a 'site.conf', omitting the leading 'default_', file in the blog directory with a yaml*ish* format, i.e.:
#
# default_author: Rebecca Black
# max_posts: 20
#
default_default_author="Drew"
default_max_posts="10" # number of posts to list on the blog index, posts beyond this will listed in archive.html. Set to "unlimited" (with quotes) to archive nothing
default_group_by_date=false # whether to group the main listing on the blog's index by dates
default_date_format="%B %-d, %Y"
default_short_date_format="%B, %Y" # format for date grouping in the archive and optionally on the index (see group_by_date above)
default_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
#######################
2019-10-01 13:40:59 +00:00
# load config file if it exists and assign stuff
if [[ -f "$blogdir/site.conf" ]]; then
conf=$(cat "$blogdir/site.conf")
if grep -q "default_author" <<< "$conf"; then
default_author=$(echo "$conf" | sed -n 's/^default_author:\s\?\(.*\)$/\1/p')
fi
if grep -q "max_posts" <<< "$conf"; then
max_posts=$(echo "$conf" | sed -n 's/^max_posts:\s\?\(.*\)$/\1/p')
fi
if grep -q "group_by_date" <<< "$conf"; then
group_by_date=$(echo "$conf" | sed -n 's/^group_by_date:\s\?\(.*\)$/\1/p')
fi
if grep -q "date_format" <<< "$conf"; then
date_format=$(echo "$conf" | sed -n 's@^date_format:\s\?\(.*\)$@\1@p')
fi
if grep -q "short_date_format" <<< "$conf"; then
short_date_format=$(echo "$conf" | sed -n 's@^short_date_format:\s\?\(.*\)$@\1@p')
fi
if grep -q "listing_format" <<< "$conf"; then
listing_format=$(echo "$conf" | sed -n 's@^listing_format:\s\?\(.*\)$@\1@p')
fi
fi
if [[ -z "$default_author" ]]; then
default_author="$default_default_author"
fi
if [[ -z "$max_posts" ]]; then
max_posts="$default_max_posts"
fi
if [[ -z "$group_by_date" ]]; then
group_by_date="$default_group_by_date"
fi
if [[ -z "$date_format" ]]; then
date_format="$default_date_format"
fi
if [[ -z "$short_date_format" ]]; then
short_date_format="$default_short_date_format"
fi
if [[ -z "$listing_format" ]]; then
listing_format="$default_listing_format"
fi
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)
2019-10-01 13:40:59 +00:00
if [[ "$count_mds" -eq "0" ]]; then
2019-09-26 05:26:09 +00:00
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 {} +
fi
2019-09-27 06:44:21 +00:00
rm -f "$blogdir/index.html" "$blogdir/rss.xml"
2019-09-26 05:26:09 +00:00
url=$(cat "$blogdir/url.txt")
temp=$(mktemp -d)
2019-09-30 09:50:29 +00:00
postcount="0"
postlist="<ul class=\"postlist\">\n"
2019-09-26 05:26:09 +00:00
for file in $(ls -t1 "$blogdir/markdown/"*.md); do
2019-09-30 09:50:29 +00:00
postcount=$((postcount+1))
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")
2019-09-30 09:50:29 +00:00
short_date=$(date -d "@$udate" +"$short_date_format")
2019-09-26 05:26:09 +00:00
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
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")
2019-09-30 09:50:29 +00:00
if [[ "$max_posts" = "unlimited" ]] || [[ $postcount -le $max_posts ]]; then
# Put it on the front page
if [[ "$group_by_date" = true ]]; then
if [[ -z "$old_short_date" ]]; then
postlist="<h3 class=\"postdate\">$short_date</h3>\n<ul class=\"postlist\">\n"
elif [[ "$short_date" != "$old_short_date" ]]; then
postlist="$postlist</ul>\n<h3 class=\"postdate\">$short_date</h3>\n<ul class=\"postlist\">\n"
fi
fi
postlist="$postlist<li>$listitem</li>\n"
2019-09-30 13:49:40 +00:00
fi
#Put it in the archive
if [[ -z "$archivelist" ]]; then
archivelist="<h3 class=\"archivedate\">$short_date</h3>\n<ul class=\"archivelist\">\n"
elif [[ "$short_date" != "$old_short_date" ]]; then
archivelist="$archivelist</ul>\n<h3 class=\"archivedate\">$short_date</h3>\n<ul class=\"archivelist\">\n"
2019-09-30 09:50:29 +00:00
fi
2019-09-30 13:49:40 +00:00
archivelist="$archivelist<li>$listitem</li>\n"
old_short_date=$short_date
2019-09-30 09:50:29 +00:00
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"
2019-09-26 05:26:09 +00:00
# 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"
2019-09-30 09:50:29 +00:00
if [[ "$max_posts" != "unlimited" ]]; then
echo "Creating archive.html"
archivelist="$archivelist</ul>"
echo -e "$archivelist" > "$temp"/archivelist
sed "/<!--archive list-->/r $temp/archivelist" "$blogdir/archive_template.html" | sed '/<!--archive list-->/d' > "$blogdir/archive.html"
fi
2019-09-26 05:26:09 +00:00
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"