summaryrefslogtreecommitdiffstats
path: root/scripts/build_content.sh
blob: 0b2ce2e16ecdc71d22ea73c3b84b8f9a1c6c5860 (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
#!/bin/bash

source "site.defaults.conf"

if [ -f "site.conf" ]; then
    source "site.conf"
fi

get_filters() {
    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_target_filename() {
    filename=$1
    filename=${filename#"$CONTENT_DIR"}
    if [ "${filename##*.}" = "md" ]; then
        filename="${filename%.md}.html"
    fi
    echo "$OUTPUT_DIR$filename"
}

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

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

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

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

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

    pages="[]"

    if [ ${#child_pages[@]} -ne 0 ]; then
        for file in "${child_pages[@]}"; do
            metadata=$(get_metadata "$1" "$file" | jq "{ \
                url: .url, \
                author: .author, \
                title: .title, \
                date: .date, \
                last_update: .last_update, \
                category: .category, \
                content: \"$(get_content "$1" "$file" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g')\" \
            } | del(.[] | nulls)")

            pages=$(echo "$pages" | jq ". += [ $metadata ]")
        done
    fi

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

handle () {
    target_file=$(get_target_filename "$1")

    mkdir -p $(dirname "$target_file")

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

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

        included_metadata=$(get_metadata "$1" "$1")
        added_metadata=$(get_subpages_metadata "$1")

        create_feed=$(echo "$included_metadata" | jq -r .create_feed)

        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 \
                -t html5 \
                --no-highlight \
                --template "${TEMPLATES_DIR}feed.xml" \
                -o "${target_file%.html}.xml" \
                --metadata content_dir="$CONTENT_DIR" \
                --metadata file="$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 \
            -t html5 \
            --no-highlight \
            --template "${TEMPLATES_DIR}base.html" \
            -o "$target_file" \
            --metadata content_dir="$CONTENT_DIR" \
            --metadata file="$1" \
            --metadata-file metadata/metadata.yaml \
            --metadata-file "$meta_file" \
            --lua-filter scripts/metadata_filter.lua \
            $FILTERS

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

        rm "$meta_file"

        # echo "$included_metadata"
        # echo "$added_metadata"

        echo -e "\033[0;90m[COMPILE ]\033[0m     Done"
    else
        echo -e "\033[0;32m[COPY    ]\033[0m $1 -> $target_file"

        cp "$1" "$target_file"
    fi
}

if [ -z "$1" ]; then
    find "$CONTENT_DIR" \
        -type f \
        ! -name "_*.md" \
        | while read file
            do
                handle "$file"
            done
elif [ "$1" = "all_md" ]; then
    find "$CONTENT_DIR" \
        -type f \
        -name "*.md" \
        ! -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
        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
        target_file=$(get_target_filename "$2")
        if [ -f "$target_file" ] || [ -d "$target_file" ]; then
            echo -e "\033[0;32m[DELETE  ]\033[0m $2 -> $target_file"
            rm -rf $target_file
        fi
    fi
else
    echo -e "\033[0;31m[ERROR   ]\033[0m Unknown operation: \"$1\""
fi