106 lines
2.8 KiB
Bash
Executable File
106 lines
2.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# aw-post — Quick-post a scroll from the command line
|
|
#
|
|
# Usage:
|
|
# aw-post "Title" # Create empty post, open for editing
|
|
# aw-post "Title" < body.md # Pipe body from stdin
|
|
# echo "Post body" | aw-post "Title" # Pipe body from stdin
|
|
# aw-post --file thoughts.md # Post from a file (title from first line)
|
|
# aw-post --from-x "Tweet text here" # Port an X/Twitter post
|
|
#
|
|
# Creates a markdown file in blog/posts/, rebuilds the blog.
|
|
|
|
set -euo pipefail
|
|
|
|
REPO_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
|
BLOG_DIR="${REPO_DIR}/blog/posts"
|
|
DATE=$(date +%Y-%m-%d)
|
|
|
|
slugify() {
|
|
echo "$1" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd 'a-z0-9-' | head -c 60
|
|
}
|
|
|
|
create_post() {
|
|
local title="$1"
|
|
local body="$2"
|
|
local slug
|
|
slug=$(slugify "$title")
|
|
local filename="${DATE}-${slug}.md"
|
|
local filepath="${BLOG_DIR}/${filename}"
|
|
|
|
if [ -f "$filepath" ]; then
|
|
echo "Error: $filepath already exists"
|
|
exit 1
|
|
fi
|
|
|
|
cat > "$filepath" << EOF
|
|
---
|
|
title: "${title}"
|
|
date: ${DATE}
|
|
---
|
|
|
|
${body}
|
|
EOF
|
|
|
|
echo "Created: ${filepath}"
|
|
python3 "${REPO_DIR}/scripts/build.py"
|
|
}
|
|
|
|
# Parse arguments
|
|
case "${1:-}" in
|
|
--file)
|
|
# Post from a file — title from first non-empty line
|
|
if [ -z "${2:-}" ] || [ ! -f "${2:-}" ]; then
|
|
echo "Usage: aw-post --file <path-to-markdown>"
|
|
exit 1
|
|
fi
|
|
BODY=$(cat "$2")
|
|
# Extract title: first line starting with # or first non-empty line
|
|
TITLE=$(echo "$BODY" | grep -m1 '^#' | sed 's/^#* *//' || echo "$BODY" | grep -m1 '.' || echo "Untitled")
|
|
if [ -z "$TITLE" ]; then
|
|
TITLE="Untitled"
|
|
fi
|
|
# Strip the title line from body if it was a markdown header
|
|
BODY=$(echo "$BODY" | sed '1{/^#/d;}')
|
|
create_post "$TITLE" "$BODY"
|
|
;;
|
|
|
|
--from-x)
|
|
# Port an X/Twitter post — the text becomes both title and body
|
|
if [ -z "${2:-}" ]; then
|
|
echo "Usage: aw-post --from-x \"Tweet text here\""
|
|
exit 1
|
|
fi
|
|
TEXT="$2"
|
|
# Title: first 60 chars, cleaned up
|
|
TITLE=$(echo "$TEXT" | head -1 | cut -c1-60 | sed 's/[[:space:]]*$//')
|
|
create_post "$TITLE" "$TEXT"
|
|
;;
|
|
|
|
--help|-h)
|
|
echo "aw-post — Quick-post a scroll to The Scrolls"
|
|
echo ""
|
|
echo "Usage:"
|
|
echo " aw-post \"Title\" Create a post (body from stdin)"
|
|
echo " aw-post --file thoughts.md Post from a markdown file"
|
|
echo " aw-post --from-x \"Tweet text\" Port an X/Twitter post"
|
|
echo " aw-post --help Show this help"
|
|
;;
|
|
|
|
"")
|
|
echo "Usage: aw-post \"Title of the Post\""
|
|
echo " See: aw-post --help"
|
|
exit 1
|
|
;;
|
|
|
|
*)
|
|
# Standard mode: title as arg, body from stdin
|
|
TITLE="$1"
|
|
BODY=""
|
|
if [ ! -t 0 ]; then
|
|
BODY=$(cat)
|
|
fi
|
|
create_post "$TITLE" "$BODY"
|
|
;;
|
|
esac
|