diff options
| author | EuAndreh <eu@euandre.org> | 2021-01-17 19:31:20 -0300 |
|---|---|---|
| committer | EuAndreh <eu@euandre.org> | 2021-01-17 19:31:20 -0300 |
| commit | bede9b3d35c3d385504d3cc661c60066a8099dfe (patch) | |
| tree | 5a6863927ac553eb1bea0ff4612019d1503b4940 /scripts/post.sh | |
| parent | Remove unused scripts (diff) | |
| download | dotfiles-bede9b3d35c3d385504d3cc661c60066a8099dfe.tar.gz dotfiles-bede9b3d35c3d385504d3cc661c60066a8099dfe.tar.xz | |
Move cron scripts into cron/, remove ad-hoc subfolder
Diffstat (limited to 'scripts/post.sh')
| -rwxr-xr-x | scripts/post.sh | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/scripts/post.sh b/scripts/post.sh new file mode 100755 index 00000000..25579fa4 --- /dev/null +++ b/scripts/post.sh @@ -0,0 +1,120 @@ +#!/bin/sh -eu + +# shellcheck source=/dev/null +. "$DOTFILES/bash/colors.sh" + +missing() { + red "Missing argument <${1}>.\n" +} + +usage() { + cat <<EOF +Usage: + $0 <TYPE> <FULL_TITLE> + + Arguments: + TYPE The type of the article: article, til, slides, podcast, screencast + FULL_TITLE Full title of the pastebin + +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" + ;; + slides) + LAYOUT=slides + DIR=_slides + ;; + *) + red "Invalid type '$TYPE'" + 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;' +} + +SLUG_TITLE="$(slugify "$FULL_TITLE")" +PASTE_DATE="$(date -I)" +OUT="$DIR/$PASTE_DATE-$SLUG_TITLE.md" + +cd ~/dev/libre/website + +[ -f "$OUT" ] && { + red "Pastebin named $OUT already exists." + exit 1 +} + +if [ "$LAYOUT" = 'slides' ]; then + cat<<EOF > "$OUT" +--- + +title: $FULL_TITLE + +date: $PASTE_DATE + +layout: $LAYOUT + +lang: en + +ref: $SLUG_TITLE + +--- + +--- + +## Thank you! + +References: + +1. these slides: [{{ site.tld }}/slides.html]({% link slides.md %}) +2. [prose version of this presentation]({% link _articles/$PASTE_DATE-$SLUG_TITLE.md %}) +EOF + "$0" article "$FULL_TITLE" + else + cat <<EOF > "$OUT" +--- + +title: $FULL_TITLE + +date: $PASTE_DATE + +layout: $LAYOUT + +lang: en + +ref: $SLUG_TITLE + +--- +EOF +fi |
