#!/bin/bash set -euo pipefail [[ $# -lt 1 ]] && echo "usage: newPost \"title\" tag1 tag2 ..." && exit 1 blog_root="/home/b/blog/brontosaurusrex.github.io" [[ ! -d "$blog_root" ]] && echo "$blog_root not found" && exit 1 title="$1" shift postname=$(echo "$title" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g; s/-\+/-/g; s/^-//; s/-$//') today=$(date +%Y-%m-%d) post="${blog_root}/_posts/${today}-${postname}.md" editor="${EDITOR:-hx}" if [ -f "$post" ]; then echo "already exists, opening" "$editor" "$post" exit 0 fi validtag=(audio bash blender books cli linux luv mine misc nvidia video web ai chess) jdate=$(date -u +"%Y-%m-%d %H:%M +0000") tmp=$(mktemp) { echo "---" echo "published: true" echo "layout: post" echo "date: '$jdate'" echo "title: \"$title\"" echo -n "tags: " for tag in "$@"; do [[ " ${validtag[*]} " == *" $tag "* ]] && echo -n "$tag " done echo echo "---" } > "$tmp" mv "$tmp" "$post" "$editor" "$post"