diff options
author | Christian Cleberg <hello@cleberg.net> | 2025-06-03 18:45:02 -0500 |
---|---|---|
committer | Christian Cleberg <hello@cleberg.net> | 2025-06-03 18:45:02 -0500 |
commit | ce28889569c1e0914baca84a6ee893c734e079e7 (patch) | |
tree | e1a5c308fa695968ef1f6df0d77d070a7205da9e | |
parent | d3c8eeb2de40d636a8d2ad894b5cb455695cb634 (diff) | |
download | cleberg.net-ce28889569c1e0914baca84a6ee893c734e079e7.tar.gz cleberg.net-ce28889569c1e0914baca84a6ee893c734e079e7.tar.bz2 cleberg.net-ce28889569c1e0914baca84a6ee893c734e079e7.zip |
feat: build script now automatically updates index posts
-rwxr-xr-x | build.sh | 172 | ||||
-rw-r--r-- | theme/templates/index.html | 42 |
2 files changed, 125 insertions, 89 deletions
@@ -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 diff --git a/theme/templates/index.html b/theme/templates/index.html index fc38322..855ff4a 100644 --- a/theme/templates/index.html +++ b/theme/templates/index.html @@ -8,45 +8,9 @@ sub rsa4096 2022-11-16 [E]</pre> </section> <section> <h2>Recent Blog Posts</h2> - - - <div class="post"> - <time datetime="2025-06-02 15:26:05">2025-06-02</time> - <div class="post-content"> - <a href="/blog/private-ios-apps.html">Selection of Privacy-Focused iOS Applications for Minimalist Users</a> - <div class="post-tags"> - <span class="tag">ios</span> - <span class="tag">privacy</span> - <span class="tag">security</span> - </div> - </div> - </div> - - <div class="post"> - <time datetime="2025-05-30 10:53:28">2025-05-30</time> - <div class="post-content"> - <a href="/blog/it-audit-career.html">Career Advancement through Mastery of Emerging Technologies in IT Audit</a> - <div class="post-tags"> - <span class="tag">audit</span> - <span class="tag">technology</span> - <span class="tag">career</span> - </div> - </div> - </div> - - <div class="post"> - <time datetime="2025-05-02 Fri 21:10:00">2025-05-02</time> - <div class="post-content"> - <a href="/blog/asahi-linux.html">Performance and Compatibility Report of Asahi Linux on Apple M2 MacBook Pro 16"</a> - <div class="post-tags"> - <span class="tag">mac</span> - <span class="tag">apple</span> - <span class="tag">linux</span> - </div> - </div> - </div> - - <br /> + <!-- RECENT-POSTS-START --> + <!-- RECENT-POSTS-END --> + <br> <a href="/blog/">All Posts →</a> </section> |