Jekyll is a static site generator created in Ruby, which makes it exceedingly easy for me to type up pages of SEO bloatware without too much trouble.
However, I found it a nuisance to have to manually touch
files under the _posts
directory with an appropriately datestamped filename, so instead, I came up with this Bash function monstrosity which now lives contentedly in my interactive environment:
mkpost ()
{
trap 'OPTIND=1; unset opt' RETURN;
OPTIND=1;
while getopts :c:t:e opt; do
case $opt in
c) local categories=$OPTARG ;;
t) local tags=$OPTARG ;;
e) local edit=1 ;;
:) printf -- '-%c needs argument\n' $OPTARG 1>&2; return 1 ;;
\?) printf -- '-%c: unknown option\n' $OPTARG 1>&2; return 1 ;;
esac
done; shift $(( --OPTIND ))
if ! bundle exec jekyll --version &> /dev/null; then
printf '%s: Not a jekyll site\n' "$PWD" 1>&2
return 1
fi
local jekylldir=$PWD
while [[ -n $jekylldir && ! -d $jekylldir/_posts ]]; do
jekylldir=${jekylldir%/*};
done
if [[ -z $jekylldir ]]; then
printf '%s: No _posts directory found in project\n' "$PWD" 1>&2
return 1
fi
local post=$(sed 's/[^[:alnum:]]\+/-/g' <<<"${*,,}")
printf -v post '%s/_posts/%(%Y-%m-%d)T-%s.md' "$jekylldir" -1 "$post"
cat > "$post" <<-EOF
---
layout: post
title: "${*}"
date: $(printf '%(%Y-%m-%d %T %z)T' -1)
categories: ${categories//,/ }
tags: ${tags//,/ }
---
EOF
(( edit )) && nvim -- "$post"
}
I am thinking that instead of having this ever-living piece of utter shit in my shell environment, ready to make Jekyll posts anywhere in my filesystem as long as bundle exec jekyll --version
can do its thing (and a _posts
directory exists somewhere), I should really just gem
this function up, so I can call bundle exec mkpost
instead. But that will require me to grow a fuck that I can give. We’ll see what the chances of that will be.
Edit: As I write and preview this post, I notice that somehow even though I’ve stolen minima’s syntax highlighting stylesheet, I have somehow managed to fuck up line number vertical alignment with the code lines. I’ll get that working later because I cannot be arsed. For now, I’ll leave line numbers off whenever I post a code snippet, and you can just count them with your fingers.