site
my personal website
git clone https://jhhm.no/repos/site.git
site/bin/makegitrepo
get raw file: makegitrepo
1#!/bin/sh 2# Usage: ssg.sh GITDIR OUTDIR [DESCRIPTION] 3set -eu 4 5if [ $# -lt 2 ]; then 6 echo "usage: $0 GITDIR OUTDIR [DESCRIPTION]" >&2 7 exit 1 8fi 9repo=$1 10out=$2 11desc=${3:-} 12files=$out/files 13raw=$out/raw 14tpl=${DIR_TEMP:-templates} 15tab=$(printf '\t') 16 17# The repo's name is its directory name, without .git. 18reponame=$(cd "$repo" && pwd -P) 19reponame=${reponame%/.git} 20reponame=${reponame##*/} 21reponame=${reponame%.git} 22 23g() { git -C "$repo" -c core.quotePath=false "$@"; } 24 25# Escape HTML special characters: escape filters stdin, esc takes a string. 26escape() { sed 's/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g'; } 27esc() { printf '%s\n' "$1" | escape; } 28 29# Link targets: percent-encode anything that would break a relative URL. 30href() { printf '%s\n' "$1" | sed 's/%/%25/g; s/ /%20/g; s/#/%23/g; s/?/%3F/g; s/&/%26/g; s/</%3C/g; s/>/%3E/g; s/"/%22/g'; } 31 32heading=$(esc "$reponame${desc:+ - $desc}") 33 34# page top|bottom: print the part of page.html before or after the 35# {{CONTENT}} line, with {{TITLE}}, {{HEADER}} and {{FOOTER}} filled in. 36page() { 37 T='{{TITLE}}' H='{{HEADER}}' F='{{FOOTER}}' C='{{CONTENT}}' 38 part=top 39 while IFS= read -r line || [ -n "$line" ]; do 40 case $line in *"$C"*) part=bottom; continue ;; esac 41 [ "$part" = "$1" ] || continue 42 case $line in 43 *"$T"*) printf '%s%s%s\n' "${line%%"$T"*}" "$title" "${line#*"$T"}" ;; 44 *"$H"*) printf '%s' "${line%%"$H"*}"; cat "$tpl/header.html"; printf '%s\n' "${line#*"$H"}" ;; 45 *"$F"*) printf '%s' "${line%%"$F"*}"; cat "$tpl/footer.html"; printf '%s\n' "${line#*"$F"}" ;; 46 *) printf '%s\n' "$line" ;; 47 esac 48 done < "$tpl/page.html" 49} 50 51# Start a page: the top of page.html, then the repo name, description and 52# nav that every page shares. Needs $title, and $root leading back to $out. 53begin() { 54 page top 55 printf '<h1 class="repo-title">%s</h1>\n' "$(esc "$reponame")" 56 printf '<div>%s</div>\n' "$(esc "$desc")" 57 printf '<div>git clone https://jhhm.no/repos/%s.git</div>\n' "$(esc "$reponame")" 58 printf '<nav class="repo-nav">\n' 59 printf '<a href="%s">about</a>\n<a href="%sfiles/">files</a>\n<a href="%slog/">log</a>\n' \ 60 "$root" "$root" "$root" 61 printf '</nav>\n' 62} 63 64# item LABEL LINK: one entry in a listing. 65item() { printf '<li><a href="./%s">%s</a></li>\n' "$(href "$2")" "$(esc "$1")"; } 66 67# A file as escaped monospace text. 68text() { printf '<pre>'; escape < "$1"; printf '</pre>\n'; } 69 70# Page content for one file. $1 is its raw copy, $mode its git mode. 71render() { 72 if [ "$mode" = 120000 ]; then 73 text "$1" # a symlink: the path it points to 74 elif ! tr -d '\000' < "$1" | cmp -s - "$1"; then 75 echo '<p><binary data></p>' # removing NUL bytes changed it 76 else 77 # "sed =" prints each line's number on a line of its own, 78 # the second sed joins the number onto the line after it. 79 printf '<pre>' 80 escape < "$1" | sed = | sed 'N; s/^\([0-9]*\)\n/<span class="ln">\1<\/span>/' 81 printf '</pre>\n' 82 fi 83} 84 85# Where repo path $1 goes under files/. A component named index.html would 86# clash with the listing of its directory, so it gets an "_" appended; any 87# component that is "index.html" plus only underscores gets one more, so 88# a real "index.html_" can't clash either. 89outpath() { 90 p= rest=$1 91 while [ -n "$rest" ]; do 92 c=${rest%%/*} 93 case $rest in */*) rest=${rest#*/} ;; *) rest= ;; esac 94 case $c in 95 index.html*) case ${c#index.html} in *[!_]*) ;; *) c=${c}_ ;; esac ;; 96 esac 97 p=${p:+$p/}$c 98 done 99 printf '%s\n' "$p" 100} 101 102# Set $root to the relative link from directory $1 (inside $out) to $out. 103up() { 104 root= d=$1 105 while :; do 106 root=../$root 107 case $d in */*) d=${d%/*} ;; *) break ;; esac 108 done 109} 110 111# entries DIR PREFIX: the <li> items for repo directory DIR ("" for the 112# root), linking to PREFIX + the entry's page. Directories have mode 040000 113# and everything else starts with 1, so sorting on the mode's first 114# character puts directories first, then by name. 115entries() { 116 g ls-tree "HEAD:$1" | LC_ALL=C sort -t "$tab" -k1.1,1.1 -k2 | 117 while IFS=$tab read -r meta name; do 118 case $name in \"*) continue ;; esac 119 case $meta in 120 040000*) label=$name/ ;; 121 160000*) continue ;; # submodules 122 *) label=$name ;; 123 esac 124 item "$label" "$2$(outpath "$name")/" 125 done 126} 127 128# Write the listing page for repo directory $1 ("" for the root). 129listing() { 130 o=$(outpath "$1") 131 title=$(esc "$reponame/${1:+$1/}") 132 up "files${o:+/$o}" 133 mkdir -p "$files/$o" 134 { 135 begin 136 printf '<h2 class="repo-path">%s</h2>\n<ul>\n' "$title" 137 [ -z "$1" ] || echo '<li><a href="../">../</a></li>' 138 entries "$1" "" 139 echo '</ul>' 140 page bottom 141 } > "$files/${o:+$o/}index.html" 142} 143 144mkdir -p "$raw" 145listing "" 146 147g ls-tree -r -t HEAD | while IFS=$tab read -r meta path; do 148 set -- $meta 149 mode=$1 type=$2 sha=$3 150 151 case $path in 152 \"*) printf 'skipping weird filename: %s\n' "$path" >&2; continue ;; 153 esac 154 155 case $type in 156 tree) 157 mkdir -p "$raw/$path" 158 listing "$path" 159 ;; 160 blob) 161 g cat-file blob "$sha" > "$raw/$path" 162 opath=$(outpath "$path") 163 up "files/$opath" 164 title=$(esc "$reponame/$path") 165 mkdir -p "$files/$opath" 166 { 167 begin 168 printf '<h2 class="repo-path">%s</h2>\n' "$title" 169 printf '<p>get raw file: <a href="%sraw/%s">%s</a></p>\n' "$root" "$(href "$path")" "$(basename "$(esc "$path")")" 170 render "$raw/$path" 171 page bottom 172 } > "$files/$opath/index.html" 173 ;; 174 esac 175done 176 177# The log page: the latest 50 commits, then a count of the rest. The 178# subject goes last in git's output so a tab inside it can't shift the 179# columns. The full subject is also in a title attribute, so it shows on 180# hover when the CSS cuts it short. 181title=$(esc "$reponame/log") root=../ 182more=$(($(g rev-list --count HEAD) - 50)) 183mkdir -p "$out/log" 184{ 185 begin 186 echo '<table class="log">' 187 echo '<colgroup><col class="date"><col class="message"><col class="author"></colgroup>' 188 echo '<tr><th>Date</th><th>Message</th><th>Author</th></tr>' 189 g log -n 50 --date=short --format='%ad%x09%an%x09%s' | escape | 190 while IFS=$tab read -r date author subject; do 191 printf '<tr><td>%s</td><td title="%s">%s</td><td>%s</td></tr>\n' \ 192 "$date" "$subject" "$subject" "$author" 193 done 194 echo '</table>' 195 if [ "$more" -gt 0 ]; then 196 printf '<p>And a whopping %s more can be fetched!</p>\n' "$more" 197 fi 198 page bottom 199} > "$out/log/index.html" 200 201# The homepage: the top of the tree (at most 10 entries), then the first 202# regular file (mode 100...) called README or README.*. 203list=$(entries "" files/) 204n=$(($(printf '%s\n' "$list" | wc -l))) 205readme=$(g ls-tree HEAD | while IFS=$tab read -r meta name; do 206 case $meta:$name in 207 100*:[Rr][Ee][Aa][Dd][Mm][Ee]|100*:[Rr][Ee][Aa][Dd][Mm][Ee].*) 208 printf '%s\n' "$name" 209 break ;; 210 esac 211done) 212 213title=$(esc "$reponame") root=./ 214{ 215 begin 216 # echo '<h2>files</h2>' 217 echo '<ul class="repo-filelist">' 218 printf '%s\n' "$list" | head -n 10 219 if [ "$n" -gt 10 ]; then 220 echo '<li><a href="./files/">... view all items</a></li>' 221 fi 222 echo '</ul>' 223 case $readme in 224 '') ;; 225 *.md|*.markdown) markup < "$raw/$readme" ;; 226 *) text "$raw/$readme" ;; 227 esac 228 page bottom 229} > "$out/index.html"