aboutsummaryrefslogtreecommitdiff
path: root/scripts/post.sh
blob: 50cd187f20352f284b26170264094ee07edf714e (plain) (blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh
set -eu

# shellcheck source=/dev/null
. "$DOTFILES/bash/colors.sh"

missing() {
  red "Missing argument <${1}>.\n" >&2
}

usage() {
  cat <<EOF >&2
Usage:
    $0 <TYPE> <FULL_TITLE>

    Arguments:
      TYPE             The type of the article: article, til, slides, podcast, screencast
      FULL_TITLE       Full title of the post

Examples:
    $0 til 'I just learned this'
    $0 article 'My example article title'
EOF
}

TYPE="${1:-}"
[ -z "$TYPE" ] && {
  missing 'TYPE'
  usage
  exit 2
}

case "$TYPE" in
  article | til | podcast | screencast)
    LAYOUT=post
    DIR="_${TYPE}s"
    EXT=md
    ;;
  slides)
    LAYOUT=slides
    DIR=_slides
    EXT=slides
    ;;
  *)
    red "Invalid type '$TYPE'\n" >&2
    usage
    exit 2
    ;;
esac

FULL_TITLE="${2:-}"
[ -z "$FULL_TITLE" ] && {
  missing 'FULL_TITLE'
  usage
  exit 2
}

# Derived from:
# https://stackoverflow.com/questions/4009281/how-can-i-generate-url-slugs-in-perl/4009519#4009519
slugify() {
  echo "$1"                     | \
    tr '[:upper:]' '[:lower:]'  | \
    perl -ne 'tr/\000-\177//cd;
              s/[^\w\s-]//g;
              s/^\s+|\s+$//g;
              s/[-\s]+/-/g;
              print;'
}

WEBSITE_REPO="$HOME/dev/libre/website"
SLUG_TITLE="$(slugify "$FULL_TITLE")"
PASTE_DATE="$(date -I)"
OUT="$WEBSITE_REPO/$DIR/$PASTE_DATE-$SLUG_TITLE.$EXT"

cd "$WEBSITE_REPO"

[ -f "$OUT" ] && {
  red "Post named $OUT already exists.\n"
  exit 1
}

if [ "$LAYOUT" = 'slides' ]; then
  ARTICLE="$($0 article "$FULL_TITLE")"
  cat<<EOF > "$OUT"
---

title: $FULL_TITLE

date: $PASTE_DATE

layout: $LAYOUT

lang: en

ref: $SLUG_TITLE

article: $ARTICLE

---

---

## Thank you!

References:

1. FIXME
2. FIXME
EOF
else
  cat <<EOF > "$OUT"
---

title: $FULL_TITLE

date: $PASTE_DATE

layout: $LAYOUT

lang: en

ref: $SLUG_TITLE

---
EOF
fi

echo "$OUT"