I Love Automation
So, I’ve been doing this blogging thing for a few months now, and now that I have the transition to Hugo done and the theme sorted out, and comments (see below!) sorted out, it’s time to make the process of blogging easier. The first task is to make it easier to actually create a markdown file with all the Hugo stuff pre-populated. I spent a little time in bash and this is what I came out with:
#!/bin/bash
# The purpose of this script is to create a blog post template, save it where it needs to go, and enter vim with the cursor in the correct place.
# Written by Matthew Weber
# Created on 6/25/2025
# Version 1.0
#
# Check if a filename argument is provided
if [ -z "$1" ]; then
echo "Usage: $0 <filename>"
exit 1
fi
CURRENT_YEAR=$(date +%Y)
CURRENT_DATE=$(date +%Y-%m-%d)
BASE_DIR="$HOME/mhome/Documents/Pages/hugo/mtwb/content/posts"
YEAR_DIR="$BASE_DIR/$CURRENT_YEAR"
# Get the desired filename from the first argument
# Convert filename to a human-readable title (e.g., "my-new-post" becomes "My New Post")
# This took me forever. I hate awk. I love awk. I'm terribe at awk. And sed.
RAW_FILENAME="$1"
TITLE=$(echo "$RAW_FILENAME" | sed 's/[_-]/ /g' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) tolower(substr($i,2));}1')
FILENAME="$RAW_FILENAME.md"
FULL_PATH="$YEAR_DIR/$FILENAME"
# TODO: Make this an if statement so that if the dir already exists, it does nothing
mkdir -p "$YEAR_DIR"
# Create the front matter content
FRONT_MATTER="+++\ntitle = \"$TITLE\"\ndate = \"$CURRENT_DATE\"\ndescription = \"\"\nrssFullText = true\n\n[params]\n author = \"Matt\"\n+++\n\n"
# Write the front matter to the new markdown file
echo -e "$FRONT_MATTER" > "$FULL_PATH"
echo "Markdown file created successfully with front matter at: $FULL_PATH"
I’m not kidding when I say the hardest part of that was the awk line. RegEx is really not my strongest talent. I suck at it, okay?
There are a few places that need some work. I need to put in an if statement for the year directory. No sense in making that do something if the directory already exists. I’m also thinking that it’s completely unnecessary as I look at it now. I also want to add a bit at the end of the Markdown file that calls out for comments, emails, and a link to the fedi. That way I don’t have to type it out each time.
This was quite fun to do. Next, I need to write a script to automate uploading to git. I’d also like one that updates the git submodule for my theme when I make a change. I’m sure I will come up with more as time goes on. I love automation like this. I’m not the best bash script maker in the world, but I can get things done. And I think that’s the point. If you can do something simple like this that makes your life easier in even the smallest way, you definitely should. Automation is not only time effective, but it’s fun too.
Thoughts? Leave a comment below! Or you can follow me on the Fediverse or email me.