In lieu of a CMS that I particularly want to engage with, or indeed spend time engaging with, I’m going to do things the way that Hugo maybe intended, and write directly in markdown. Wait, I hear you ask, what does that look like? Well, it looks a little like this… image

It sure does look like effort repetition, and I’d probably have to make a new file every time I wanted to make a post. At least, the only other option is that I could reuse files, Reduce Reuse Recycle and all that… While I can’t eliminate the work of writing a post entirely, I could make it a bit quicker to start a new post. So what can I reuse each time?

Post metadata

Well, every post starts as a new markdown (.md) file, which lives in the content/posts/ directory, and for no good reason I have decided to file under the year it’s written with the filename year-month-date.md. If I write more than one in a day, I’ll add a suffix of -1, -2, etc. In addition, the templating engine expects some bits and pieces inside the file, with some metadata - author, date, title, and description - stored in between two lines of +++.

The author isn’t about to change between posts, the title and description always change, and the date always changes (but predictably). So I can at least automate: - creation of the file (in the right place) - setting the filename - adding expected formatting - filling in the basic metadata

There are a few ways that I could go about this, but the easiest is going to be a simple bash script that I can run. Realistically that’s all (heh) a CMS is, a collection of automated backend actions responding to some frontend input. It’s worth noting I’m not a CMS scientist, so don’t quote me.

Bash basics, and dates

Let’s start with a basic bash script called newpost.sh:

#! /bin/bash

The #! is known as a shebang, and marks the start of a script. /bin/bash tells the system that it should be run by the bash interpreter.

Moving swiftly on, let’s get the date. Bash has the handy date function, which I can pass a number of flags to get the date and/or time in a variety of formats, or even a custom format. (Linuxize has a nice little post on this here.) This isn’t the “best” way to do it as I should really be using printf -v date for reasons in a bash of version 4.2 or greater, but date still works great for now.

From this, date "+%Y" gives me the year; date "+%Y-%m-%d" gives me the date in Year-Month-Day format; and date "%T" gives me the time in Hour:Minute:Second 24-hour format. I can get everything I need from these - theoretically I could probably do it with a single date command and then split out sections that I want for different purposes, but realistically that’s only going to save me in situations where I’m making a new post on one or two particular seconds of the year: 31st December, 23:59:59. One could argue chained commands call the date individually, so it could change between the first invocation and the last; I’ll avoid this by just not making posts as the New Year ball drops.

Making a file

So, let’s use this to fill variables and make a file. I’ll be running this script most probably from the root of my repository, so I can use relative directory commands to make it instead of explicit ones.

#! /bin/bash

# let's get the date, time, and year into 3 variables
YEAR=$(`date "+%Y")
LONGDATE=$(date "+%Y-%m-%d")
TIME=$(`date "+%T")

# the filename is going to just be the long date with a markdown file type (.md)
FILENAME="$LONGDATE.md"
# the destination is going to be under content/posts/ and then whatever the year is
DESTINATION="content/posts/$YEAR"
# together, we get FILENAMELONG!
FILENAMELONG="$DESTINATION/$FILENAME"
# and that's all we need!

# First, let's check the destination folder exists
if [ ! -d  $DESTINATION ]; then
    # the folder doesn't exist, let's make it
    mkdir -p "$DESTINATION";
fi

# Next, let's check that a file doesn't already exist (juust in case)
if [ ! -f "$FILENAMELONG" ]; then
    # touch can be used to create a file in linux
    touch "$FILENAMELONG"
else
    # if it already exists, let's re-set the filename to include a -1 suffix
    FILENAMELONG="$DESTINATION/$LONGDATE-1.md"
    touch $FILENAMELONG
fi

And that’s it. This will make a file in the right place based on the time that the script runs, with the date in the filename, and it even makes it a second file if the first exists.

This does still limit me to 2 posts a day. Something I could do in the future is extend this further, and list all files starting with $LONGDATE in the $DESTINATION directory. Then, if there are multiple files, I can just increment the suffix and make as many posts per day as I wish.

Next post, I’ll add in the templating bits and also go over why it looks for the metadata.

To be continued!

Edit: Added some context for the shebang at the start of the script.