blob: 6ef0a22358f4bcdf06de8769518b8ae00c56b340 (
plain) (
tree)
|
|
#!/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)
\`\`\`
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
|