Blog Composer Script

One thing I love about this setup is I just have to write markdown; and I can easily do that from my text editor in console. However it became apparent that I needed more stuff in my front matter than I thought; so I decided to automate things a bit.

title: Blog Composer Script
layout: post
date: 2022-09-07 17:47:15
permalink: /dev/2022/SEP/07-blog-composer-script.php
excerpt_separator: <!--more-->

This is quite literally the front matter for this post. No, it’s not a whole lot of information; but it would be really nice to just have all of that dumped in to a file right in to a ready-to-write state. Run a command and get to typing. This why we run Linux and self-host, right? To automate all the self-hosted things!

Well, that’s why I do it. Either way, here’s the bash script that I hastly hacked together:

# post composer
# by: Jay/nq4t/@music_onhold

# usage: ./ [category] [title]
# run in the root of your site files/repository
# assumes categories are directories in root

# Variables and category argument
pd=$(date +'%Y-%m-%d')
pt=$(date +'%T')
# Ditch the category argument
shift 1
# Read everything else as title.
t=${t// /-}
fd=$(date +'%Y/%^b/%d')
# Let's write the front matter to our temp file.
printf -- "---\ntitle: $title\nlayout: post\ndate: $pd $pt\npermalink: /$category/$fd-$t.php\nexcerpt_separator: <!--more-->\n---\n\n" >> $file
# Write the post in whatever editor you want.
nano + $file

# Move the file to category/_posts replacing spaces with hyphen
mv $file $category/_posts/$pd-${t// /-}.md
# Display some output to verify it's done.
printf "\nPost $title created in $category: $category/_posts/$pd-$\n\n"

Weeeeee, it’s more comments than actual code; and it literally is just printing lines to files and playing fill in the blanks. Plus I don’t think I have to write much more, it’s already documented.

The only issue I had was when I had a naughty character in a title; so I may need to improve this by adding some stuff to escape those. You can use any editor you want. I’m just used to nano; and being even lazier; I wanted nano to drop the cursor at the end of the file. It’s made dealing with the front matter sooooooo much easier. Template it once and fill in the blanks.

Posted on: 07-SEP-2022