diff options
Diffstat (limited to '')
-rwxr-xr-x | bin/pb | 131 | ||||
-rwxr-xr-x | bin/post.sh | 136 |
2 files changed, 267 insertions, 0 deletions
@@ -0,0 +1,131 @@ +#!/bin/sh +set -eu + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'hV' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done + +NAME='pb' + +usage() { + cat <<EOF +Missing argument <$1>. + +Usage: + $NAME <FULL_TITLE> [-|FILE [LANG]] + + Reads contents from [FILE], from stdin if '-' is given, and opens the + editor on the content. + + Arguments: + FULL_TITLE Full title of the pastebin + +Examples: + $NAME 'My example pastebin title' + $NAME 'My example pastebin title' file + $NAME 'My example pastebin title' - < file + cat file | $NAME 'My example pastebin title' - + $NAME 'Title' file lang + $NAME 'Title' - lang < file + cat file | $NAME 'Title' - lang +EOF +} + +FULL_TITLE="${1:-}" +[ -z "$FULL_TITLE" ] && { + usage 'FULL_TITLE' + 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="_pastebins/$PASTE_DATE-$SLUG_TITLE.md" +INPUT="${2:-}" +PL="${3:-FIXME}" +if [ -n "$INPUT" ]; then + CONTENT=$(cat "$INPUT") +else + CONTENT='FIXME' +fi + + +cd ~/dev/libre/website > /dev/null + +[ -f "$OUT" ] && { + echo "Pastebin named $OUT already exists." + exit 1 +} + +FILE="$(mktemp)" +cat <<EOF > "$FILE" +--- + +title: ${FULL_TITLE} + +date: ${PASTE_DATE} + +layout: post + +lang: en + +ref: $SLUG_TITLE + +--- + +\`\`\`$PL +$CONTENT +\`\`\` +EOF + +if [ "$PL" = FIXME ]; then + vipe < "$FILE" > "$OUT" +else + cp "$FILE" "$OUT" +fi + +git reset . +git add "$OUT" +git commit -m "pastebin: Auto-add $OUT" +make clean publish +URL="https://euandre.org/pastebin/$(echo "$PASTE_DATE" | tr '-' '/')/$SLUG_TITLE.html" +open "$URL" +printf "$URL" | xsel -ib +echo 'Opened on the browser and copied URL to clipboard.' >&2 +cd - > /dev/null diff --git a/bin/post.sh b/bin/post.sh new file mode 100755 index 0000000..3c4b483 --- /dev/null +++ b/bin/post.sh @@ -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 <<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" |