A wild bash script appeared! Or, in other words, I no longer use Forestry.

Kev Quirk recently tried Forestry.io and decided it was not for him. This reminded me it's been a while I used it at all. Here's the little bash script I use instead.

Be aware this is a draft post — please adjust your expectations accordingly. Get in touch if this post could use an improvement.

This is a part of the 100 Days To Offload challenge.

Kev recently tried Forestry.io and decided it was not for him. This reminded me it’s been a while I used it at all. Although I disliked writing Markdown syntax when in a content flow (ish) state — and therefore preferred Forestry — I don’t anymore. I actually prefer just writing the Markdown syntax directly. Weird.

So I decided to finally remove the biggest barrier to writing new articles, and to solve it locally: getting the skeleton of a new article ready.

Having been forced to learn a bit of bash scripting at work recently, I wrote one here too! While not the fanciest script out there, it does the job and gets me writing in no time. Initially, I had added support for both articles and notes. For the time being, I decided to just focus on articles and not overwhelm myself with too many post types. So far it works well.

I’d like to add neat features like slugifying a given input so I can write the slug more freely, and defaulting to article if no input it provided, since that’s the only supported post type I have at the moment. There’s no need to write the word ‘article’ every time.

Here’s the whole script. 🙂

#!/bin/bash

supported_types=( "article" )

# Full current date including spaces in the format: YYYY-MM-DD HH:MM:SS +0530
get_full_date() {
date "+%F\T%T%z"
}

get_short_date() {
date "+%F"
}

# Takes a single argument of what the delimiter will be
post_types_delimited() {
echo ${supported_types[@]} | sed "s/ /$1/g"
}

validate_type_input() {
# Credit: Dominik @ https://fosstodon.org/web/statuses/105637235459902845
if [[ "${supported_types[@]}" =~ "${type}" ]]; then
# When valid input
echo "Post type OK."
else
# When invalid input
# Redirect output to STDERR
echo "Incorrect post type specified!" >&2
exit 1
fi
}

# Get inputs as variables
get_input() {
read -p "Type ($(post_types_delimited ', ')): " type
validate_type_input
read -p "Title: " title
read -p "Slug: " slug
}

get_input

# TODO Move to a function
# Construct the location of the new content file
out="./src/content/${type}s/$(get_short_date)-${slug}.md"
if test -f "$out"; then
# Redirect echo to STDERR
echo "File already exists!" >&2
# Exit with an error code of 1
exit 1
else
# Copy post type template to out location
echo "Writing to $out"
cp "./content-template/$type" "$out"
fi

# TODO Move to a function
# The parentheses store the selection as '1' which we use later as \1
# \s denotes space
# Replace date
sed -i -E "s/(date:\s)/\1$(get_full_date)/" $out
# Replace title
sed -i -E "s/(title:\s)/\1\"$title\"/" $out
# Replace slug
sed -i -E "s/(slug\:\s)/\1\"$slug\"/" $out

echo "Done."
0

Comment via email.