From 3085689202ddfdae2b1d4a1ff5fd1566a130cf55 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sat, 9 Jan 2021 21:32:20 +0100 Subject: Improved build script variable names, use output instead of input filenames to generate URLs --- scripts/build_content.sh | 143 +++++++++++++++++++++++++------------------- scripts/metadata_filter.lua | 35 +++++------ 2 files changed, 97 insertions(+), 81 deletions(-) (limited to 'scripts') diff --git a/scripts/build_content.sh b/scripts/build_content.sh index 0b2ce2e..63bc525 100755 --- a/scripts/build_content.sh +++ b/scripts/build_content.sh @@ -7,7 +7,7 @@ if [ -f "site.conf" ]; then fi get_filters() { - filters_args="" + local filters_args="" mapfile -d $'\0' filters < <(find $FILTERS_DIR \ -type f \ @@ -18,27 +18,37 @@ get_filters() { filters_args="$filters_args --lua-filter $file" done - echo $filters_args + echo "$filters_args" } FILTERS=$(get_filters) -get_target_filename() { - filename=$1 +get_filename_out() { + local filename=$1 filename=${filename#"$CONTENT_DIR"} if [ "${filename##*.}" = "md" ]; then filename="${filename%.md}.html" fi + if [ "${filename##*/}" != "index.html" ]; then + filename="${filename%.html}/index.html" + fi echo "$OUTPUT_DIR$filename" } get_metadata() { - pandoc "$2" \ + local file_out=$(get_filename_out "$1") + local base_file_out="$2" + + if [ -z "$2" ]; then + base_file_out="$file_out" + fi + + pandoc "$1" \ -f markdown \ -t plain \ - --metadata content_dir="$CONTENT_DIR" \ - --metadata base_file="$1" \ - --metadata file="$2" \ + --metadata output_dir="$OUTPUT_DIR" \ + --metadata base_file_out="$base_file_out" \ + --metadata file_out="$file_out" \ --metadata-file metadata/metadata.yaml \ --no-highlight \ --template scripts/metadata_tpl.json \ @@ -46,19 +56,26 @@ get_metadata() { } get_content() { - pandoc "$2" \ + local file_out=$(get_filename_out "$1") + local base_file_out="$2" + + if [ -z "$2" ]; then + base_file_out="$file_out" + fi + + pandoc "$1" \ -f markdown \ -t markdown \ - --metadata content_dir="$CONTENT_DIR" \ - --metadata base_file="$1" \ - --metadata file="$2" \ + --metadata output_dir="$OUTPUT_DIR" \ + --metadata base_file_out="$base_file_out" \ + --metadata file_out="$file_out" \ --metadata-file metadata/metadata.yaml \ --no-highlight \ --lua-filter scripts/metadata_filter.lua } get_subpages_basedir() { - filename=$1 + local filename=$1 filename=${filename%.md} if [ "${filename##*/}" = "index" ]; then filename=${filename%index} @@ -67,71 +84,75 @@ get_subpages_basedir() { } get_subpages_metadata() { - basedir=$(get_subpages_basedir "$1") - child_pages=() + local base_file_in=$1 + local base_file_out=$2 + local base_dir_in=$(get_subpages_basedir "$base_file_in") + local child_pages=() - if [ -d "$basedir" ]; then - mapfile -d $'\0' child_pages < <(find "$basedir" \ + if [ -d "$base_dir_in" ]; then + mapfile -d $'\0' child_pages < <(find "$base_dir_in" \ -maxdepth 2 \ -type f \ -name "*.md" \ ! -name "_*.md" \ - ! -wholename "${basedir}index.md" \ - \( -wholename "$basedir*/index.md" -o ! -wholename "$basedir*/*.md" \) \ + ! -wholename "${base_dir_in}index.md" \ + \( -wholename "$base_dir_in*/index.md" -o ! -wholename "$base_dir_in*/*.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 + local pages="[]" + + for file_in in "${child_pages[@]}"; do + local content=$(get_content "$file_in" "$base_file_out" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g') + + local metadata=$(get_metadata "$file_in" "$base_file_out" | jq "{ \ + url: .url, \ + author: .author, \ + title: .title, \ + date: .date, \ + last_update: .last_update, \ + category: .category, \ + content: \"$content\" \ + } | del(.[] | nulls)") + + pages=$(echo "$pages" | jq ". += [ $metadata ]") + done echo "{ \"pages\": $pages }" } handle () { - target_file=$(get_target_filename "$1") + local file_out=$(get_filename_out "$1") - mkdir -p $(dirname "$target_file") + mkdir -p $(dirname "$file_out") if [ "${1#*.}" = "md" ]; then - echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $target_file" + echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out" echo -e "\033[0;90m[COMPILE ]\033[0m Getting metadata" - included_metadata=$(get_metadata "$1" "$1") - added_metadata=$(get_subpages_metadata "$1") + local included_metadata=$(get_metadata "$1") + local added_metadata=$(get_subpages_metadata "$1" "$file_out") - create_feed=$(echo "$included_metadata" | jq -r .create_feed) + local create_feed=$(echo "$included_metadata" | jq -r .create_feed) - meta_file=$(mktemp) + local meta_file=$(mktemp) echo "$added_metadata" > "$meta_file" if [ "$create_feed" = "true" ]; then echo -e "\033[0;90m[COMPILE ]\033[0m Creating feed" + local feed_file_out="${file_out%.html}.xml" + 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" \ + -o "$feed_file_out" \ + --metadata output_dir="$OUTPUT_DIR" \ + --metadata file_out="$feed_file_out" \ --metadata page_type=feed \ --metadata-file metadata/metadata.yaml \ --metadata-file "$meta_file" \ @@ -145,9 +166,9 @@ handle () { -t html5 \ --no-highlight \ --template "${TEMPLATES_DIR}base.html" \ - -o "$target_file" \ - --metadata content_dir="$CONTENT_DIR" \ - --metadata file="$1" \ + -o "$file_out" \ + --metadata output_dir="$OUTPUT_DIR" \ + --metadata file_out="$file_out" \ --metadata-file metadata/metadata.yaml \ --metadata-file "$meta_file" \ --lua-filter scripts/metadata_filter.lua \ @@ -158,8 +179,8 @@ handle () { # -t plain \ # --no-highlight \ # --template scripts/metadata_tpl.json \ - # --metadata content_dir="$CONTENT_DIR" \ - # --metadata file="$1" \ + # --metadata output_dir="$OUTPUT_DIR" \ + # --metadata file_out="$file_out" \ # --metadata-file metadata/metadata.yaml \ # --metadata-file "$meta_file" \ # --lua-filter scripts/metadata_filter.lua \ @@ -172,9 +193,9 @@ handle () { echo -e "\033[0;90m[COMPILE ]\033[0m Done" else - echo -e "\033[0;32m[COPY ]\033[0m $1 -> $target_file" + echo -e "\033[0;32m[COPY ]\033[0m $1 -> $file_out" - cp "$1" "$target_file" + cp "$1" "$file_out" fi } @@ -182,18 +203,18 @@ if [ -z "$1" ]; then find "$CONTENT_DIR" \ -type f \ ! -name "_*.md" \ - | while read file + | while read file_in do - handle "$file" + handle "$file_in" done elif [ "$1" = "all_md" ]; then find "$CONTENT_DIR" \ -type f \ -name "*.md" \ ! -name "_*.md" \ - | while read file + | while read file_in do - handle "$file" + handle "$file_in" done elif [ "$1" = "single" ]; then if [ -z "$2" ]; then @@ -208,10 +229,10 @@ 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 + 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 diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index fb7b3c9..61fe90f 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -13,7 +13,8 @@ function format_date(date) return pandoc.MetaMap({ yyyy_mm_dd = pandoc.MetaString(os.date("%F", time)), yyyy = pandoc.MetaString(os.date("%Y", time)), - mm_dd = pandoc.MetaString(os.date("%m-%d", time)), + mm = pandoc.MetaString(os.date("%m", time)), + dd = pandoc.MetaString(os.date("%d", time)), rfc3339 = pandoc.MetaString(os.date("%FT%T+00:00", time)) }) end @@ -91,15 +92,11 @@ function relative_to(dir, target) return path end -function resolve_url(page_type, site_url, content_dir, base_dir, cur_file) - if page_type == "page" then - cur_file = cur_file:gsub("%.md$", ".html") - elseif page_type == "feed" then - cur_file = cur_file:gsub("%.md$", ".xml") - end - - local abs = cur_file:gsub("^" .. content_dir, ""):gsub("/index.html$", "/") - local rel = relative_to(base_dir, cur_file):gsub("/index.html$", "/") +function resolve_url(page_type, site_url, output_dir, base_dir_out, cur_file_out) + local abs = cur_file_out:gsub("^" .. output_dir, ""):gsub("/index.html$", + "/") + local rel = + relative_to(base_dir_out, cur_file_out):gsub("/index.html$", "/") return pandoc.MetaMap({ abs = pandoc.MetaString(abs), @@ -143,9 +140,7 @@ function create_main_menu_state(section, main_menu) local item = main_menu[i] local active = pandoc.utils.stringify(item.id) == section.id item.active = pandoc.MetaBool(active) - if active then - active_item = item - end + if active then active_item = item end end return pandoc.MetaMap({ @@ -206,10 +201,10 @@ function organize_subpages(pages) end function Meta(meta) - meta.content_dir = meta.content_dir:gsub("/$", "") + meta.output_dir = meta.output_dir:gsub("/$", "") meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") - meta.base_file = meta.base_file or meta.file - meta.base_dir = meta.base_file:gsub("^(.*)/.-$", "%1") + meta.base_file_out = meta.base_file_out or meta.file_out + meta.base_dir_out = meta.base_file_out:gsub("^(.*)/.-$", "%1") meta.page_type = meta.page_type or "page" meta.layout = resolve_layout(meta.layout) @@ -223,13 +218,13 @@ function Meta(meta) if meta.page_type == "feed" then meta.page = pandoc.MetaMap({ - url = resolve_url("page", meta.site.url, meta.content_dir, - meta.base_dir, meta.file) + url = resolve_url("page", meta.site.url, meta.output_dir, + meta.base_dir_out, meta.file_out) }) end - meta.url = resolve_url(meta.page_type, meta.site.url, meta.content_dir, - meta.base_dir, meta.file) + meta.url = resolve_url(meta.page_type, meta.site.url, meta.output_dir, + meta.base_dir_out, meta.file_out) meta.section = resolve_section(meta.url.abs) meta.category = resolve_category(meta.categories[meta.section.id], meta.category) -- cgit v1.2.3-70-g09d2