summaryrefslogtreecommitdiffstats
path: root/scripts/build_content.sh
blob: b7d0c24b80ae9b7ae771c56412bcf2d327a5eeed (plain) (blame)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash

CONTENT_DIR="content/"
OUTPUT_DIR="output/"

target_filename () {
    filename=$1
    filename=${filename#"$CONTENT_DIR"}
    if [ "${filename##*.}" = "md" ]; then
        filename="${filename%.md}.html"
    fi
    echo "$OUTPUT_DIR$filename"
}

target_url_rel () {
    basedir=$1
    filename=$2
    filename=$(realpath --relative-to="$basedir" "$filename")
    filename="${filename%.md}.html"
    if [ "${filename##*/}" = "index.html" ]; then
        filename=${filename%index.html}
    fi
    echo "$filename"
}

get_subpages_basedir() {
    filename=$1
    if [ "${filename##*/}" = "index.md" ]; then
        filename=$(dirname "$filename")
    fi
    echo "$filename"
}

get_subpages() {
    basedir=$(get_subpages_basedir "$1")
    child_pages=()

    if [ -d "$basedir" ]; then
        mapfile -d $'\0' child_pages_1 < <(find $basedir/ \
            -type f \
            -name "*.md" ! -name "index.md" \
            -maxdepth 1 \
            -print0)

        mapfile -d $'\0' child_pages_2 < <(find $basedir/ \
            -type f \
            -name "index.md" \
            -mindepth 2 \
            -maxdepth 2 \
            -print0)

        child_pages=("${child_pages_1[@]}" "${child_pages_2[@]}")
    fi

    if [ ${#child_pages[@]} -ne 0 ]; then
        echo -e "\033[0;90m[////////]\033[0m Child pages:"

        for file in "${child_pages[@]}"; do
            link=$(target_url_rel "$basedir" "$file")
            echo -e "\033[0;90m[////////]\033[0m   -  $file -> rel. URL: $link"
        done
    fi
}

handle () {
    target=$(target_filename "$1")
    mkdir -p $(dirname "$target")

    if [ "${1#*.}" = "md" ]; then
        echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $target"

        subpages_meta=$(mktemp)

        # $(get_subpages "$1") > "$subpages_meta"

        get_subpages "$1"

        pandoc "$1" \
            -f markdown \
            -t html5 \
            --template templates/base.html \
            -o "$target" \
            --metadata-file metadata/metadata.yaml \
            --metadata-file "$subpages_meta"

        rm "$subpages_meta"
    else
        echo -e "\033[0;32m[COPY    ]\033[0m $1 -> $target"

        cp "$1" "$target"
    fi
}

if [ -z "$1" ]; then
    find "$CONTENT_DIR" \
        -type f \
        | while read file
            do
                handle "$file"
            done
elif [ "$1" = "all_md" ]; then
    find "$CONTENT_DIR" \
        -type f \
        -name "*.md" \
        | while read file
            do
                handle "$file"
            done
elif [ "$1" = "single" ]; then
    if [ -z "$2" ]; then
        echo -e "\033[0;31m[ERROR   ]\033[0m \"single\" operation requires file argument"
    else
        handle "$2"
    fi
elif [ "$1" = "delete" ]; then
    if [ -z "$2" ]; then
        echo -e "\033[0;31m[ERROR   ]\033[0m \"delete\" operation requires file argument"
    else
        TARGET=$(target_filename "$2")
        echo -e "\033[0;32m[DELETE  ]\033[0m $2 -> $TARGET"
        rm -rf $TARGET
    fi
else
    echo -e "\033[0;31m[ERROR   ]\033[0m Unknown operation: \"$1\""
fi