ssw

Suckless static webframework
Log | Files | Refs | LICENSE

ssw (1458B)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/bin/sh

die() { echo $*; exit 1; }

gen_html() {
    cmd="$("$PPROC" < "$1")"
    tmpfile=$(mktemp)
    eval "$cmd" > $tmpfile
    type 'ongen' &>/dev/null && ongen $1
    $PARSER < $tmpfile > $tmpfile.out
    [ -n "$page_layout" ] || page_layout="$LAYOUT"
    sed \
        -e 's^{%\s*title\s*%}^'"$page_title"'^' \
        -e "s/{%\s*description\s*%}/$page_description/" \
        -e "/{%\s*content\s*%}/ r $tmpfile.out" \
        -e "s/{%\s*content\s*%}//" \
        "$LDIR/$page_layout" > "$2"
    rm $tmpfile; rm $tmpfile.out
}

DIR=$1
cd "$DIR" || die "Wrong directory: $DIR"
CWD=$(pwd)

[ -f config.sh ] && . ./config.sh
[ -n "$EXT" ]     || EXT=".smu"
[ -n "$PARSER" ]  || PARSER="smu"
[ -n "$PPROC" ]   || PPROC="sapp"
[ -n "$IN" ]      || IN="src"; IDIR="$CWD/$IN"
[ -n "$OUT" ]     || OUT="output"; ODIR="$CWD/$OUT"
[ -n "$LAYOUT" ]  || LAYOUT="default.html";
[ -n "$LAYOUTS" ] || LAYOUTS="layouts"; LDIR="$CWD/$LAYOUTS"

cd "$IDIR" || die "Input directory does not exist: $IDIR"
mkdir -p $ODIR
cp -rf * $ODIR
rm -f $(find $ODIR -type f -iname "*$EXT")

if [ -f "$CWD/.timestamp" ]; then
    FILES=$(find . -anewer "$CWD/.timestamp" -type f -iname "*$EXT" | sed -e 's#^\./##')
else
    FILES=$(find . -type f -iname "*$EXT" | sed -e 's#^\./##')
fi

for f in $FILES; do
    of="$(echo $f | sed -e "s,$EXT$,.html,g")"
    printf "\033[1m$f\033[0m\n"
    gen_html "$f" "$ODIR/$of"
done

touch "$CWD/.timestamp"
type 'onexit' &>/dev/null && onexit