htg-content/scripts/new_post.sh

84 lines
1.4 KiB
Bash
Executable File

#!/bin/bash
set -e
BASEDIR=`dirname $0`
if [ -f $BASEDIR/config.sh ]; then
. $BASEDIR/config.sh
fi
if [ -z "$POSTS_DIR" ]
then
echo "POSTS_DIR environment variable is unset." > /dev/stderr
exit 2
fi
function usage {
echo -e "Usage: $0 [OPTIONS] <post title>
Options:
-h\t\tshow this help
-d DATE\tuse DATE instead of today's date
-a AUTHOR\tuse AUTHOR instead of env var \$AUTHOR
-t TAG\tadd TAG to the taglist (can be used multiple times or contain comma-separated list)
-e\t\topen new post in \$EDITOR"
}
function filename {
local unsafe_name="$1"
echo $unsafe_name | iconv -t ascii//TRANSLIT | tr '[:upper:]' '[:lower:]' | tr -s '[:punct:] ' '-' | sed 's/-*$//g;s/^-*//g'
}
DATE=$(date +%Y-%m-%d)
TAGLIST=''
edit=0
while getopts "hd:a:t:e" option
do
case $option in
h)
usage
exit
;;
d)
DATE=$(date +%Y-%m-%d "$OPTARG")
;;
a)
AUTHOR="$OPTARG"
;;
t)
if [ -z $TAGLIST ]
then
TAGLIST="$OPTARG"
else
TAGLIST="$TAGLIST,$OPTARG"
fi
;;
e)
edit=1
;;
*)
echo "Use $0 -h to show help" >&2
exit 1
;;
esac
done
shift $((OPTIND-1))
TITLE="$1"
TEMPLATE=post
FILENAME="$(filename "$TITLE").md"
export TITLE AUTHOR DATE TAGLIST TEMPLATE
cat $BASEDIR/post.tpl | envsubst > $BASEDIR/$POSTS_DIR/$FILENAME
if [ $edit == 1 ]
then
$EDITOR $BASEDIR/$POSTS_DIR/$FILENAME
else
echo $FILENAME
fi