summaryrefslogtreecommitdiff
path: root/new-post.sh
blob: 331f7005d99d696a41a796da3344581fd89a92d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/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;;
    *)
        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"
if $(echo "$description" | grep -q '[^.$!?]$'); then
    echo 'Description needs to be a complete sentence, with ending punctuation.'
    exit 1
fi

postFileName=static/src/_posts/$td-$clean_title.md
echo "Creating $postFileName"
postContent=$(cat <<EOF
---
title: >-
    $title
description: >-
    $description
---

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