summaryrefslogtreecommitdiff
path: root/webgen.sh
blob: c8bd02481fb5f344d7744c548e0ab3f68ce9dd06 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh

TEMPLATEPAGE="./templates/page.html"
TEMPLATEBLOG="./templates/blog.html"
TEMPLATEATOM="./templates/atom.xml"
CONTENTPAGE="./content/page"
CONTENTBLOG="./content/blog"
OUTPUTDIR="./output"
STATIC="./static"

# Create necessary directories
mkdir -p templates content/page content/blog static
rm -rf "$OUTPUTDIR"
mkdir -p "$OUTPUTDIR"

# Copy static files
cp -r "$STATIC"/* "$OUTPUTDIR"

# Generate the index page
sed '/{{.Content}}/ {
    r ./content/index.html
    d
}' "$TEMPLATEPAGE" > "$OUTPUTDIR/index.html"

# Generate content pages
for file in "$CONTENTPAGE"/*.html; do
    name=$(basename -s .html "$file")
    mkdir -p "$OUTPUTDIR/$name/"
    sed '/{{.Content}}/ {
        r '"$file"'
        d
    }' "$TEMPLATEPAGE" > "$OUTPUTDIR/$name/index.html"
done

# Generate blog posts (only if 'post' is passed as argument)
if [ "$1" = "post" ]; then
    mkdir -p tmp
    for file in "$CONTENTBLOG"/*.html; do
        name=$(basename -s .html "$file")
        mkdir -p "$OUTPUTDIR/blog/$name/"
        sed '/{{.Content}}/ {
            r '"$file"'
            d
        }' "$TEMPLATEBLOG" > "$OUTPUTDIR/blog/$name/index.html"

        # Metadata
        post_date=$(sed -n 's/<!-- *date: *\([^ ]*\) *-->/\1/p' "$file")
        title=$(sed -n 's/<!-- *title: *\(.*[^ ]\) *-->/\1/p' "$file")
        domain=$(sed -n 's/<!-- *domain: *\(.*[^ ]\) *-->/\1/p' "$file")

        # Append to atom entry list
        {
            echo "        <entry>"
            echo "            <title>$title</title>"
            echo "            <link href=\"https://$domain/blog/$name\" />"
            echo "            <id>https://$domain/blog/$name/</id>"
            echo "            <updated>$post_date</updated>"
            echo "            <content type=\"xhtml\">"
            echo "                <div xmlns=\"http://www.w3.org/1999/xhtml\">"
            echo "                    $(cat "$file" | sed '/<!-- *\(date\|title\|domain\):.*-->/d' | sed 's/^/                    /')"
            echo "            </div>"
            echo "            </content>"
            echo "        </entry>"
            echo ""
        } >> tmp/entry.xml
    done

    # Generate atom with entries injected
    sed '/{{.Content}}/ {
        r tmp/entry.xml
        d
    }' "$TEMPLATEATOM" > "$OUTPUTDIR/atom.xml"

    date=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
    sed "s/{{.Date}}/$date/" "$OUTPUTDIR/atom.xml" > "$OUTPUTDIR/atom.xml.tmp"
    mv "$OUTPUTDIR/atom.xml.tmp" "$OUTPUTDIR/atom.xml"

    # Remove tmp directory
    rm -r tmp
fi