summaryrefslogtreecommitdiffstats
path: root/scripts/build_content.sh
blob: 3dea44f724978d92d312d43d209f2aceb795eab6 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash

source "site.defaults.conf"

get_filters() {
    local filters_args=""

    mapfile -d $'\0' filters < <(find $FILTERS_DIR \
        -type f \
        -name "*.lua" ! -name ".*" \
        -print0)

    for file in "${filters[@]}"; do
        filters_args="$filters_args --lua-filter $file"
    done

    echo "$filters_args"
}

FILTERS=$(get_filters)

get_filename_out() {
    local filename=$1
    filename=${filename#"$CONTENT_DIR"}
    echo "$OUTPUT_DIR$filename"
}

get_metadata() {
    pandoc "$1" \
        -f markdown-citations \
        -t plain \
        --metadata content_dir="$CONTENT_DIR" \
        --metadata file_in="$1" \
        --metadata relative_to="$2" \
        --metadata-file metadata/metadata.yaml \
        --no-highlight \
        --template scripts/metadata_tpl.json \
        --lua-filter scripts/metadata_filter.lua
}

get_content() {
    pandoc "$1" \
        -f markdown-citations \
        -t markdown \
        --metadata content_dir="$CONTENT_DIR" \
        --metadata file_in="$1" \
        --metadata-file metadata/metadata.yaml \
        --no-highlight \
        --lua-filter scripts/metadata_filter.lua
}

get_subpages_metadata() {
    local base_dir_in=${1%/index.md}
    local relative_to="$2"
    if [ -z "$2" ]; then
        relative_to="$1"
    fi
    local child_pages=()

    if [ -d "$base_dir_in" ]; then
        mapfile -d $'\0' child_pages < <(find "$base_dir_in" \
            -maxdepth 2 \
            -type f \
            -wholename "$base_dir_in/*/index.md" \
            ! -name "_*.md" \
            -print0)
    fi

    local pages="[]"
    local subsections="{}"

    for file_in in "${child_pages[@]}"; do
        local subsection=$(basename "${file_in%/index.md}")
        local content=$(get_content "$file_in" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\\"/g')
        local metadata=$(get_metadata "$file_in" | jq "{ \
            file_out: .file_out, \
            url: .url, \
            author: .author, \
            title: .title, \
            date: .date, \
            last_update: .last_update, \
            category: .category, \
            preview: .preview, \
            content: \"$content\" \
        } | del(.[] | nulls)")
        local title=$(echo "$metadata" | jq ".title")
        local subpages="[]"

        if [ -z "$2" ]; then
            subpages=$(get_subpages_metadata "$file_in" "$1" | jq ".pages")
        fi

        pages=$(echo "$pages" | jq ". += [ $metadata ]")
        subsections=$(echo "$subsections" | jq ". += { \"$subsection\": { title: $title, pages: $subpages } }")
    done

    echo "{ \"pages\": $pages, \"subsections\": $subsections }"
}

handle () {
    if [ "${1#*.}" = "md" ]; then
        local included_metadata=$(get_metadata "$1")
        local file_out=$(echo "$included_metadata" | jq -r .file_out)
        file_out="$OUTPUT_DIR${file_out#/}"
        local create_feed=$(echo "$included_metadata" | jq -r .create_feed)

        mkdir -p $(dirname "$file_out")

        echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out"

        echo -e "\033[0;90m[COMPILE ]\033[0m     Getting subpages"

        local added_metadata=$(get_subpages_metadata "$1")

        local meta_file=$(mktemp)

        echo "$added_metadata" > "$meta_file"
            
        if [ "$create_feed" = "true" ]; then
            echo -e "\033[0;90m[COMPILE ]\033[0m     Creating feed"

            pandoc "$1" \
                -f markdown-citations \
                -t html5 \
                --no-highlight \
                --template "${TEMPLATES_DIR}feed.xml" \
                -o "${file_out%.html}.xml" \
                --metadata content_dir="$CONTENT_DIR" \
                --metadata file_in="$1" \
                --metadata page_type=feed \
                --metadata-file metadata/metadata.yaml \
                --metadata-file "$meta_file" \
                --lua-filter scripts/metadata_filter.lua
        fi

        echo -e "\033[0;90m[COMPILE ]\033[0m     Creating page"

        pandoc "$1" \
            -f markdown-citations \
            -t html5 \
            --no-highlight \
            --template "${TEMPLATES_DIR}base.html" \
            -o "$file_out" \
            --metadata content_dir="$CONTENT_DIR" \
            --metadata file_in="$1" \
            --metadata-file metadata/metadata.yaml \
            --metadata-file "$meta_file" \
            --lua-filter scripts/metadata_filter.lua \
            $FILTERS

        # echo "$(pandoc "$1" \
        #     -f markdown-citations \
        #     -t plain \
        #     --no-highlight \
        #     --template scripts/metadata_tpl.json \
        #     --metadata content_dir="$CONTENT_DIR" \
        #     --metadata file_in="$1" \
        #     --metadata-file metadata/metadata.yaml \
        #     --metadata-file "$meta_file" \
        #     --lua-filter scripts/metadata_filter.lua \
        #     $FILTERS)"

        rm "$meta_file"

        echo -e "\033[0;90m[COMPILE ]\033[0m     Done"
    else
        local file_out=$(get_filename_out "$1")

        mkdir -p $(dirname "$file_out")

        echo -e "\033[0;32m[COPY    ]\033[0m $1 -> $file_out"

        cp "$1" "$file_out"
    fi
}

mdfilter=""
if [ "$LIVE" != true ]; then
    mdfilter="! -name _*.md"
fi

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