lb/lb

83 lines
5.4 KiB
Plaintext
Raw Permalink Normal View History

2018-12-14 17:25:31 +00:00
#!/bin/bash
2021-08-03 08:07:06 +00:00
name="Rick"
2019-06-01 17:29:23 +00:00
webdir="$PWD"
2021-08-03 08:07:06 +00:00
website="https://r1k.tilde.institute/" # Keep a trailing slash.
2018-12-14 17:25:31 +00:00
css="../style.css"
2020-04-17 20:01:39 +00:00
blogfile="$(date +%Y).html"
2018-08-11 23:31:17 +00:00
indexfile="blogindex.html"
2018-12-14 17:25:31 +00:00
rssfile="rss.xml"
2020-09-02 17:18:06 +00:00
archivefile="$webdir/blog/.htaccess"
2021-08-03 08:07:06 +00:00
[ -z "$EDITOR" ] && EDITOR="nvim"
2018-10-29 11:39:58 +00:00
2019-04-26 17:04:58 +00:00
[ ! -d "$webdir/blog/.drafts" ] &&
2018-12-14 17:25:31 +00:00
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"
2020-09-02 17:18:06 +00:00
echo "Options +Indexes" > "$archivefile"
2018-12-14 17:25:31 +00:00
else
exit
fi
2018-10-29 11:39:58 +00:00
2018-12-14 17:25:31 +00:00
newpost() { read -erp "Give a title for your post:
" title
2020-07-08 15:40:07 +00:00
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")"
2020-09-02 17:18:06 +00:00
echo "AddDescription \"$title\" \"$url.html\"" >> "$archivefile"|| { echo "Error: Is $archivefile writeable?"; exit; }
2020-07-08 15:40:07 +00:00
( [ -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
2018-12-14 17:25:31 +00:00
$EDITOR "$webdir/blog/.drafts/$url.html" ;}
2018-05-16 21:46:29 +00:00
listandReturn() {
2020-07-13 20:47:15 +00:00
printf "Listing contents of %s\\n" "$1"
2020-07-08 15:40:07 +00:00
case "$(ls "$1" | wc -l)" in
0) echo "There's nothing to $2." && exit 1 ;;
2020-07-13 20:49:45 +00:00
1) number=1 && printf "There's only one entry to %s.\\nDefaulting selection to %s\\n" "$2" "$(ls -rc "$1" | awk -F '/' '{print $NF}')" ;;
2020-07-08 15:40:07 +00:00
*) 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
2020-07-28 14:04:28 +00:00
chosen="$(ls -rc "$1" | nl | grep -w " $number" | awk '{print $2}')"
basefile="$(basename "$chosen")" && base="${basefile%.*}"
}
2018-05-16 21:46:29 +00:00
2018-12-14 17:25:31 +00:00
publish() { \
delete draft
2020-09-02 17:18:06 +00:00
htaccessentry=$(grep "$basefile" "$archivefile")
2018-12-14 17:25:31 +00:00
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.
2018-12-14 17:25:31 +00:00
webdate="$(date '+%a, %d %b %Y %H:%M:%S %z')" # But this visible date you can set to any format.
2018-05-16 21:46:29 +00:00
tmpdir=$(mktemp -d)
2020-06-25 01:52:17 +00:00
printf "<!DOCTYPE html>\\n<html lang=\"en\">\\n<head>\\n<title>%s</title>\\n<link rel='stylesheet' type='text/css' href='%s'>\\n<meta charset='utf-8'/>\\n</head>\\n<body>\\n<h1>%s</h1>\\n<small>[<a href='%s#%s'>link</a>&mdash;<a href='%s'>standalone</a>]</small>\\n%s\\n<footer>by <strong><a href='%s'>%s</a></strong></footer>\\n</body>\\n\\n</html>" "$realname" "$css" "$realname" "../$blogfile" "$base" "$basefile" "$(cat "$webdir/blog/.drafts/$basefile")" "$website" "$name" > "$webdir/blog/$basefile"
2019-04-01 00:44:29 +00:00
printf "\\n<item>\\n<title>%s</title>\\n<guid>%s%s#%s</guid>\\n<pubDate>%s</pubDate>\\n<description><![CDATA[\\n%s\\n]]></description>\\n</item>\\n\\n" "$realname" "$website" "$blogfile" "$base" "$rssdate" "$(cat "$webdir/blog/.drafts/$basefile")" > "$tmpdir/rss"
2018-12-14 17:25:31 +00:00
printf "<div class='entry'>\\n<h2 id='%s'>%s</h2>\\n<small>[<a href='#%s'>link</a>&mdash;<a href='%s'>standalone</a>]</small>\\n%s\\n<small>%s</small>\\n</div>\\n" "$base" "$realname" "$base" "blog/$basefile" "$(cat "$webdir/blog/.drafts/$basefile")" "$webdate" > "$tmpdir/html"
2018-12-14 17:36:56 +00:00
printf "<li>%s &ndash; <a href=\"blog/%s\">%s</a></li>\\n" "$(date '+%Y %b %d')" "$basefile" "$realname" > "$tmpdir/index"
2018-12-14 17:25:31 +00:00
sed -i "/<!-- LB -->/r $tmpdir/html" "$blogfile"
sed -i "/<!-- LB -->/r $tmpdir/rss" "$rssfile"
sed -i "/<!-- LB -->/r $tmpdir/index" "$indexfile"
2020-09-02 17:18:06 +00:00
sed -i "/ \"$base.html\"/d" "$archivefile"
echo "AddDescription \"$realname\" \"$basefile\" #$rssdate" >> "$archivefile"
2018-12-14 17:25:31 +00:00
rm -f "$webdir/blog/.drafts/$chosen"
2018-05-16 21:46:29 +00:00
}
2020-07-08 15:40:07 +00:00
confirm() { read -erp "Really $1 \"$base\"? (y/N) " choice && echo "$choice" | grep -qi "^y$" || exit 1 ;}
2018-12-14 17:25:31 +00:00
2018-05-16 21:46:29 +00:00
delete() { \
2020-07-08 15:40:07 +00:00
sed -i "/<item/{:a;N;/<\\/item>/!ba};/#$base<\\/guid/d" "$rssfile"
sed -i "/<div class='entry'>/{:a;N;/<\\/div>/!ba};/id='$base'/d" "$blogfile"
sed -i "/<li>.*<a href=\"blog\\/$base.html\">/d" "$indexfile"
rm -f "$webdir/blog/$basefile" && [[ "$1" != "draft" ]] && printf "Old blog entry removed.\\n";}
2018-05-16 21:46:29 +00:00
2018-12-14 17:25:31 +00:00
revise() { awk '/^<small>\[/{flag=1;next}/<footer>/{flag=0}flag' "$webdir/blog/$chosen" > "$webdir/blog/.drafts/$basefile"
"$EDITOR" "$webdir/blog/.drafts/$basefile"
printf "Revision stored in blog/.drafts. Publish as normal entry when desired.\\n" ;}
2018-07-30 01:56:18 +00:00
2018-05-16 21:46:29 +00:00
case "$1" in
2018-12-14 17:25:31 +00:00
n*) newpost ;;
e*) listandReturn "$webdir"/blog/.drafts/ edit && "$EDITOR" "$webdir/blog/.drafts/$chosen" ;;
p*) listandReturn "$webdir"/blog/.drafts/ publish && publish ;;
2020-09-02 17:18:06 +00:00
t*) listandReturn "$webdir"/blog/.drafts/ trash && confirm trash && rm -f "$webdir/blog/.drafts/$chosen" && sed -i "/ \"$base.html\"/d" "$archivefile" ; printf "Draft deleted.\\n" ;;
d*) listandReturn "$webdir"/blog/ delete && confirm delete && delete && sed -i "/ \"$base.html\"/d" "$archivefile" ;;
2018-12-14 17:25:31 +00:00
r*) listandReturn "$webdir"/blog/ revise && revise ;;
2019-01-02 22:01:03 +00:00
*) printf "lb blog system by Luke Smith <luke@lukesmith.xyz>\\nUsage:\\n lb n:\\tnew draft\\n lb e:\\tedit draft\\n lb p:\\tpublish/finalize draft\\n lb r:\\trevise published entry\\n lb t:\\tdiscard draft\\n lb d:\\tdelete published entry\\n\\nBe sure to have the following pattern added to your RSS feed, blog file and blog index:\\n\\n<!-- LB -->\\n\\nNew content will be added directly below that sequence. This is required.\\nSee https://github.com/LukeSmithxyz/lb for more.\\n" ;;
2018-05-16 21:46:29 +00:00
esac