From d5038707bd32783c1c088c6d6ca81d61b5c20051 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 2 May 2022 19:39:09 -0300 Subject: git mv bin/post.sh bin/article --- bin/article | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ bin/post.sh | 136 ------------------------------------------------------------ 2 files changed, 136 insertions(+), 136 deletions(-) create mode 100755 bin/article delete mode 100755 bin/post.sh diff --git a/bin/article b/bin/article new file mode 100755 index 0000000..3c4b483 --- /dev/null +++ b/bin/article @@ -0,0 +1,136 @@ +#!/bin/sh +set -eu + +# shellcheck source=/dev/null +. "$DOTFILES/sh/colors.sh" + +missing() { + red "Missing argument <${1}>.\n" >&2 +} + +usage() { + cat <&2 +Usage: + $0 + + 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< "$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 < "$OUT" +--- + +title: $FULL_TITLE + +date: $PASTE_DATE + +layout: $LAYOUT + +lang: en + +ref: $SLUG_TITLE + +--- +EOF +fi + +vi "$OUT" diff --git a/bin/post.sh b/bin/post.sh deleted file mode 100755 index 3c4b483..0000000 --- a/bin/post.sh +++ /dev/null @@ -1,136 +0,0 @@ -#!/bin/sh -set -eu - -# shellcheck source=/dev/null -. "$DOTFILES/sh/colors.sh" - -missing() { - red "Missing argument <${1}>.\n" >&2 -} - -usage() { - cat <&2 -Usage: - $0 - - 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< "$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 < "$OUT" ---- - -title: $FULL_TITLE - -date: $PASTE_DATE - -layout: $LAYOUT - -lang: en - -ref: $SLUG_TITLE - ---- -EOF -fi - -vi "$OUT" -- cgit v1.2.3