#!/usr/bin/env bash
command -v "just" >/dev/null 2>&1 || { echo >&2 "!!! just is not installed, do not run this without just. Aborting."; exit 1; }
command -v "rsync" >/dev/null 2>&1 || { echo >&2 "!!! rsync is not installed. Aborting."; exit 1; }
source "config.env"
build_pages() {
local OUTPUT_FILE
rsync -r --exclude="*.html" "$CONTENT_DIR/" "$OUTPUT_DIR/"
find "$CONTENT_DIR" -type f -name "*.html" | while read -r FILE; do
OUTPUT_FILE="$(printf %s "$FILE" | sed "s/$CONTENT_DIR/$OUTPUT_DIR/")"
if [ ! -f "$OUTPUT_FILE" ]; then
printf '\n' > "$OUTPUT_FILE"
printf '\n' >> "$OUTPUT_FILE"
sed "s!
!&$SITE_TITLE!" "templates/_head.html" >> "$OUTPUT_FILE"
printf '\n' >> "$OUTPUT_FILE"
cat "templates/_nav.html" "$FILE" "templates/_footer.html" >> "$OUTPUT_FILE"
printf '\n' >> "$OUTPUT_FILE"
printf '\n' >> "$OUTPUT_FILE"
fi
done
}
build_feed() {
local BLOG_INDEX
BLOG_INDEX="$BLOG_DIR/index.html"
local OUTPUT_FILE
OUTPUT_FILE="$OUTPUT_DIR/index.xml"
printf '\n' > "$OUTPUT_FILE"
printf '\n' >> "$OUTPUT_FILE"
printf '\n' >> "$OUTPUT_FILE"
printf '' >> "$OUTPUT_FILE"
printf '%s\n' "$SITE_TITLE" >> "$OUTPUT_FILE"
printf '%s\n' "$DOMAIN" >> "$OUTPUT_FILE"
printf '%s\n' "$DESCRIPTION" >> "$OUTPUT_FILE"
printf '%s\n' "$COPYRIGHT" >> "$OUTPUT_FILE"
grep -Eo "/blog/[a-zA-Z0-9./?=_%:-]*.html" "$BLOG_INDEX" | while read -r POST_URL; do
local POST_FILE
POST_FILE="$CONTENT_DIR$POST_URL"
local POST_TITLE
POST_TITLE="$(grep "class=\"title\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
local POST_DATE
POST_DATE="$(date -d "$(grep "class=\"date\"" "$POST_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')" '+%a, %d %b %Y %H:%M:%S %Z')"
local POST_CONTENT
POST_CONTENT="$(tail -n +6 "$POST_FILE" | head -n -1 | sed 's/^\t//g')"
printf '%s' "$AUTHOR" >> "$OUTPUT_FILE"
printf '%s' "$POST_DATE" >> "$OUTPUT_FILE"
printf '%s' "$POST_TITLE" >> "$OUTPUT_FILE"
printf '%s' "$DOMAIN" >> "$OUTPUT_FILE"
printf '%s' "$POST_URL" >> "$OUTPUT_FILE"
printf '%s' "$DOMAIN" >> "$OUTPUT_FILE"
printf '%s' "$POST_URL" >> "$OUTPUT_FILE"
printf '> "$OUTPUT_FILE"
printf '%s' "$POST_CONTENT" >> "$OUTPUT_FILE"
printf ']]>\n' >> "$OUTPUT_FILE"
done
printf '\n' >> "$OUTPUT_FILE"
}
draft_post() {
mkdir -p "$DRAFTS_DIR"
local DRAFT_FILE
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
cp "$TEMPLATES_DIR/_blog_post.html" "$DRAFT_FILE"
"$EDITOR" "$DRAFT_FILE"
}
edit_draft() {
local DRAFT_FILE
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
"$EDITOR" "$DRAFT_FILE"
read -r -p 'Post this draft? (Y/n) ' CHOICE
case "$CHOICE" in
[nN]*) exit 0 ;;
[yY]*|*) post_draft "$DRAFT_FILE" ;;
esac
}
post_draft() {
local DRAFT_FILE
DRAFT_FILE="$DRAFTS_DIR/$(basename "${1%.*}").html"
local POST_URL
POST_URL="/$(basename "$BLOG_DIR")/$(basename "$DRAFT_FILE")"
local POST_TITLE
POST_TITLE="$(grep "class=\"title\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
local POST_DATE
POST_DATE="$(grep "class=\"date\"" "$DRAFT_FILE" | head -n 1 | sed 's!^[^>]*>!!' | sed 's!<.*$!!')"
local INDEX_LINE
INDEX_LINE="