2024-12-26 14:23:38 +01:00
|
|
|
#!/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="---
|
|
|
|
deploy-test * Test Deployment
|
|
|
|
deploy-prod * Prod Deployment
|
2024-12-26 15:09:32 +01:00
|
|
|
maintenance * Maintenance Mode
|
2024-12-26 14:23:38 +01:00
|
|
|
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\|_index \
|
|
|
|
| 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")
|
|
|
|
|
|
|
|
echo "$_file * $_date_fmt | $_title $_draft_fmt"
|
|
|
|
|
|
|
|
done | (sort -t" " -k 2; echo "$_extra") \
|
|
|
|
| fzf -e --tac +s --with-nth 2.. \
|
|
|
|
| cut -d" " -f1)
|
|
|
|
|
|
|
|
### functions
|
|
|
|
_new() {
|
2024-12-26 14:49:00 +01:00
|
|
|
unset _newquit
|
|
|
|
while [ -z "$_newquit" ]
|
|
|
|
do
|
|
|
|
unset _newtitle
|
|
|
|
unset _filename
|
|
|
|
|
2024-12-26 14:23:38 +01:00
|
|
|
echo "Enter Post Title"
|
|
|
|
echo -n ": "
|
2024-12-26 14:49:00 +01:00
|
|
|
read _newtitle
|
|
|
|
if [ -n "$_newtitle" ]
|
2024-12-26 14:23:38 +01:00
|
|
|
then
|
|
|
|
# Welcome to my awful filename generator / sanitizer.
|
|
|
|
# Improvements are welcome!
|
2024-12-26 14:49:00 +01:00
|
|
|
_filename=$(echo "$_newtitle" \
|
2024-12-26 14:23:38 +01:00
|
|
|
| tr -d '?!%$:\\' \
|
|
|
|
| tr ' ./' '_' \
|
|
|
|
| tr -s '_' \
|
|
|
|
| tr '[:upper:]' '[:lower:]' \
|
|
|
|
| sed 's/_$//g;s/^_//g')
|
|
|
|
fi
|
|
|
|
if [ -n "$_filename" ]
|
|
|
|
then
|
2024-12-26 14:49:00 +01:00
|
|
|
echo "Creating:"
|
|
|
|
echo "File: content/posts/$_filename.md"
|
|
|
|
echo "Title: $_newtitle"
|
|
|
|
echo -n "Ok? [Y/n]: "
|
2024-12-26 14:23:38 +01:00
|
|
|
read _ok
|
|
|
|
case "$_ok" in
|
2024-12-26 14:49:00 +01:00
|
|
|
[nN]) ;;
|
|
|
|
*) hugo new content --editor=vim "content/posts/$_filename.md"
|
|
|
|
_newquit=1
|
|
|
|
;;
|
2024-12-26 14:23:38 +01:00
|
|
|
esac
|
|
|
|
else
|
|
|
|
echo "No title entered. Returning to menu."
|
2024-12-26 14:49:00 +01:00
|
|
|
_newquit=1
|
2024-12-26 14:23:38 +01:00
|
|
|
sleep 1
|
|
|
|
fi
|
2024-12-26 14:49:00 +01:00
|
|
|
done
|
2024-12-26 14:23:38 +01:00
|
|
|
}
|
|
|
|
_edit_entry() {
|
|
|
|
unset _quitmenu
|
|
|
|
while [ -z "$_quitmenu" ]
|
|
|
|
do
|
|
|
|
clear
|
|
|
|
_f="${_newname:-$1}"
|
2024-12-26 14:49:00 +01:00
|
|
|
echo "--- $_f:"
|
|
|
|
echo
|
2024-12-26 14:23:38 +01:00
|
|
|
head -15 "$_f"
|
2024-12-26 14:49:00 +01:00
|
|
|
echo "---"
|
2024-12-26 14:23:38 +01:00
|
|
|
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
|
2024-12-26 14:49:00 +01:00
|
|
|
[nN]) unset _newname ;;
|
|
|
|
*) _newname="content/posts/$_newname"
|
|
|
|
mv -vf "$_f" "$_newname"
|
|
|
|
;;
|
2024-12-26 14:23:38 +01:00
|
|
|
esac
|
|
|
|
;;
|
2024-12-26 14:49:00 +01:00
|
|
|
*) vim "$_f" ;;
|
2024-12-26 14:23:38 +01:00
|
|
|
[qQ]) _quitmenu=1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
case "$_selection" in
|
|
|
|
new) _new ;;
|
|
|
|
---) ;;
|
|
|
|
deploy-test) make update ;;
|
|
|
|
deploy-prod) make prod ;;
|
2024-12-26 15:09:32 +01:00
|
|
|
maintenance) make maintenance ;;
|
2024-12-26 14:23:38 +01:00
|
|
|
quit) _quit=1; echo "Good bye." ;;
|
|
|
|
*) [ -n "$_selection" ] \
|
|
|
|
&& _edit_entry "$_selection" \
|
|
|
|
|| _quit=1 ;;
|
|
|
|
esac
|
|
|
|
done
|