Posted:

newpost script for blogofile

To test my new shell script that creates a new blog post, I'm creating a post about the script. The script just picks the next numeric prefix, sets up the YAML header, and fills in the basic fields.

If you want to use this, you should fix POSTDIR, and you should have it fire up your favorite editor at the end.

#!/bin/bash
#
# newpost [title]
#

set -e # bail on first error
shopt -s nullglob

MARKUP="markdown"

TITLE="$*"
[ -z "$TITLE" ] && TITLE="another post"
SAFE_TITLE="$(echo -n $TITLE | tr -c 'a-z A-Z0-9_.,-' '-')"

POSTDIR="/path/to/blogofile/_posts"
POSTNUMFILE="${POSTDIR}/.postnum"

pnum=$(<"$POSTNUMFILE")
pnum=$(( $pnum + 1 ))
echo $pnum > "$POSTNUMFILE"

dup=false
for f in "${POSTDIR}/"*"${SAFE_TITLE}"*; do
    dup=true
    echo "See $f"
done

if $dup; then
    echo "Existing post with similar title.  Bail?"
    exit 13
fi

POSTNAME="${POSTDIR}/$(printf %05d $pnum)-${SAFE_TITLE}.${MARKUP}"
POSTDATE=$(date +"%Y/%m/%d %H:%M:%S")

# Stick a YAML header into the post
cat >"$POSTNAME" <<EOF
---
title: $TITLE
tags:
date: $POSTDATE
draft: True
---
EOF

echo "Created $POSTNAME"

emacs -nw "$POSTNAME"

#eof

Comments or Questions?

Subscribe via RSS | Atom Feed