I Updated My Blog Script
Back in June, I wrote a post about a script I wrote to automate posting to this blog. It was a fairly simple script that basically crafted the title and then created the .md
file with the front matter inside. I have to admit, I was pretty proud of that script, and I’ve been using it ever since. Every blog post I’ve published since then has been started with that script.
But, as you know, I’m trying Emacs, and that has spurred me to try to make the script better. The first reason is that I really don’t care for Emacs in the terminal. It just doesn’t feel right. Second, making things better is fun.
So, my idea was to incorporate rofi, so that I’d get asked what I wanted to do: Create a new post or update the hugo git repo. I may add a few options later.
My first thought was to make a bunch of nested if
statements, just to piss everyone off. But calmer heads prevailed and I crafted a very large case statement instead. Here’s what I came up with:
#!/usr/bin/env bash
# The purpose of this script is to create a blog post template, save it where it needs to go,
# and enter Emacs with the cursor in the correct place—or to update Hugo.
# Written by Matthew Weber
# Created on 6/25/2025
# Updated on 8/6/2025
# Version 1.2
set -euo pipefail
# Directory Variables
CURRENT_YEAR=$(date +%Y)
CURRENT_DATETIME=$(date +'%Y-%m-%dT%H:%M:%S%:z')
BASE_DIR="$HOME/mhome/Documents/Pages/hugo/mtwb/content/posts"
YEAR_DIR="$BASE_DIR/$CURRENT_YEAR"
DRAFTS_DIR="$YEAR_DIR/drafts"
# Ensure base directories exist
if [ -e $DRAFTS_DIR ]; then
break
else
notify-send "ERROR: No Drafts Directory" && exit 1
fi
# Rofi menu options
options=(
"New Post"
"Update Hugo"
)
choice=$(printf '%s\n' "${options[@]}" \
| rofi -dmenu -i -p 'What Do You Want to Do?')
case "$choice" in
"New Post")
# Prompt for a human-readable title
RAW_TITLE=$(rofi -dmenu -p 'Enter a title: ')
# Convert title into a slug: lowercase, spaces → hyphens, remove non-alphanum/hyphens
SLUG=$(echo "$RAW_TITLE" \
| tr '[:upper:]' '[:lower:]' \
| sed -E 's/[^a-z0-9 ]//g' \
| sed 's/[[:space:]]\+/-/g')
# Convert slug back into a pretty Title Case string
TITLE=$(echo "$SLUG" \
| sed 's/-/ /g' \
| awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2));}1')
FILENAME="$SLUG.md"
FULL_PATH="$DRAFTS_DIR/$FILENAME"
# Write front matter
cat > "$FULL_PATH" <<EOF
+++
title = "$TITLE"
date = "$CURRENT_DATETIME"
description = ""
draft = true
tags = [""]
rssFullText = true
[params]
author = "Matt"
+++
EOF
echo "✅ Created draft: $FULL_PATH"
emacs "$FULL_PATH"
;;
"Update Hugo")
echo "🔄 Running mtblog to update Hugo…"
mtblog
notify-send "Updating Hugo"
;;
*)
echo "⏹ Program Terminated."
exit 1
;;
esac
Overall, it is much, much cleaner than the first version. I’ve tidied up the variables and added a real check for the drafts folder instead of just running mkdir
, and I’ve added in some notifications so that I know what is happening.
I still want to add in more of those. If the script fails, I’d like to know why. I won’t be running this in the terminal, but instead I’ve made a keybinding for it, so I won’t actually get any output unless I craft a notification or pop up. Especially with running a git update like this, I’ll want to know if something goes wrong. So that’s the next step.
I have also been inspired by this to create a note creation script, so I can create one off notes that I can then organize later in Obsidian. I’ll have some fun doing that later.
This was day 7 of Blaugust 2025