dotfiles/.bin/blog
2025-01-18 09:10:20 +01:00

141 lines
3.5 KiB
Bash
Executable File

#!/bin/sh
# hugo cli frontend
# needs: sh, fzf, grep, cat, cut tr, date (bsd date...), hugo, vim
# deployments are handled using a Makefile (bring your own...)
cd "$HOME/blog"
# extra options appended to the menu
_extra="---
maintenance * Maintenance Mode
new * New Post
quit * Quit Main Menu"
# main loop
while [ -z "$_quit" ]
do
# parse csv, build and show list
_selection=$(hugo list all \
| grep -Ev ^path \
| while read _line
do
# csv fields:
# 1: path
# 2: slug
# 3: title
# 4: date
# 5: expiryDate
# 6: publishDate
# 7: draft
# 8: permalink
# 9: kind
# 10: section
_file="$(echo $_line | cut -d"," -f1)"
_title="$(echo $_line | cut -d"," -f3)"
_draft=$(echo $_line | cut -d"," -f7)
_draft_fmt=$([ "$_draft" == "true" ] && echo "(draft)")
_date="$(echo $_line | cut -d"," -f4)"
_date_fmt=$(date -f "%Y-%m-%dT%H:%M:%S" -j "$_date" +"%Y-%m-%d %H:%M")
_type=$(echo "$_file" | grep -q "_index.md$" && echo "page" || echo "post")
echo "$_file * $_date_fmt | $_type | $_title $_draft_fmt"
done | (sort -t" " -k 2; echo "$_extra") \
| fzf -e --tac +s --with-nth 2.. \
| cut -d" " -f1)
### functions
_new() {
unset _newquit
while [ -z "$_newquit" ]
do
unset _newtitle
unset _filename
echo "Enter Post Title"
echo -n ": "
read _newtitle
if [ -n "$_newtitle" ]
then
# Welcome to my awful filename generator / sanitizer.
# Improvements are welcome!
_filename=$(echo "$_newtitle" \
| tr -d '?!%$:\\' \
| tr ' ./' '_' \
| tr -s '_' \
| tr '[:upper:]' '[:lower:]' \
| sed 's/_$//g;s/^_//g')
fi
if [ -n "$_filename" ]
then
echo "Creating:"
echo "File: content/posts/$_filename.md"
echo "Title: $_newtitle"
echo -n "Ok? [Y/n]: "
read _ok
case "$_ok" in
[nN]) ;;
*) hugo new content --editor=vim "content/posts/$_filename.md"
_newquit=1
;;
esac
else
echo "No title entered. Returning to menu."
_newquit=1
sleep 1
fi
done
}
_edit_entry() {
unset _quitmenu
while [ -z "$_quitmenu" ]
do
clear
_f="${_newname:-$1}"
echo "--- $_f:"
echo
head -15 "$_f"
echo "---"
echo
echo "Filename: $(basename "$_f")"
echo "Options: [E]dit, [D]elete, [R]ename, [T]oggle draft, [Q]uit Edit Menu"
echo -n ": "
read _opt
case $_opt in
[dD]) mkdir -p .trash; mv -f "$_f" .trash/
_quitmenu=1 ;;
[qQ]) return ;;
[tT]) grep -qE '^draft.*=.*false' "$_f" \
&& sed -i 's/^draft.*=.*false/draft = true/' "$_f" \
|| sed -i 's/^draft.*=.*true/draft = false/' "$_f" ;;
[rR]) echo "old: $(basename "$_f")"
echo -n "new: "
read _newname;
echo -n "ok? [Y/n]: "
read _ok
case "$_ok" in
[nN]) unset _newname ;;
*) _newname="content/posts/$_newname"
mv -vf "$_f" "$_newname"
;;
esac
;;
*) vim "$_f" ;;
[qQ]) _quitmenu=1 ;;
esac
done
}
case "$_selection" in
new) _new ;;
---) ;;
maintenance) make maintenance ;;
quit) _quit=1; echo "Good bye." ;;
*) [ -n "$_selection" ] \
&& _edit_entry "$_selection" \
|| _quit=1 ;;
esac
done