site

A custom static site generator written in C.
git clone https://unitehenry.com/git/site.git
Log | Files | Refs | README

stagit (5786B)


      1 #!/usr/bin/env bash
      2 
      3 # Generate static stagit pages for the projects listed in content/index.md.
      4 #
      5 # The project metadata is read from the generated build/index.html with htmlq,
      6 # so run scripts/generate first. Projects without a GitHub link are skipped.
      7 #
      8 # Repositories are mirrored into .cache/stagit/repos so subsequent runs only
      9 # fetch new commits. The generated site is written to build/git and can be
     10 # served by any static file server.
     11 #
     12 # Environment variables:
     13 #   STAGIT_HTML       generated site HTML (default: build/index.html)
     14 #   STAGIT_CACHE      repository cache directory (default: .cache/stagit)
     15 #   STAGIT_OUTPUT     output directory (default: build/git)
     16 #   STAGIT_BASE_URL   public URL of the output (default: https://unitehenry.com/git)
     17 #   STAGIT_BIN        path to stagit (default: stagit)
     18 #   STAGIT_INDEX_BIN  path to stagit-index (default: stagit-index)
     19 
     20 set -euo pipefail
     21 
     22 ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
     23 INDEX="${STAGIT_HTML:-$ROOT/build/index.html}"
     24 
     25 CACHE_DIR="${STAGIT_CACHE:-$ROOT/.cache/stagit}"
     26 REPO_DIR="$CACHE_DIR/repos"
     27 OUT_DIR="${STAGIT_OUTPUT:-$ROOT/build/git}"
     28 ASSET_DIR="$ROOT/static/git"
     29 BASE_URL="${STAGIT_BASE_URL:-https://unitehenry.com/git}"
     30 
     31 STAGIT_BIN="${STAGIT_BIN:-stagit}"
     32 STAGIT_INDEX_BIN="${STAGIT_INDEX_BIN:-stagit-index}"
     33 
     34 if ! command -v "$STAGIT_BIN" >/dev/null 2>&1; then
     35   echo "error: $STAGIT_BIN not found" >&2
     36   echo "install stagit or set STAGIT_BIN" >&2
     37   exit 1
     38 fi
     39 
     40 if ! command -v "$STAGIT_INDEX_BIN" >/dev/null 2>&1; then
     41   echo "error: $STAGIT_INDEX_BIN not found" >&2
     42   echo "install stagit or set STAGIT_INDEX_BIN" >&2
     43   exit 1
     44 fi
     45 
     46 if ! command -v htmlq >/dev/null 2>&1; then
     47   echo "error: htmlq not found (required to parse $INDEX)" >&2
     48   exit 1
     49 fi
     50 
     51 if [ ! -f "$INDEX" ]; then
     52   echo "error: $INDEX not found (run scripts/generate first)" >&2
     53   exit 1
     54 fi
     55 
     56 for asset in style.css logo.png favicon.png; do
     57   if [ ! -f "$ASSET_DIR/$asset" ]; then
     58     echo "error: $ASSET_DIR/$asset not found" >&2
     59     exit 1
     60   fi
     61 done
     62 
     63 STAGIT_BIN="$(command -v "$STAGIT_BIN")"
     64 STAGIT_INDEX_BIN="$(command -v "$STAGIT_INDEX_BIN")"
     65 
     66 # Read project details from the generated index. Only details with both a local
     67 # git link and a GitHub link are considered.
     68 parse_projects() {
     69   htmlq -f "$INDEX" -r '#blog ~ details' 'details' |
     70     awk '
     71       /<details>/ {
     72         inblock = 1
     73         block = $0 "\n"
     74         next
     75       }
     76 
     77       inblock {
     78         block = block $0 "\n"
     79       }
     80 
     81       inblock && /<\/details>/ {
     82         if (block ~ /href="\/git\//) printf "%s%c", block, 0
     83         inblock = 0
     84         block = ""
     85       }
     86     ' |
     87     while IFS= read -r -d '' block; do
     88       git_href="$(printf '%s\n' "$block" | htmlq -a href 'summary a[href^="/git/"]' | sed -n '1p')"
     89       gh_href="$(printf '%s\n' "$block" | htmlq -a href 'summary a[href^="https://github.com/"]' | sed -n '1p')"
     90 
     91       [ -n "$git_href" ] && [ -n "$gh_href" ] || continue
     92 
     93       name="$(printf '%s\n' "$git_href" | sed -E 's|^/git/([^/]+)/.*|\1|')"
     94       owner="$(printf '%s\n' "$gh_href" | sed -E 's|^https://github\.com/([^/]+)/.*|\1|')"
     95       description="$(printf '%s\n' "$block" | htmlq -t 'p' | tr '\t\r\n' '   ' | sed -e 's/  */ /g' -e 's/^ //' -e 's/ $//')"
     96 
     97       printf '%s\t%s\t%s\t%s\n' "$name" "$description" "$owner" "$gh_href"
     98     done
     99 }
    100 
    101 mkdir -p "$REPO_DIR" "$OUT_DIR"
    102 
    103 repo_dirs=()
    104 
    105 if ! project_rows="$(parse_projects)"; then
    106   exit 1
    107 fi
    108 
    109 while IFS=$'\t' read -r name description owner url; do
    110   [ -n "$name" ] || continue
    111 
    112   if [ -z "$url" ]; then
    113     printf 'Skipping %s (no repository URL)\n' "$name" >&2
    114     continue
    115   fi
    116 
    117   if [[ ! "$name" =~ ^[A-Za-z0-9._-]+$ ]]; then
    118     echo "error: invalid repository name: $name" >&2
    119     exit 1
    120   fi
    121 
    122   if [ -z "$description" ]; then
    123     description="$name"
    124   fi
    125 
    126   repo_dir="$REPO_DIR/$name.git"
    127 
    128   if [ -d "$repo_dir" ]; then
    129     current_url="$(git --git-dir="$repo_dir" config --get remote.origin.url || true)"
    130 
    131     if [ "$current_url" != "$url" ]; then
    132       # A project may switch to a different repository (for example, a mirror).
    133       # A normal update would keep serving refs from the old remote, so reclone.
    134       printf 'Recloning %s (remote changed from %s)...\n' "$name" "$current_url"
    135       rm -rf "$repo_dir"
    136       git clone --quiet --mirror "$url" "$repo_dir"
    137     else
    138       printf 'Updating %s...\n' "$name"
    139       git --git-dir="$repo_dir" remote update --prune
    140     fi
    141   else
    142     printf 'Cloning %s...\n' "$name"
    143     git clone --quiet --mirror "$url" "$repo_dir"
    144   fi
    145 
    146   printf '%s\n' "$description" >"$repo_dir/description"
    147   printf '%s\n' "$owner" >"$repo_dir/owner"
    148   printf '%s\n' "$BASE_URL/$name.git" >"$repo_dir/url"
    149 
    150   repo_dirs+=("$repo_dir")
    151 done <<<"$project_rows"
    152 
    153 if [ "${#repo_dirs[@]}" -eq 0 ]; then
    154   echo "error: no repositories found in $INDEX" >&2
    155   exit 1
    156 fi
    157 
    158 mkdir -p "$OUT_DIR"
    159 
    160 for repo_dir in "${repo_dirs[@]}"; do
    161   name="$(basename "$repo_dir" .git)"
    162 
    163   printf 'Generating %s...\n' "$name"
    164 
    165   rm -rf "$OUT_DIR/$name"
    166   mkdir -p "$OUT_DIR/$name"
    167 
    168   (
    169     cd "$OUT_DIR/$name"
    170     "$STAGIT_BIN" -u "$BASE_URL/$name/" "$repo_dir"
    171 
    172     # Assets are copied rather than symlinked so the build artifact works on
    173     # hosts that do not preserve symlinks.
    174     cp "$ASSET_DIR/style.css" "$ASSET_DIR/logo.png" "$ASSET_DIR/favicon.png" .
    175   )
    176 
    177   # Publish a bare copy for cloning over dumb HTTP. update-server-info writes
    178   # the ref and pack metadata that git clone needs from a static file server.
    179   rm -rf "$OUT_DIR/$name.git"
    180   cp -a "$repo_dir" "$OUT_DIR/$name.git"
    181   git --git-dir="$OUT_DIR/$name.git" update-server-info
    182 done
    183 
    184 printf 'Generating index...\n'
    185 
    186 "$STAGIT_INDEX_BIN" "${repo_dirs[@]}" >"$OUT_DIR/index.html"
    187 
    188 cp "$ASSET_DIR/style.css" "$ASSET_DIR/logo.png" "$ASSET_DIR/favicon.png" "$OUT_DIR"
    189 
    190 printf 'Generated %s\n' "$OUT_DIR"