mediocre-blog/new-post.sh

86 lines
1.7 KiB
Bash
Raw Normal View History

2018-09-06 17:11:00 +00:00
#!/bin/sh
set -e
numargs=2
function usage {
echo "Usage: $0 [options] <post title> <post description>
Options:
-i Create image directory
-d \"YYYY-MM-DD\" Custom date to use instead of today
-V Verbose
-x Dry run, don't make any changes
"
exit 1
}
td=$(date "+%Y-%m-%d")
while [ "$(echo $1 | head -c1)" = '-' -o "$#" -gt $numargs ]; do
arg="$1"
shift
case "$arg" in
"-i") IMG_DIR=1;;
"-d") td=$1; shift;;
"-V") VERBOSE=1;;
"-x") DRY_RUN=1;;
2022-05-04 04:23:52 +00:00
"--no-editor") NO_EDITOR=1;;
2018-09-06 17:11:00 +00:00
*)
echo "Unknown option '$arg'"
usage;;
esac
done
if [ "$#" != $numargs ]; then usage; fi
if [ ! -z $VERBOSE ]; then set -x; fi
title="$1"
clean_title=$(echo "$title" |\
tr '[:upper:]' '[:lower:]' |\
sed 's/[^a-z0-9 ]//g' |\
tr ' ' '-' \
)
description="$2"
2021-01-15 23:12:25 +00:00
if $(echo "$description" | grep -q '[^.$!?]$'); then
2018-09-06 17:11:00 +00:00
echo 'Description needs to be a complete sentence, with ending punctuation.'
exit 1
fi
2021-08-05 00:24:32 +00:00
postFileName=static/src/_posts/$td-$clean_title.md
2018-09-06 17:11:00 +00:00
echo "Creating $postFileName"
postContent=$(cat <<EOF
---
title: >-
$title
description: >-
$description
2022-03-13 05:21:51 +00:00
#tags: tech art crypto
2018-09-06 17:11:00 +00:00
---
Write stuff here, title will automatically be added as an h1
## Secondary header
Title is already h1 so all sub-titles should be h2 or below.
EOF
)
if [ -z $DRY_RUN ]; then
echo "$postContent" > "$postFileName"
fi
if [ ! -z $IMG_DIR ]; then
imgDirName="img/$clean_title"
echo "Creating directory $imgDirName"
if [ -z $DRY_RUN ]; then
mkdir -p "$imgDirName"
fi
fi
2022-05-04 04:23:52 +00:00
if [ -z $DRY_RUN ] && [ -z $NO_EDITOR ]; then
exec $EDITOR "$postFileName"
fi