#!/bin/bash name="Rick" webdir="$PWD" website="https://r1k.tilde.institute/" # Keep a trailing slash. css="../style.css" blogfile="$(date +%Y).html" indexfile="blogindex.html" rssfile="rss.xml" archivefile="$webdir/blog/.htaccess" [ -z "$EDITOR" ] && EDITOR="nvim" [ ! -d "$webdir/blog/.drafts" ] && read -erp "Initialize blog in $webdir?" ask && if [ "$ask" = "y" ]; then printf "Initializing blog system...\\n" mkdir -pv "$webdir/blog/.drafts" || printf "Error. Do you have write permissions in this directory?\\n" echo "Options +Indexes" > "$archivefile" else exit fi newpost() { read -erp "Give a title for your post: " title echo "$title" | grep -q "\"" && printf "Double quotation marks (\") are not allowed in entry titles.\\n" && exit url="$(echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")" echo "AddDescription \"$title\" \"$url.html\"" >> "$archivefile"|| { echo "Error: Is $archivefile writeable?"; exit; } ( [ -f "$webdir/blog/.drafts/$url.html" ] || [ -f "$webdir/blog/$url.html" ] ) && echo "There is already an existing draft or post of that same name/URL." && exit $EDITOR "$webdir/blog/.drafts/$url.html" ;} listandReturn() { printf "Listing contents of %s\\n" "$1" case "$(ls "$1" | wc -l)" in 0) echo "There's nothing to $2." && exit 1 ;; 1) number=1 && printf "There's only one entry to %s.\\nDefaulting selection to %s\\n" "$2" "$(ls -rc "$1" | awk -F '/' '{print $NF}')" ;; *) ls -rc "$1" | awk -F '/' '{print $NF}' | nl read -erp "Pick an entry by number to $2, or press Ctrl-C to cancel. " number ;; esac chosen="$(ls -rc "$1" | nl | grep -w " $number" | awk '{print $2}')" basefile="$(basename "$chosen")" && base="${basefile%.*}" } publish() { \ delete draft htaccessentry=$(grep "$basefile" "$archivefile") realname="$(echo "$htaccessentry" | cut -d'"' -f2)" rssdate="$(grep "$basefile" blog/.htaccess | sed "s/.*\.html\"* *#*//g" | tr -d '\n')" [ -z "$rssdate" ] && rssdate="$(LC_TIME=en_US date '+%a, %d %b %Y %H:%M:%S %z')" # RSS date formats must comply with standards to validate. webdate="$(date '+%a, %d %b %Y %H:%M:%S %z')" # But this visible date you can set to any format. tmpdir=$(mktemp -d) printf "\\n\\n\\n%s\\n\\n\\n\\n\\n

%s

\\n[linkstandalone]\\n%s\\n\\n\\n\\n" "$realname" "$css" "$realname" "../$blogfile" "$base" "$basefile" "$(cat "$webdir/blog/.drafts/$basefile")" "$website" "$name" > "$webdir/blog/$basefile" printf "\\n\\n%s\\n%s%s#%s\\n%s\\n\\n\\n\\n" "$realname" "$website" "$blogfile" "$base" "$rssdate" "$(cat "$webdir/blog/.drafts/$basefile")" > "$tmpdir/rss" printf "
\\n

%s

\\n[linkstandalone]\\n%s\\n%s\\n
\\n" "$base" "$realname" "$base" "blog/$basefile" "$(cat "$webdir/blog/.drafts/$basefile")" "$webdate" > "$tmpdir/html" printf "
  • %s – %s
  • \\n" "$(date '+%Y %b %d')" "$basefile" "$realname" > "$tmpdir/index" sed -i "//r $tmpdir/html" "$blogfile" sed -i "//r $tmpdir/rss" "$rssfile" sed -i "//r $tmpdir/index" "$indexfile" sed -i "/ \"$base.html\"/d" "$archivefile" echo "AddDescription \"$realname\" \"$basefile\" #$rssdate" >> "$archivefile" rm -f "$webdir/blog/.drafts/$chosen" } confirm() { read -erp "Really $1 \"$base\"? (y/N) " choice && echo "$choice" | grep -qi "^y$" || exit 1 ;} delete() { \ sed -i "//!ba};/#$base<\\/guid/d" "$rssfile" sed -i "/
    /{:a;N;/<\\/div>/!ba};/id='$base'/d" "$blogfile" sed -i "/
  • .*/d" "$indexfile" rm -f "$webdir/blog/$basefile" && [[ "$1" != "draft" ]] && printf "Old blog entry removed.\\n";} revise() { awk '/^\[/{flag=1;next}/