aboutsummaryrefslogblamecommitdiff
path: root/src/bin/article
blob: a6ce7483423ed7ea3b867b016ad38007534d46d2 (plain) (tree)
1
2
3
4
5
6
7
8
9


         





                            

 















                                                             

 


 














































































































                                                                                                  
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  article
		  article -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -h, --help    show this message


		Examples:

		  FIXME:

		    $ FIXME
			$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"