From 1bea0b7b1ecf0b04f724e2cda575df8bdbb70ad2 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 29 Dec 2020 21:05:48 +0100 Subject: Improved metadata generation, added ATOM feeds --- scripts/build_content.sh | 171 ++++++++++++++++++++++++++++++---------------- scripts/metadata_tpl.json | 3 +- 2 files changed, 113 insertions(+), 61 deletions(-) (limited to 'scripts') diff --git a/scripts/build_content.sh b/scripts/build_content.sh index b258449..e2b3bb3 100755 --- a/scripts/build_content.sh +++ b/scripts/build_content.sh @@ -1,13 +1,13 @@ #!/bin/bash +SITE="https://volpeon.ink/" + CONTENT_DIR="content/" FILTERS_DIR="filters/" METADATA_DIR="metadata/" TEMPLATES_DIR="templates/" OUTPUT_DIR="output/" -CATEGORY_MAP=$(cat "$METADATA_DIR/categories.json") - get_filters() { filters_args="" @@ -37,16 +37,22 @@ target_filename() { target_url_rel() { basedir=$1 filename=$2 + ext=$3 filename=$(realpath --relative-to="$basedir" "$filename") - filename="${filename%.md}.html" - if [ "${filename##*/}" = "index.html" ]; then - filename=${filename%index.html} - fi + filename="${filename%.md}.$ext" + filename=${filename%index.html} echo "$filename" } target_url_abs() { - echo "/$(target_url_rel "$CONTENT_DIR" "$1")" + echo "/$(target_url_rel "$CONTENT_DIR" "$1" "$2")" +} + +metadata() { + pandoc "$1" \ + -f markdown \ + -t plain \ + --template scripts/metadata_tpl.json } get_section() { @@ -65,7 +71,7 @@ get_subpages_basedir() { echo "$filename" } -get_metadata() { +get_subpages_meta() { basedir=$(get_subpages_basedir "$1") child_pages=() @@ -86,42 +92,44 @@ get_metadata() { child_pages=("${child_pages_1[@]}" "${child_pages_2[@]}") fi + pages=$(echo "{}" | jq ". + { \ + all: [], \ + by_year: {} \ + }") + if [ ${#child_pages[@]} -ne 0 ]; then #echo -e "\033[0;90m[////////]\033[0m Child pages:" - pages=$(echo "{}" | jq ". + { \ - all: [], \ - by_year: {} \ - }") - - categories="{}" - for file in "${child_pages[@]}"; do #echo -e "\033[0;90m[////////]\033[0m - $file" - url_rel=$(target_url_rel "$basedir" "$file") - url_abs=$(target_url_abs "$file") + metadata=$(metadata "$file") - metadata=$(pandoc "$file" \ - -f markdown \ - -t plain \ - --template scripts/metadata_tpl.json) + url_rel=$(target_url_rel "$basedir" "$file" "html") + url_abs=$(target_url_abs "$file" "html") + url_full="${SITE%/}$url_abs" date=$(echo "$metadata" | jq -r .date) - category=$(echo "$metadata" | jq -r .category) - category_name=$(echo "$CATEGORY_MAP" | jq -r .$category) + date_rfc3339="" - metadata=$(echo "$metadata" | jq ". + { \ - url_rel: \"$url_rel\", \ - url_abs: \"$url_abs\" \ + if [ "$date" != "" ]; then + date_rfc3339=$(date -d "$date" --rfc-3339=seconds | sed 's/ /T/') + fi + + metadata=$(echo "$metadata" | jq ". * { \ + date: { \ + yyyy_mm_dd: \"$date\", \ + rfc3339: \"$date_rfc3339\" \ + }, \ + url: { \ + rel: \"$url_rel\", \ + abs: \"$url_abs\", \ + full: \"$url_full\" \ + } \ }") pages=$(echo "$pages" | jq ".all += [ $metadata ]") - if [ "$category" != "" ]; then - categories=$(echo "$categories" | jq ". + { $category: \"$category_name\" }") - fi - if [ "$date" != "" ]; then date_year=$(date -d "$date" +%Y) #date_month=$(date -d "$date" +%m) @@ -135,58 +143,101 @@ get_metadata() { | jq ".all |= (sort_by(.date) | reverse) \ | .by_year |= (to_entries | sort_by(.key) | reverse \ | .[].value |= (sort_by(.date) | reverse))") - - categories=$(echo "$categories" | jq ". | to_entries | sort_by(.key)") - - echo "{}" | jq ". + { \ - pages: $pages, \ - categories: $categories \ - }" fi + + echo "{}" | jq ". + { \ + pages: $pages \ + }" } handle () { - target=$(target_filename "$1") + target_file=$(target_filename "$1") - mkdir -p $(dirname "$target") + mkdir -p $(dirname "$target_file") if [ "${1#*.}" = "md" ]; then - echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $target" + echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $target_file" + + meta_file=$(mktemp) + + metadata=$(metadata "$file") + metadata=$(echo "[ \ + $metadata, \ + $(get_subpages_meta "$1") \ + ]" | jq ".[0] * .[1]") + + url_abs=$(target_url_abs "$1" "html") + url_full="${SITE%/}$url_abs" - is_home=false section=$(get_section "$1") - if [ "$target" = "${OUTPUT_DIR}index.html" ]; then + is_home=false + if [ "$target_file" = "${OUTPUT_DIR}index.html" ]; then is_home=true fi - subpages_meta_file=$(mktemp) + metadata=$(echo "$metadata" | jq ". * { \ + url: { \ + abs: \"$url_abs\", \ + full: \"$url_full\" \ + }, \ + section: { \ + id: \"$section\", \ + is_$section: true \ + }, \ + is_home: $is_home \ + }") - # echo "$(get_metadata "$1")" - echo "$(get_metadata "$1")" > "$subpages_meta_file" + create_feed=$(echo "$metadata" | jq -r ".create_feed") + + if [ "$create_feed" = "true" ]; then + feed_target_file="${target_file%.html}.xml" + feed_url_abs="$(target_url_abs "$1" "xml")" + feed_url_full="${SITE%/}$feed_url_abs" + last_update=$(echo "$metadata" | jq -r ".pages.all[0].date") + + metadata=$(echo "$metadata" | jq ". * { \ + feed: { \ + last_update: $last_update, \ + url: { \ + abs: \"$feed_url_abs\", \ + full: \"$feed_url_full\" \ + } \ + } \ + }") - # echo $(pandoc "$1" \ - # -f markdown \ - # -t json) + echo "$metadata" > "$meta_file" + + pandoc "$1" \ + -f markdown \ + -t html5 \ + --template "${TEMPLATES_DIR}feed.xml" \ + -o "$feed_target_file" \ + --metadata-file metadata/metadata.yaml \ + --metadata-file "$meta_file" \ + $FILTERS + fi + + echo "$metadata" > "$meta_file" pandoc "$1" \ -f markdown \ -t html5 \ --template "${TEMPLATES_DIR}base.html" \ - -o "$target" \ - --metadata "ishome"=$is_home \ - --metadata section=$section \ - --metadata "is$section" \ + -o "$target_file" \ --metadata-file metadata/metadata.yaml \ - --metadata-file "$subpages_meta_file" \ + --metadata-file "$meta_file" \ $FILTERS - # --lua-filter "filters/add_refs.lua" - rm "$subpages_meta_file" + rm "$meta_file" + + # echo $(pandoc "$1" \ + # -f markdown \ + # -t json) else - echo -e "\033[0;32m[COPY ]\033[0m $1 -> $target" + echo -e "\033[0;32m[COPY ]\033[0m $1 -> $target_file" - cp "$1" "$target" + cp "$1" "$target_file" fi } @@ -215,9 +266,9 @@ 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 + target_file=$(target_filename "$2") + echo -e "\033[0;32m[DELETE ]\033[0m $2 -> $target_file" + rm -rf $target_file fi else echo -e "\033[0;31m[ERROR ]\033[0m Unknown operation: \"$1\"" diff --git a/scripts/metadata_tpl.json b/scripts/metadata_tpl.json index 76e4439..79eabe1 100755 --- a/scripts/metadata_tpl.json +++ b/scripts/metadata_tpl.json @@ -1,5 +1,6 @@ { "title": "$title$", "date": "$date$", - "category": "$category$" + "category": "$category$", + "create_feed": "$create_feed$" } -- cgit v1.2.3-70-g09d2