From 3ad6bd151364a3278618e3a26e21918d17680d24 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sun, 10 Jan 2021 13:24:21 +0100 Subject: Moved output filename handling into Pandoc filter, implemented path rewriting --- scripts/build_content.sh | 73 +++++++++++++++-------------------------- scripts/metadata_filter.lua | 79 ++++++++++++++++++++++++++++++++------------- scripts/watch_content.sh | 3 +- 3 files changed, 85 insertions(+), 70 deletions(-) (limited to 'scripts') diff --git a/scripts/build_content.sh b/scripts/build_content.sh index 63bc525..97f57a3 100755 --- a/scripts/build_content.sh +++ b/scripts/build_content.sh @@ -26,29 +26,16 @@ FILTERS=$(get_filters) 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() { - 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 output_dir="$OUTPUT_DIR" \ - --metadata base_file_out="$base_file_out" \ - --metadata file_out="$file_out" \ + --metadata file_in="$1" \ --metadata-file metadata/metadata.yaml \ --no-highlight \ --template scripts/metadata_tpl.json \ @@ -56,19 +43,12 @@ get_metadata() { } get_content() { - 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 output_dir="$OUTPUT_DIR" \ - --metadata base_file_out="$base_file_out" \ - --metadata file_out="$file_out" \ + --metadata file_in="$1" \ --metadata-file metadata/metadata.yaml \ --no-highlight \ --lua-filter scripts/metadata_filter.lua @@ -84,9 +64,7 @@ get_subpages_basedir() { } get_subpages_metadata() { - local base_file_in=$1 - local base_file_out=$2 - local base_dir_in=$(get_subpages_basedir "$base_file_in") + local base_dir_in=$(get_subpages_basedir "$1") local child_pages=() if [ -d "$base_dir_in" ]; then @@ -103,10 +81,10 @@ get_subpages_metadata() { 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 content=$(get_content "$file_in" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\"/g') - local metadata=$(get_metadata "$file_in" "$base_file_out" | jq "{ \ - url: .url, \ + local metadata=$(get_metadata "$file_in" | jq "{ \ + file_out: .file_out, \ author: .author, \ title: .title, \ date: .date, \ @@ -122,19 +100,18 @@ get_subpages_metadata() { } handle () { - local file_out=$(get_filename_out "$1") + if [ "${1#*.}" = "md" ]; then + local included_metadata=$(get_metadata "$1") + local file_out=$(echo "$included_metadata" | jq -r .file_out) + local create_feed=$(echo "$included_metadata" | jq -r .create_feed) - mkdir -p $(dirname "$file_out") + mkdir -p $(dirname "$file_out") - if [ "${1#*.}" = "md" ]; then echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out" - echo -e "\033[0;90m[COMPILE ]\033[0m Getting metadata" - - local included_metadata=$(get_metadata "$1") - local added_metadata=$(get_subpages_metadata "$1" "$file_out") + echo -e "\033[0;90m[COMPILE ]\033[0m Getting subpages" - local create_feed=$(echo "$included_metadata" | jq -r .create_feed) + local added_metadata=$(get_subpages_metadata "$1") local meta_file=$(mktemp) @@ -143,16 +120,15 @@ handle () { 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 "$feed_file_out" \ + -o "${file_out%.html}.xml" \ + --metadata content_dir="$CONTENT_DIR" \ --metadata output_dir="$OUTPUT_DIR" \ - --metadata file_out="$feed_file_out" \ + --metadata file_in="$1" \ --metadata page_type=feed \ --metadata-file metadata/metadata.yaml \ --metadata-file "$meta_file" \ @@ -167,8 +143,9 @@ handle () { --no-highlight \ --template "${TEMPLATES_DIR}base.html" \ -o "$file_out" \ + --metadata content_dir="$CONTENT_DIR" \ --metadata output_dir="$OUTPUT_DIR" \ - --metadata file_out="$file_out" \ + --metadata file_in="$1" \ --metadata-file metadata/metadata.yaml \ --metadata-file "$meta_file" \ --lua-filter scripts/metadata_filter.lua \ @@ -179,8 +156,9 @@ handle () { # -t plain \ # --no-highlight \ # --template scripts/metadata_tpl.json \ + # --metadata content_dir="$CONTENT_DIR" \ # --metadata output_dir="$OUTPUT_DIR" \ - # --metadata file_out="$file_out" \ + # --metadata file_in="$1" \ # --metadata-file metadata/metadata.yaml \ # --metadata-file "$meta_file" \ # --lua-filter scripts/metadata_filter.lua \ @@ -188,11 +166,12 @@ handle () { rm "$meta_file" - # echo "$included_metadata" - # echo "$added_metadata" - 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" diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 61fe90f..5ed5382 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -92,11 +92,33 @@ function relative_to(dir, target) return path end -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$", - "/") +function apply_path_rewrites(rewrites, str) + for i = 1, #rewrites.path do + local r = rewrites.path[i] + str = str:gsub(pandoc.utils.stringify(r.from), + pandoc.utils.stringify(r.to)) + end + return str +end + +function get_file_out(rewrites, content_dir, output_dir, file_in) + local file_out = file_in:gsub("^" .. content_dir, ""):gsub("%.md$", ".html") + + if file_out:match(".html$") and not file_out:match("/index%.html$") then + file_out = file_out:gsub("/(.*)%.html$", "/%1/index.html") + end + + file_out = apply_path_rewrites(rewrites, file_out) + + return pandoc.MetaString(output_dir .. file_out) +end + +function resolve_url(site_url, output_dir, ref_file, target_file) + local ref_base_dir = ref_file:gsub("^(.*)/.-$", "%1") + local abs = target_file:gsub("^" .. output_dir, ""):gsub("/index%.html$", + "/") local rel = - relative_to(base_dir_out, cur_file_out):gsub("/index.html$", "/") + relative_to(ref_base_dir, target_file):gsub("/index%.html$", "/") return pandoc.MetaMap({ abs = pandoc.MetaString(abs), @@ -115,8 +137,9 @@ function resolve_layout(layout) end end -function resolve_section(abs_url) - local section = abs_url:match("^/(.-)[/.]") or "index" +function resolve_section(content_dir, file_in) + local section = file_in:gsub("^" .. content_dir, ""):match("^/(.-)[/.]") or + "index" return pandoc.MetaMap({ id = pandoc.MetaString(section), ["is_" .. section] = pandoc.MetaBool(true) @@ -133,13 +156,15 @@ function resolve_category(categories, category) end end -function create_main_menu_state(section, main_menu) +function prep_main_menu(rewrites, section, main_menu) local active_item = nil for i = 1, #main_menu do local item = main_menu[i] local active = pandoc.utils.stringify(item.id) == section.id item.active = pandoc.MetaBool(active) + item.url = apply_path_rewrites(rewrites, + pandoc.utils.stringify(item.url)) if active then active_item = item end end @@ -151,7 +176,13 @@ function create_main_menu_state(section, main_menu) }) end -function organize_subpages(pages) +function organize_subpages(site_url, output_dir, ref_file, pages) + for i = 1, #pages do + local page = pages[i] + page.url = resolve_url(site_url, output_dir, ref_file, + pandoc.utils.stringify(page.file_out)) + end + local grouped_pages = group_by(pages, function(p) return not p.date end) local pages_undated = grouped_pages[true] or pandoc.MetaList({}) @@ -201,13 +232,17 @@ 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_out = meta.base_file_out or meta.file_out - meta.base_dir_out = meta.base_file_out:gsub("^(.*)/.-$", "%1") + meta.rewrites = meta.rewrites or + pandoc.MetaMap({path = pandoc.MetaList({})}) meta.page_type = meta.page_type or "page" - meta.layout = resolve_layout(meta.layout) + meta.section = resolve_section(meta.content_dir, meta.file_in) + + meta.file_out = get_file_out(meta.rewrites, meta.content_dir, + meta.output_dir, meta.file_in) meta.date = format_date(meta.date) if meta.last_update ~= nil then @@ -216,26 +251,26 @@ function Meta(meta) meta.last_update = meta.date end + meta.category = resolve_category(meta.categories[meta.section.id], + meta.category) + meta.categories = nil + if meta.page_type == "feed" then meta.page = pandoc.MetaMap({ - url = resolve_url("page", meta.site.url, meta.output_dir, - meta.base_dir_out, meta.file_out) + url = resolve_url(meta.site.url, meta.output_dir, meta.file_out, + meta.file_out:gsub("%.xml$", ".html")) }) end - 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) - meta.categories = nil - if meta.menus and meta.menus.main then - meta.menus.main = create_main_menu_state(meta.section, meta.menus.main) + meta.menus.main = prep_main_menu(meta.rewrites, meta.section, + meta.menus.main) end if meta.pages then - local pages, categories = organize_subpages(meta.pages) + local pages, categories = organize_subpages(meta.site.url, + meta.output_dir, + meta.file_out, meta.pages) meta.pages = pages meta.categories = categories end diff --git a/scripts/watch_content.sh b/scripts/watch_content.sh index 92719d3..66b726f 100755 --- a/scripts/watch_content.sh +++ b/scripts/watch_content.sh @@ -10,7 +10,8 @@ inotifywait -qrme close_write,delete,move --format "%w%f" "${CONTENT_DIR%/}" \ | while read file do if [ -f "$file" ]; then - scripts/build_content.sh "single" "$file" + # scripts/build_content.sh "single" "$file" + scripts/build_content.sh "all_md" elif [ ! -d "$file" ]; then scripts/build_content.sh "delete" "$file" fi -- cgit v1.2.3-70-g09d2