From 1c719e099f3ffb037bfafd5908cf4f90bb142515 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Mon, 2 May 2022 02:43:52 -0300 Subject: bin/pb: Refactor and simplify - stop depending on Perl in slugify(); - stop relying on GNU sed extensions on slugify(); - use optional flags over positional arguments; - use POSIX "date +%Y-%m-%d" over "date -I"; - always read from STDIN instead of deciding based on the input; - don't rely on GNU coreutils' `mktemp`; --- bin/pb | 205 +++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 124 insertions(+), 81 deletions(-) diff --git a/bin/pb b/bin/pb index 43a1de2..5c51c46 100755 --- a/bin/pb +++ b/bin/pb @@ -1,6 +1,83 @@ #!/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 fqdn)" \ + "$(echo "$DATE" | tr '-' '/')" \ + "$SLUG" +} + + for flag in "$@"; do case "$flag" in --) @@ -16,8 +93,17 @@ for flag in "$@"; do esac done -while getopts 'hV' flag; do +while getopts 't:l:s:h' flag; do case "$flag" in + t) + TITLE="$OPTARG" + ;; + l) + LANGUAGE="$OPTARG" + ;; + s) + SLUG="$OPTARG" + ;; h) usage help @@ -30,102 +116,59 @@ while getopts 'hV' flag; do esac done -NAME='pb' - -usage() { - cat <. - -Usage: - $NAME [-|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;' +assert_arg() { + if [ -z "$1" ]; then + { + printf 'Missing %s.\n' "$2" + printf '\n' + usage + } >&2 + exit 2 + fi } -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 "$WEBSITE_DIR" -cd ~/dev/libre/website > /dev/null +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")" -[ -f "$OUT" ] && { - echo "Pastebin named $OUT already exists." - exit 1 -} +trap "rm -f $OUT-tmp" EXIT +cat /dev/stdin <<-EOF | vipe > "$OUT-tmp" + --- -FILE="$(mktemp)" -cat < "$FILE" ---- + title: $TITLE -title: ${FULL_TITLE} + date: $DATE -date: ${PASTE_DATE} + layout: post -layout: post + lang: en -lang: en + ref: $SLUG -ref: $SLUG_TITLE + --- ---- - -\`\`\`$PL -$CONTENT -\`\`\` + \`\`\`$LANGUAGE + $(cat) + \`\`\` EOF -if [ "$PL" = FIXME ]; then - vipe < "$FILE" > "$OUT" -else - cp "$FILE" "$OUT" +exec 0&2 + exit fi +mv "$OUT-tmp" "$OUT" 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" +make deploy + open "$URL" -printf "$URL" | xsel -ib -echo 'Opened on the browser and copied URL to clipboard.' >&2 -cd - > /dev/null +printf '%s' "$URL" | copy +printf 'Opened on the browser and copied URL to clipboard.\n' >&2 -- cgit v1.2.3