aboutsummaryrefslogtreecommitdiff
path: root/build.sh
diff options
context:
space:
mode:
authorChristian Cleberg <hello@cleberg.net>2025-06-03 18:45:02 -0500
committerChristian Cleberg <hello@cleberg.net>2025-06-03 18:45:02 -0500
commitce28889569c1e0914baca84a6ee893c734e079e7 (patch)
treee1a5c308fa695968ef1f6df0d77d070a7205da9e /build.sh
parentd3c8eeb2de40d636a8d2ad894b5cb455695cb634 (diff)
downloadcleberg.net-ce28889569c1e0914baca84a6ee893c734e079e7.tar.gz
cleberg.net-ce28889569c1e0914baca84a6ee893c734e079e7.tar.bz2
cleberg.net-ce28889569c1e0914baca84a6ee893c734e079e7.zip
feat: build script now automatically updates index posts
Diffstat (limited to 'build.sh')
-rwxr-xr-xbuild.sh172
1 files changed, 122 insertions, 50 deletions
diff --git a/build.sh b/build.sh
index 66887b3..506909c 100755
--- a/build.sh
+++ b/build.sh
@@ -1,56 +1,128 @@
-#!/bin/bash
-
-# Ensure the latest post is included on the home page
-printf "Did you update the 'Recent Blog Posts' section? [yn] "
-read answer
-
-# Only continue if latest post is included on the home page
-if [[ "$answer" =~ ^[Yy]$ ]]; then
- # Check if the environment flag is set to PROD
- if [[ "$ENV" == "prod" ]]; then
- echo "Environment: Production"
-
- # Check if publishing via LAN or remotely
- printf "Publishing on remote or LAN? [r|l] "
-
- # Update ubuntu_server variable based on answer
- read method
- if [[ "$method" =~ ^[Rr]$ ]]; then
- ubuntu_server="ubuntu-remote"
- elif [[ "$method" =~ ^[Ll]$ ]]; then
- ubuntu_server="ubuntu"
- else
- echo "Invalid input. Assuming LAN (ubuntu)"
- ubuntu_server="ubuntu"
- fi
-
- # Remove previous build
- rm -rf .build/*
-
- # Minify CSS
- minify -o theme/static/styles.min.css theme/static/styles.css
-
- # Run publishing script
- emacs --script publish.el &>/dev/null
-
- # Deploy changes
- rsync -r --delete-before .build/* $ubuntu_server:/var/www/cleberg.net/
- else
- echo "Environment: Development"
+#!/usr/bin/env bash
+set -e
+
+# -----------------------------------------------------------------------------
+# Function: generate_recent_posts
+# After publishing to .build/, finds the 3 newest HTML files under .build/blog
+# (excluding blog index.html), extracts each post’s date/title/tags by:
+# - <time datetime="...">
+# - <h1>…</h1>
+# - <span class="tag">…</span> lines
+# Replaces everything between <!-- RECENT-POSTS-START --> and
+# <!-- RECENT-POSTS-END --> in .build/index.html.
+# Tested on macOS.
+# -----------------------------------------------------------------------------
+generate_recent_posts() {
+ BUILD_DIR="${SCRIPT_DIR}/.build"
+ INDEXFILE="$BUILD_DIR/index.html"
+ BLOGDIR="$BUILD_DIR/blog"
+ TMPFILE="$(mktemp)"
+ MARKER_START="<!-- RECENT-POSTS-START -->"
+ MARKER_END="<!-- RECENT-POSTS-END -->"
+
+ # 1) Verify generated index.html exists
+ if [[ ! -f "$INDEXFILE" ]]; then
+ echo "Error: Generated index.html not found at $INDEXFILE" >&2
+ exit 1
+ fi
+
+ # 2) Verify .build/blog exists
+ if [[ ! -d "$BLOGDIR" ]]; then
+ echo "Error: Generated blog directory not found at $BLOGDIR" >&2
+ exit 1
+ fi
+
+ # 3) Collect the three newest post HTML files, excluding /index.html
+ POSTS=()
+ for FILEPATH in $(ls -1t "$BLOGDIR"/*.html 2>/dev/null | grep -v "/index.html\$" | head -n 3); do
+ [[ -f "$FILEPATH" ]] && POSTS+=("$FILEPATH")
+ done
+
+ # 4) Build the HTML snippet into TMPFILE
+ {
+ for POST in "${POSTS[@]}"; do
+ BASENAME="$(basename "$POST")"
+ URL="/blog/${BASENAME}"
+
+ # 4a) DATE: extract from first <time datetime="...">
+ POST_DATE_FULL="$(sed -n 's/.*<time datetime="\([^"]*\)".*/\1/p' "$POST" | head -n1)"
+ if [[ -n "$POST_DATE_FULL" ]]; then
+ POST_DATE="${POST_DATE_FULL%% *}"
+ else
+ POST_DATE="$(stat -f "%Sm" -t "%Y-%m-%d" "$POST")"
+ fi
+
+ # 4b) TITLE: extract from first <h1>…</h1>
+ TITLE="$(sed -n 's/.*<h1[^>]*>\(.*\)<\/h1>.*/\1/p' "$POST" | head -n1)"
+ if [[ -z "$TITLE" ]]; then
+ TITLE="${BASENAME%.html}"
+ fi
- # Remove previous build
- rm -rf .build/*
+ # 4c) TAGS: extract each <span class="tag">…</span> line, re-indent by 6 spaces
+ TAGSPAN="$(grep -o '^[[:space:]]*<span class="tag">[^<]*</span>' "$POST" \
+ | sed 's/^[[:space:]]*/ /' || true)"
- # Minify CSS
- minify -o theme/static/styles.min.css theme/static/styles.css
+ # 4d) Emit one <div class="post">…</div> block
+ cat <<EOF
+ <div class="post">
+ <time datetime="${POST_DATE}">${POST_DATE}</time>
+ <div class="post-content">
+ <a href="${URL}">${TITLE}</a>
+ <div class="post-tags">
+ $(printf "%s\n" "$TAGSPAN")
+ </div>
+ </div>
+ </div>
+EOF
+ done
+ } > "$TMPFILE"
- # Run publishing script
- emacs --script publish.el
+ # 5) Replace everything between the markers in .build/index.html (keeping markers)
+ sed -i '' -e "/${MARKER_START}/,/${MARKER_END}/{
+ /${MARKER_START}/{p; r $TMPFILE
+ };
+ /${MARKER_END}/p;
+ d
+ }" "$INDEXFILE"
- # Launch development web server
- cd .build/
- python3 -m http.server
+ rm "$TMPFILE"
+ echo "→ Injected up to 3 newest posts into $INDEXFILE"
+}
+
+
+# -------------------- Main build.sh logic --------------------
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+
+# Step 1: Clean previous build
+rm -rf "${SCRIPT_DIR}/.build"/*
+
+# Step 2: Minify CSS
+minify -o "${SCRIPT_DIR}/theme/static/styles.min.css" "${SCRIPT_DIR}/theme/static/styles.css"
+
+# Step 3: Run Emacs publish.el (outputs into .build/)
+emacs --script "${SCRIPT_DIR}/publish.el" &>/dev/null
+
+# Step 4: Inject the “Recent Blog Posts” section into the generated index.html
+generate_recent_posts
+
+if [[ "$ENV" == "prod" ]]; then
+ echo "Environment: Production"
+ printf "Publishing on remote or LAN? [r|l] "
+ read -r method
+ if [[ "$method" =~ ^[Rr]$ ]]; then
+ ubuntu_server="ubuntu-remote"
+ elif [[ "$method" =~ ^[Ll]$ ]]; then
+ ubuntu_server="ubuntu"
+ else
+ echo "Invalid input. Assuming LAN (ubuntu)"
+ ubuntu_server="ubuntu"
fi
+
+ # Step 5: Deploy via rsync
+ rsync -r --delete-before "${SCRIPT_DIR}/.build/" "$ubuntu_server":/var/www/cleberg.net/
else
- echo "Please update the 'Recent Blog Posts' section before publishing!"
-fi
+ echo "Environment: Development"
+ # Step 5: Launch local HTTP server from .build/
+ cd "${SCRIPT_DIR}/.build/" || exit
+ python3 -m http.server
+fi \ No newline at end of file