diff options
| author | Volpeon <git@volpeon.ink> | 2021-05-10 16:41:05 +0200 |
|---|---|---|
| committer | Volpeon <git@volpeon.ink> | 2021-05-10 16:41:05 +0200 |
| commit | a48d05d1d5fcff414359c8ba6cc8f38467aebdeb (patch) | |
| tree | 3542bf116c910a1bb42b40d8531f60c2c2498b08 /scripts/build_content.sh | |
| parent | Update (diff) | |
| download | volpeon.ink-a48d05d1d5fcff414359c8ba6cc8f38467aebdeb.tar.gz volpeon.ink-a48d05d1d5fcff414359c8ba6cc8f38467aebdeb.tar.bz2 volpeon.ink-a48d05d1d5fcff414359c8ba6cc8f38467aebdeb.zip | |
Refactoring to fully take advantage of Make
Diffstat (limited to 'scripts/build_content.sh')
| -rwxr-xr-x | scripts/build_content.sh | 222 |
1 files changed, 0 insertions, 222 deletions
diff --git a/scripts/build_content.sh b/scripts/build_content.sh deleted file mode 100755 index 3dea44f..0000000 --- a/scripts/build_content.sh +++ /dev/null | |||
| @@ -1,222 +0,0 @@ | |||
| 1 | #!/bin/bash | ||
| 2 | |||
| 3 | source "site.defaults.conf" | ||
| 4 | |||
| 5 | get_filters() { | ||
| 6 | local filters_args="" | ||
| 7 | |||
| 8 | mapfile -d $'\0' filters < <(find $FILTERS_DIR \ | ||
| 9 | -type f \ | ||
| 10 | -name "*.lua" ! -name ".*" \ | ||
| 11 | -print0) | ||
| 12 | |||
| 13 | for file in "${filters[@]}"; do | ||
| 14 | filters_args="$filters_args --lua-filter $file" | ||
| 15 | done | ||
| 16 | |||
| 17 | echo "$filters_args" | ||
| 18 | } | ||
| 19 | |||
| 20 | FILTERS=$(get_filters) | ||
| 21 | |||
| 22 | get_filename_out() { | ||
| 23 | local filename=$1 | ||
| 24 | filename=${filename#"$CONTENT_DIR"} | ||
| 25 | echo "$OUTPUT_DIR$filename" | ||
| 26 | } | ||
| 27 | |||
| 28 | get_metadata() { | ||
| 29 | pandoc "$1" \ | ||
| 30 | -f markdown-citations \ | ||
| 31 | -t plain \ | ||
| 32 | --metadata content_dir="$CONTENT_DIR" \ | ||
| 33 | --metadata file_in="$1" \ | ||
| 34 | --metadata relative_to="$2" \ | ||
| 35 | --metadata-file metadata/metadata.yaml \ | ||
| 36 | --no-highlight \ | ||
| 37 | --template scripts/metadata_tpl.json \ | ||
| 38 | --lua-filter scripts/metadata_filter.lua | ||
| 39 | } | ||
| 40 | |||
| 41 | get_content() { | ||
| 42 | pandoc "$1" \ | ||
| 43 | -f markdown-citations \ | ||
| 44 | -t markdown \ | ||
| 45 | --metadata content_dir="$CONTENT_DIR" \ | ||
| 46 | --metadata file_in="$1" \ | ||
| 47 | --metadata-file metadata/metadata.yaml \ | ||
| 48 | --no-highlight \ | ||
| 49 | --lua-filter scripts/metadata_filter.lua | ||
| 50 | } | ||
| 51 | |||
| 52 | get_subpages_metadata() { | ||
| 53 | local base_dir_in=${1%/index.md} | ||
| 54 | local relative_to="$2" | ||
| 55 | if [ -z "$2" ]; then | ||
| 56 | relative_to="$1" | ||
| 57 | fi | ||
| 58 | local child_pages=() | ||
| 59 | |||
| 60 | if [ -d "$base_dir_in" ]; then | ||
| 61 | mapfile -d $'\0' child_pages < <(find "$base_dir_in" \ | ||
| 62 | -maxdepth 2 \ | ||
| 63 | -type f \ | ||
| 64 | -wholename "$base_dir_in/*/index.md" \ | ||
| 65 | ! -name "_*.md" \ | ||
| 66 | -print0) | ||
| 67 | fi | ||
| 68 | |||
| 69 | local pages="[]" | ||
| 70 | local subsections="{}" | ||
| 71 | |||
| 72 | for file_in in "${child_pages[@]}"; do | ||
| 73 | local subsection=$(basename "${file_in%/index.md}") | ||
| 74 | local content=$(get_content "$file_in" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\\"/g') | ||
| 75 | local metadata=$(get_metadata "$file_in" | jq "{ \ | ||
| 76 | file_out: .file_out, \ | ||
| 77 | url: .url, \ | ||
| 78 | author: .author, \ | ||
| 79 | title: .title, \ | ||
| 80 | date: .date, \ | ||
| 81 | last_update: .last_update, \ | ||
| 82 | category: .category, \ | ||
| 83 | preview: .preview, \ | ||
| 84 | content: \"$content\" \ | ||
| 85 | } | del(.[] | nulls)") | ||
| 86 | local title=$(echo "$metadata" | jq ".title") | ||
| 87 | local subpages="[]" | ||
| 88 | |||
| 89 | if [ -z "$2" ]; then | ||
| 90 | subpages=$(get_subpages_metadata "$file_in" "$1" | jq ".pages") | ||
| 91 | fi | ||
| 92 | |||
| 93 | pages=$(echo "$pages" | jq ". += [ $metadata ]") | ||
| 94 | subsections=$(echo "$subsections" | jq ". += { \"$subsection\": { title: $title, pages: $subpages } }") | ||
| 95 | done | ||
| 96 | |||
| 97 | echo "{ \"pages\": $pages, \"subsections\": $subsections }" | ||
| 98 | } | ||
| 99 | |||
| 100 | handle () { | ||
| 101 | if [ "${1#*.}" = "md" ]; then | ||
| 102 | local included_metadata=$(get_metadata "$1") | ||
| 103 | local file_out=$(echo "$included_metadata" | jq -r .file_out) | ||
| 104 | file_out="$OUTPUT_DIR${file_out#/}" | ||
| 105 | local create_feed=$(echo "$included_metadata" | jq -r .create_feed) | ||
| 106 | |||
| 107 | mkdir -p $(dirname "$file_out") | ||
| 108 | |||
| 109 | echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out" | ||
| 110 | |||
| 111 | echo -e "\033[0;90m[COMPILE ]\033[0m Getting subpages" | ||
| 112 | |||
| 113 | local added_metadata=$(get_subpages_metadata "$1") | ||
| 114 | |||
| 115 | local meta_file=$(mktemp) | ||
| 116 | |||
| 117 | echo "$added_metadata" > "$meta_file" | ||
| 118 | |||
| 119 | if [ "$create_feed" = "true" ]; then | ||
| 120 | echo -e "\033[0;90m[COMPILE ]\033[0m Creating feed" | ||
| 121 | |||
| 122 | pandoc "$1" \ | ||
| 123 | -f markdown-citations \ | ||
| 124 | -t html5 \ | ||
| 125 | --no-highlight \ | ||
| 126 | --template "${TEMPLATES_DIR}feed.xml" \ | ||
| 127 | -o "${file_out%.html}.xml" \ | ||
| 128 | --metadata content_dir="$CONTENT_DIR" \ | ||
| 129 | --metadata file_in="$1" \ | ||
| 130 | --metadata page_type=feed \ | ||
| 131 | --metadata-file metadata/metadata.yaml \ | ||
| 132 | --metadata-file "$meta_file" \ | ||
| 133 | --lua-filter scripts/metadata_filter.lua | ||
| 134 | fi | ||
| 135 | |||
| 136 | echo -e "\033[0;90m[COMPILE ]\033[0m Creating page" | ||
| 137 | |||
| 138 | pandoc "$1" \ | ||
| 139 | -f markdown-citations \ | ||
| 140 | -t html5 \ | ||
| 141 | --no-highlight \ | ||
| 142 | --template "${TEMPLATES_DIR}base.html" \ | ||
| 143 | -o "$file_out" \ | ||
| 144 | --metadata content_dir="$CONTENT_DIR" \ | ||
| 145 | --metadata file_in="$1" \ | ||
| 146 | --metadata-file metadata/metadata.yaml \ | ||
| 147 | --metadata-file "$meta_file" \ | ||
| 148 | --lua-filter scripts/metadata_filter.lua \ | ||
| 149 | $FILTERS | ||
| 150 | |||
| 151 | # echo "$(pandoc "$1" \ | ||
| 152 | # -f markdown-citations \ | ||
| 153 | # -t plain \ | ||
| 154 | # --no-highlight \ | ||
| 155 | # --template scripts/metadata_tpl.json \ | ||
| 156 | # --metadata content_dir="$CONTENT_DIR" \ | ||
| 157 | # --metadata file_in="$1" \ | ||
| 158 | # --metadata-file metadata/metadata.yaml \ | ||
| 159 | # --metadata-file "$meta_file" \ | ||
| 160 | # --lua-filter scripts/metadata_filter.lua \ | ||
| 161 | # $FILTERS)" | ||
| 162 | |||
| 163 | rm "$meta_file" | ||
| 164 | |||
| 165 | echo -e "\033[0;90m[COMPILE ]\033[0m Done" | ||
| 166 | else | ||
| 167 | local file_out=$(get_filename_out "$1") | ||
| 168 | |||
| 169 | mkdir -p $(dirname "$file_out") | ||
| 170 | |||
| 171 | echo -e "\033[0;32m[COPY ]\033[0m $1 -> $file_out" | ||
| 172 | |||
| 173 | cp "$1" "$file_out" | ||
| 174 | fi | ||
| 175 | } | ||
| 176 | |||
| 177 | mdfilter="" | ||
| 178 | if [ "$LIVE" != true ]; then | ||
| 179 | mdfilter="! -name _*.md" | ||
| 180 | fi | ||
| 181 | |||
| 182 | if [ -z "$1" ]; then | ||
| 183 | find "$CONTENT_DIR" \ | ||
| 184 | -type f \ | ||
| 185 | ! -name ".*" \ | ||
| 186 | $mdfilter \ | ||
| 187 | | while read file_in | ||
| 188 | do | ||
| 189 | handle "$file_in" | ||
| 190 | done | ||
| 191 | elif [ "$1" = "all_md" ]; then | ||
| 192 | find "$CONTENT_DIR" \ | ||
| 193 | -type f \ | ||
| 194 | -name "*.md" \ | ||
| 195 | ! -name ".*" \ | ||
| 196 | $mdfilter \ | ||
| 197 | | while read file_in | ||
| 198 | do | ||
| 199 | handle "$file_in" | ||
| 200 | done | ||
| 201 | elif [ "$1" = "single" ]; then | ||
| 202 | if [ -z "$2" ]; then | ||
| 203 | echo -e "\033[0;31m[ERROR ]\033[0m \"single\" operation requires file argument" | ||
| 204 | else | ||
| 205 | filename=$(basename "$2") | ||
| 206 | if [ "${filename:0:1}" != "_" ]; then | ||
| 207 | handle "$2" | ||
| 208 | fi | ||
| 209 | fi | ||
| 210 | elif [ "$1" = "delete" ]; then | ||
| 211 | if [ -z "$2" ]; then | ||
| 212 | echo -e "\033[0;31m[ERROR ]\033[0m \"delete\" operation requires file argument" | ||
| 213 | else | ||
| 214 | file_out=$(get_filename_out "$2") | ||
| 215 | if [ -f "$file_out" ] || [ -d "$file_out" ]; then | ||
| 216 | echo -e "\033[0;32m[DELETE ]\033[0m $2 -> $file_out" | ||
| 217 | rm -rf $file_out | ||
| 218 | fi | ||
| 219 | fi | ||
| 220 | else | ||
| 221 | echo -e "\033[0;31m[ERROR ]\033[0m Unknown operation: \"$1\"" | ||
| 222 | fi | ||
