diff options
Diffstat (limited to 'src/bin')
-rwxr-xr-x | src/bin/article | 143 | ||||
-rwxr-xr-x | src/bin/pb | 175 | ||||
-rwxr-xr-x | src/bin/tmp | 74 |
3 files changed, 392 insertions, 0 deletions
diff --git a/src/bin/article b/src/bin/article new file mode 100755 index 0000000..a6ce748 --- /dev/null +++ b/src/bin/article @@ -0,0 +1,143 @@ +#!/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" diff --git a/src/bin/pb b/src/bin/pb new file mode 100755 index 0000000..0e71da5 --- /dev/null +++ b/src/bin/pb @@ -0,0 +1,175 @@ +#!/bin/sh +set -eu + + +WEBSITE_DIR=~/dev/libre/website + + +usage() { + cat <<-'EOF' + Usage: + pb -t TITLE [-l LANGUAGE] [-s SLUG] + pg -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -t TITLE the title of the snippet + -l LANGUAGE the programming language of the snippet + -s SLUG the URL slug of the snippet + + Examples: + + FIXME: + + $ FIXME + EOF +} + + +slugify() { + iconv -ct ASCII//TRANSLIT | + tr '[:upper:]' '[:lower:]' | + sed -e 's/[^a-z0-9]/-/g' \ + -e 's/--*/-/g' \ + -e 's/^-//' \ + -e 's/-$//' +} + +prompt() { + printf '%s [Y/n]: ' "$1" + read -r yesno + if [ "$yesno" = 'n' ] || [ "$yesno" = 'N' ]; then + return 1 + else + return 0 + fi +} + +outname() { + DATE=$1 + SLUG=$2 + N="${3:-0}" + if [ "$N" = 0 ]; then + SUFFIX='' + else + SUFFIX="-$N" + fi + F="_pastebins/$DATE-$SLUG$SUFFIX.md" + if [ -e "$F" ]; then + outname "$DATE" "$SLUG" $((N + 1)) + else + # FIXME + eval "SLUG=$SLUG$SUFFIX" + printf '%s' "$F" + fi +} + +url() { + DATE="$1" + SLUG="$2" + printf 'https://%s/pastebin/%s/%s.html' \ + "$(make -s fqdn)" \ + "$(echo "$DATE" | tr '-' '/')" \ + "$SLUG" +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 't:l:s:h' flag; do + case "$flag" in + t) + TITLE="$OPTARG" + ;; + l) + LANGUAGE="$OPTARG" + ;; + s) + SLUG="$OPTARG" + ;; + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done + +assert_arg() { + if [ -z "$1" ]; then + { + printf 'Missing %s.\n' "$2" + printf '\n' + usage + } >&2 + exit 2 + fi +} + +cd "$WEBSITE_DIR" + +assert_arg "${TITLE:-}" '-t TITLE' +LANGUAGE="${LANGUAGE:-}" +DATE="$(date '+%Y-%m-%d')" +SLUG="${SLUG:-$(echo "$TITLE" | slugify)}" +OUT="$(outname "$DATE" "$SLUG")" +URL="$(url "$DATE" "$SLUG")" + +# shellcheck disable=2064 +trap "rm -f '$OUT-tmp'" EXIT +cat <<-EOF | vipe > "$OUT-tmp" + --- + + title: $TITLE + + date: $DATE + + layout: post + + lang: en + + ref: $SLUG + + --- + + \`\`\`$LANGUAGE + $(cat | sed "s|^```\$|\\\\\`\\\\\`\\\\\`|') + \`\`\` +EOF + +exec 0</dev/tty +if ! prompt 'Proceed with publishing the snippet?'; then + printf 'Aborted.\n' >&2 + exit +fi + +mv "$OUT-tmp" "$OUT" +git reset . +git add "$OUT" +git commit -m "pastebin: Auto-add $OUT" +make deploy + +open "$URL" +printf '%s' "$URL" | copy +printf 'Opened on the browser and copied URL to clipboard.\n' >&2 diff --git a/src/bin/tmp b/src/bin/tmp new file mode 100755 index 0000000..80cadd9 --- /dev/null +++ b/src/bin/tmp @@ -0,0 +1,74 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + tmp FILE... + tmp -d + tmp -h + EOF +} + +help() { + cat <<-'EOF' + + Options: + -d delete the remote "tmp/" folder + -h, --help show this message + EOF +} + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +TLD='euandre.org' +REMOTE_TMP='/home/user-data/www/default/tmp' +while getopts 'dh' flag; do + case "$flag" in + d) + printf 'Deleting %s:%s...\n' "$TLD" "$REMOTE_TMP/" >&2 + ssh "$TLD" rm -rf "$REMOTE_TMP" + exit + ;; + h) + usage + help + exit + ;; + *) + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +if [ -z "${1:-}" ]; then + usage >&2 + exit 2 +fi + +for f in "$@"; do + FILENAME="$(basename "$f")" + # shellcheck disable=2029 + ssh "$TLD" "mkdir -p '$REMOTE_TMP' && cat > '$REMOTE_TMP/$FILENAME'" < "$f" + + LINK="$(printf 'https://%s/tmp/%s' "$TLD" "$FILENAME")" + open "$LINK" + if [ $# = 1 ]; then + printf '%s' "$LINK" | copy + printf 'Copied %s to the clipboard!\n' "$LINK" >&2 + fi +done |