master build.sh
  1#!/bin/sh
  2
  3set -e
  4
  5verbose=1
  6
  7log() {
  8    [ "$verbose" = 1 ] && echo "$1"
  9}
 10
 11err() {
 12    echo "[error] $1" >&2
 13}
 14
 15setup() {
 16    d=$(dirname "$0")
 17    cf="$d/config.env"
 18    
 19    if [ ! -f "$cf" ]; then
 20        err "config.env not found in $d"
 21        exit 1
 22    fi
 23    
 24    while IFS='=' read -r k val; do
 25        case "$k" in
 26            TITLE) TITLE="$val" ;;
 27            FOOTER) FOOTER="$val" ;;
 28            OUTPUT) OUTPUT="$val" ;;
 29            INPUT) INPUT="$val" ;;
 30            NAVDIR) NAVDIR="$val" ;;
 31            NAVFILE) NAVFILE="$val" ;;
 32            NAVCURRENT) NAVCURRENT="$val" ;;
 33        esac
 34    done < "$cf"
 35    
 36    case "$OUTPUT" in
 37        /*) ;;
 38        ~*) OUTPUT="$HOME${OUTPUT#\~}" ;;
 39        *) OUTPUT="$d/$OUTPUT" ;;
 40    esac
 41    
 42    case "$INPUT" in
 43        /*) ;;
 44        ~*) INPUT="$HOME${INPUT#\~}" ;;
 45        *) INPUT="$d/$INPUT" ;;
 46    esac
 47    
 48    export TITLE FOOTER OUTPUT INPUT NAVDIR NAVFILE NAVCURRENT
 49    
 50    if [ -z "$TITLE" ] || [ -z "$FOOTER" ] || [ -z "$OUTPUT" ] || [ -z "$INPUT" ] || [ -z "$NAVDIR" ] || [ -z "$NAVFILE" ] || [ -z "$NAVCURRENT" ]; then
 51        err "missing variables in config.env"
 52        exit 1
 53    fi
 54    
 55    log "TITLE=$TITLE"
 56    log "INPUT=$INPUT"
 57    log "OUTPUT=$OUTPUT"
 58    log "NAVDIR=$NAVDIR"
 59    log "NAVFILE=$NAVFILE"
 60    log "NAVCURRENT=$NAVCURRENT"
 61    
 62    if [ ! -d "$INPUT" ]; then
 63        err "source directory not found: $INPUT"
 64        exit 1
 65    fi
 66    if [ ! -f "$INPUT/template.html" ]; then
 67        err "template.html not found in $INPUT"
 68        exit 1
 69    fi
 70    
 71    rm -rf "$OUTPUT"
 72    mkdir -p "$OUTPUT"
 73    log "setup complete"
 74}
 75
 76cpstat() {
 77    log "copying static files from $INPUT..."
 78    
 79    copyfiles() {
 80        for f in "$1"/* "$1"/.[!.]*; do
 81            [ -e "$f" ] || continue
 82            if [ -d "$f" ]; then
 83                copyfiles "$f"
 84            elif [ -f "$f" ]; then
 85                case "$f" in
 86                    *.md|*/template.html) continue ;;
 87                    *)
 88                        rel="${f#$INPUT/}"
 89                        mkdir -p "$(dirname "$OUTPUT/$rel")"
 90                        cp "$f" "$OUTPUT/$rel"
 91                        log "  copied $f -> $OUTPUT/$rel"
 92                        ;;
 93                esac
 94            fi
 95        done
 96    }
 97    
 98    copyfiles "$INPUT"
 99    log "static copy complete"
100}
101
102md2h() {
103    cmark -t html "$1" | sed 's/&quot;/"/g; s/&#39;/'"'"'/g'
104}
105
106tname() {
107    name=${1##*/}
108    name=${name%.md}
109    echo "$name" | tr '-' ' '
110}
111
112bnav() {
113    dir="$1"
114    cur="$2"
115    depth="$3"
116    indent=""
117    i=0
118    while [ "$i" -lt "$depth" ]; do
119        indent="${indent}  "
120        i=$((i + 1))
121    done
122
123    for item in "$dir"/*; do
124        [ ! -e "$item" ] && continue
125        name=$(basename "$item")
126
127        if [ -d "$item" ]; then
128            if [ -f "$item/index.md" ]; then
129                dp="${item#$INPUT}"
130                [ -z "$dp" ] && dp="/"
131                echo "${indent}<li><a href=\"${dp}/index.html\">${name}${NAVDIR}</a>"
132            else
133                echo "${indent}<li>${name}${NAVDIR}"
134            fi
135            echo "${indent}  <ul>"
136            bnav "$item" "$cur" $((depth + 1))
137            echo "${indent}  </ul>"
138            echo "${indent}</li>"
139        elif [ -f "$item" ] && [ "${item%.md}" != "$item" ]; then
140            [ "$name" = "index.md" ] && continue
141            rel="${item#$INPUT/}"
142            rel="${rel%.md}.html"
143            title=$(tname "$item")
144            if [ "/$rel" = "$cur" ]; then
145                echo "${indent}<li><a href=\"/$rel\" class=\"active-page\"><span class=\"active-indicator\">${NAVCURRENT}</span>${title}</a></li>"
146            else
147                echo "${indent}<li><a href=\"/$rel\">${NAVFILE}${title}</a></li>"
148            fi
149        fi
150    done
151}
152
153rnav() {
154    echo "<ul>"
155    bnav "$INPUT" "$1" 1
156    echo "</ul>"
157}
158
159apply() {
160    awk -v t="$TITLE" -v n="$1" -v c="$2" -v f="$FOOTER" '
161    {
162        gsub(/\{\{TITLE\}\}/, t)
163        gsub(/\{\{NAV\}\}/, n)
164        gsub(/\{\{CONTENT\}\}/, c)
165        gsub(/\{\{FOOTER\}\}/, f)
166        print
167    }
168    ' "$INPUT/template.html"
169}
170
171main() {
172    setup
173    
174    S="$INPUT"
175    S="${S#./}"
176    S="${S%/}"
177    log "using source directory: $S"
178    
179    cpstat
180
181    procmd() {
182        for f in "$1"/*; do
183            [ -e "$f" ] || continue
184            if [ -d "$f" ]; then
185                procmd "$f"
186            elif [ -f "$f" ] && [ "${f##*.}" = "md" ]; then
187                f="${f#./}"
188                r="${f#$S/}"
189                r="${r%.md}.html"
190                mkdir -p "$(dirname "$OUTPUT/$r")"
191                h=$(md2h "$f")
192                c="/$r"
193                apply "$(rnav "$c")" "$h" > "$OUTPUT/$r"
194                log "wrote $OUTPUT/$r"
195            fi
196        done
197    }
198    
199    procmd "$S"
200    log "build complete"
201}
202
203main