aboutsummaryrefslogtreecommitdiff
path: root/sswg.sh
blob: d62233d969b4c1a20e59444c8e576e6bc5d698db (plain)
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
# SSWG - Simple Static Website Generator
#!/bin/sh

SSWG_OUTPUT_DIR="_static"
SSWG_ASSETS_DIR="_assets"
SSWG_PAGES_DIR="_pages"
SSWG_HEADER_TEMPLATE="_header.t.html"
SSWG_FOOTER_TEMPLATE="_footer.t.html"

rm -rf "$SSWG_OUTPUT_DIR"
mkdir "$SSWG_OUTPUT_DIR"

# Copy static assets
cp -r "$SSWG_ASSETS_DIR"/* "$SSWG_OUTPUT_DIR"/.

# Generate HTML pages
find "$SSWG_PAGES_DIR" -iname '*.html' -o -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' | while read -r page;
do
    filename="$SSWG_OUTPUT_DIR/${page##$SSWG_PAGES_DIR/}"
    mkdir -p "$(dirname "$filename")"

    if [ "${filename##*.}" = "html" ]; then
        # prepose header
        cat "$SSWG_HEADER_TEMPLATE" >> "$filename"

        # Fix indentation in the generated source
        cat "$page" | awk '
        BEGIN {print ""}
        FNR>3 {print "      " $0}
        END {print ""}' >> "$filename"

        # append footer
        cat "$SSWG_FOOTER_TEMPLATE" >> "$filename"

        # macro substitution
        eval `cat "$page" | awk 'FNR<3'`
        sed -i'' "s@TITLE@$TITLE@g" "$filename"
        sed -i'' "s@DESCRIPTION@$DESCRIPTION@g" "$filename"

        # remove comments
        sed -i'' "/^[ \t]*#/d" "$filename"

        # remove indentation before whitespaces+pipe
        #   useful e.g. with <pre> tags
        sed -i'' "s/^[ \t]*|//g" "$filename"  # remove
    else
        cp "$page" "$filename"
    fi;
done;