aboutsummaryrefslogtreecommitdiff
path: root/bin/article
blob: 3c4b483e657df19cd50ffa157c84b122f7e6640e (about) (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
129
130
131
132
133
134
135
136
#!/bin/sh
set -eu

# shellcheck source=/dev/null
. "$DOTFILES/sh/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
}

EXTRA=''
case "$TYPE" in
  article|til)
    LAYOUT=post
    DIR="_${TYPE}s"
    EXT=md
    ;;
  podcast|screencast)
    LAYOUT=post
    DIR="_${TYPE}s"
    EXT=md
    EXTRA="
$TYPE: true
"
    ;;
  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"

if [ -f "$OUT" ]; then
  exec vi "$OUT"
fi

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
$EXTRA
---

---

## 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

vi "$OUT"