From a48d05d1d5fcff414359c8ba6cc8f38467aebdeb Mon Sep 17 00:00:00 2001 From: Volpeon Date: Mon, 10 May 2021 16:41:05 +0200 Subject: Refactoring to fully take advantage of Make --- scripts/build_content.sh | 222 -------------------------------------------- scripts/build_fonts.sh | 19 ---- scripts/build_sass.sh | 6 -- scripts/deploy.sh | 6 -- scripts/metadata_filter.lua | 222 +++++++++++++------------------------------- scripts/subpages.jq | 8 ++ scripts/watch_content.sh | 14 --- scripts/watch_filters.sh | 9 -- scripts/watch_metadata.sh | 9 -- scripts/watch_sass.sh | 9 -- scripts/watch_templates.sh | 9 -- 11 files changed, 75 insertions(+), 458 deletions(-) delete mode 100755 scripts/build_content.sh delete mode 100755 scripts/build_fonts.sh delete mode 100755 scripts/build_sass.sh delete mode 100755 scripts/deploy.sh create mode 100644 scripts/subpages.jq delete mode 100755 scripts/watch_content.sh delete mode 100755 scripts/watch_filters.sh delete mode 100755 scripts/watch_metadata.sh delete mode 100755 scripts/watch_sass.sh delete mode 100755 scripts/watch_templates.sh (limited to 'scripts') 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 @@ -#!/bin/bash - -source "site.defaults.conf" - -get_filters() { - local 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_filename_out() { - local filename=$1 - filename=${filename#"$CONTENT_DIR"} - echo "$OUTPUT_DIR$filename" -} - -get_metadata() { - pandoc "$1" \ - -f markdown-citations \ - -t plain \ - --metadata content_dir="$CONTENT_DIR" \ - --metadata file_in="$1" \ - --metadata relative_to="$2" \ - --metadata-file metadata/metadata.yaml \ - --no-highlight \ - --template scripts/metadata_tpl.json \ - --lua-filter scripts/metadata_filter.lua -} - -get_content() { - pandoc "$1" \ - -f markdown-citations \ - -t markdown \ - --metadata content_dir="$CONTENT_DIR" \ - --metadata file_in="$1" \ - --metadata-file metadata/metadata.yaml \ - --no-highlight \ - --lua-filter scripts/metadata_filter.lua -} - -get_subpages_metadata() { - local base_dir_in=${1%/index.md} - local relative_to="$2" - if [ -z "$2" ]; then - relative_to="$1" - fi - local child_pages=() - - if [ -d "$base_dir_in" ]; then - mapfile -d $'\0' child_pages < <(find "$base_dir_in" \ - -maxdepth 2 \ - -type f \ - -wholename "$base_dir_in/*/index.md" \ - ! -name "_*.md" \ - -print0) - fi - - local pages="[]" - local subsections="{}" - - for file_in in "${child_pages[@]}"; do - local subsection=$(basename "${file_in%/index.md}") - local content=$(get_content "$file_in" | sed -z 's/\\/\\\\/g;s/\n/\\n/g;s/"/\\"/g') - local metadata=$(get_metadata "$file_in" | jq "{ \ - file_out: .file_out, \ - url: .url, \ - author: .author, \ - title: .title, \ - date: .date, \ - last_update: .last_update, \ - category: .category, \ - preview: .preview, \ - content: \"$content\" \ - } | del(.[] | nulls)") - local title=$(echo "$metadata" | jq ".title") - local subpages="[]" - - if [ -z "$2" ]; then - subpages=$(get_subpages_metadata "$file_in" "$1" | jq ".pages") - fi - - pages=$(echo "$pages" | jq ". += [ $metadata ]") - subsections=$(echo "$subsections" | jq ". += { \"$subsection\": { title: $title, pages: $subpages } }") - done - - echo "{ \"pages\": $pages, \"subsections\": $subsections }" -} - -handle () { - if [ "${1#*.}" = "md" ]; then - local included_metadata=$(get_metadata "$1") - local file_out=$(echo "$included_metadata" | jq -r .file_out) - file_out="$OUTPUT_DIR${file_out#/}" - local create_feed=$(echo "$included_metadata" | jq -r .create_feed) - - mkdir -p $(dirname "$file_out") - - echo -e "\033[0;32m[COMPILE ]\033[0m $1 -> $file_out" - - echo -e "\033[0;90m[COMPILE ]\033[0m Getting subpages" - - local added_metadata=$(get_subpages_metadata "$1") - - local 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-citations \ - -t html5 \ - --no-highlight \ - --template "${TEMPLATES_DIR}feed.xml" \ - -o "${file_out%.html}.xml" \ - --metadata content_dir="$CONTENT_DIR" \ - --metadata file_in="$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-citations \ - -t html5 \ - --no-highlight \ - --template "${TEMPLATES_DIR}base.html" \ - -o "$file_out" \ - --metadata content_dir="$CONTENT_DIR" \ - --metadata file_in="$1" \ - --metadata-file metadata/metadata.yaml \ - --metadata-file "$meta_file" \ - --lua-filter scripts/metadata_filter.lua \ - $FILTERS - - # echo "$(pandoc "$1" \ - # -f markdown-citations \ - # -t plain \ - # --no-highlight \ - # --template scripts/metadata_tpl.json \ - # --metadata content_dir="$CONTENT_DIR" \ - # --metadata file_in="$1" \ - # --metadata-file metadata/metadata.yaml \ - # --metadata-file "$meta_file" \ - # --lua-filter scripts/metadata_filter.lua \ - # $FILTERS)" - - rm "$meta_file" - - 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" - fi -} - -mdfilter="" -if [ "$LIVE" != true ]; then - mdfilter="! -name _*.md" -fi - -if [ -z "$1" ]; then - find "$CONTENT_DIR" \ - -type f \ - ! -name ".*" \ - $mdfilter \ - | while read file_in - do - handle "$file_in" - done -elif [ "$1" = "all_md" ]; then - find "$CONTENT_DIR" \ - -type f \ - -name "*.md" \ - ! -name ".*" \ - $mdfilter \ - | while read file_in - do - handle "$file_in" - 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 - 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 - echo -e "\033[0;31m[ERROR ]\033[0m Unknown operation: \"$1\"" -fi diff --git a/scripts/build_fonts.sh b/scripts/build_fonts.sh deleted file mode 100755 index 7d368e7..0000000 --- a/scripts/build_fonts.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -source "site.defaults.conf" - -find "${ASSETS_DIR}fonts" \ - -type f \ - -name "*.ttf" \ - | while read file - do - target_file=$(basename "${file%.ttf}.woff2") - - echo -e "\033[0;32m[MINIFY ]\033[0m $file -> $OUTPUT_DIR$target_file" - - pyftsubset "$file" \ - --text-file="${ASSETS_DIR}fonts/glyphs.txt" \ - --layout-features+=ss02,ss09,dlig \ - --flavor="woff2" \ - --output-file="$OUTPUT_DIR$target_file" - done diff --git a/scripts/build_sass.sh b/scripts/build_sass.sh deleted file mode 100755 index 3f5f768..0000000 --- a/scripts/build_sass.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -source "site.defaults.conf" - -echo -e "\033[0;32m[COMPILE ]\033[0m ${ASSETS_DIR}css/style.scss -> ${OUTPUT_DIR}style.css" -sassc -t compressed ${ASSETS_DIR}css/style.scss | ./node_modules/.bin/postcss --use autoprefixer --no-map > "${OUTPUT_DIR}style.css" diff --git a/scripts/deploy.sh b/scripts/deploy.sh deleted file mode 100755 index a810b74..0000000 --- a/scripts/deploy.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash - -source "site.defaults.conf" - -echo -e "\033[0;32m[DEPLOY ]\033[0m $DEPLOY_TARGET" -rsync --progress --stats -rvz --delete "$OUTPUT_DIR" "$DEPLOY_TARGET" diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 90ca465..21f7759 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -5,27 +5,21 @@ function format_date(date) local year, month, day = date:match("(%d%d%d%d)-(%d%d)-(%d%d)") if year == nil then return nil end - local time = os.time({ - year = tonumber(year), - month = tonumber(month), - day = tonumber(day) - }) + local time = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)}) return pandoc.MetaMap({ - yyyy_mm_dd = pandoc.MetaString(os.date("%F", time)), - yyyy = pandoc.MetaString(os.date("%Y", 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)), - long = pandoc.MetaString(os.date("%B %d, %Y", time)), + yyyy_mm_dd = os.date("%F", time), + yyyy = os.date("%Y", time), + mm = os.date("%m", time), + dd = os.date("%d", time), + rfc3339 = os.date("%FT%T+00:00", time), + long = os.date("%B %d, %Y", time), }) end function table_to_list(t, cmp) local l = pandoc.List() - for key, value in pairs(t) do - l:insert(pandoc.MetaMap({key = key, value = value})) - end + for key, value in pairs(t) do l:insert(pandoc.MetaMap({key = key, value = value})) end l:sort(cmp or function(i1, i2) return i1.key < i2.key end) @@ -83,9 +77,7 @@ function relative_to(dir, target) end if #dir < #target then - for i = #dir + 1, #target do - path = path .. (path == "" and "" or "/") .. target[i] - end + for i = #dir + 1, #target do path = path .. (path == "" and "" or "/") .. target[i] end elseif #dir > #target then for _ = #target + 1, #dir do path = "../" .. path end end @@ -93,31 +85,8 @@ function relative_to(dir, target) return path end -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, 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(file_out) -end - function make_absolute(rel, base) - return - rel:find("^/") ~= nil and rel or base:gsub("^(.*)/.-$", "%1") .. "/" .. - rel + return rel:find("^/") ~= nil and rel or base:gsub("^(.*)/.-$", "%1") .. "/" .. rel end function resolve_url(site_url, ref_file, target_file) @@ -127,119 +96,82 @@ function resolve_url(site_url, ref_file, target_file) local abs = target_file local rel = relative_to(ref_base_dir, abs):gsub("/index%.html$", "/") - return pandoc.MetaMap({ - abs = pandoc.MetaString(abs), - rel = pandoc.MetaString(rel), - full = pandoc.MetaString(site_url .. abs) - }) + return pandoc.MetaMap({abs = abs, rel = rel, full = (site_url .. abs)}) end function resolve_layout(layout) - if layout then + if layout ~= nil then layout = pandoc.utils.stringify(layout) - return pandoc.MetaMap({ - id = pandoc.MetaString(layout), - ["is_" .. layout] = pandoc.MetaBool(true) - }) + return pandoc.MetaMap({id = layout, ["is_" .. layout] = pandoc.MetaBool(true)}) end end -function resolve_section(content_dir, file_in) - local section = file_in:gsub("^" .. content_dir, ""):match("^/(.-)[/.]") or - "index" +function resolve_namespace(namespace) + namespace = pandoc.utils.stringify(namespace) + + local root = "index" + if namespace ~= "" then root = namespace:gsub("^/([^/]*).*$", "%1") end + return pandoc.MetaMap({ - id = pandoc.MetaString(section), - ["is_" .. section] = pandoc.MetaBool(true) + root = pandoc.MetaMap({id = root, ["is_" .. root] = pandoc.MetaBool(true)}), + full = namespace, }) end function resolve_category(categories, category) - if categories and category then + if categories ~= nil and category ~= nil then category = pandoc.utils.stringify(category) data = pandoc.MetaMap(categories[category]) - data.id = pandoc.MetaString(category) + data.id = category return data end end -function prep_main_menu(rewrites, section, main_menu) +function prep_menu(active_id, 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 + local active = pandoc.utils.stringify(item.id) == active_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 return pandoc.MetaMap({ - items = main_menu:filter(function(item) - return not item.hidden or item.active - end), - active = active_item + items = main_menu:filter(function(item) return not item.hidden or item.active end), + active = active_item, }) end -function organize_subpages(site_url, ref_file, pages) - for i = 1, #pages do - local page = pages[i] - page.url = resolve_url(site_url, ref_file, - pandoc.utils.stringify(page.file_out)) - end - - local pages_grouped_date = - group_by(pages, function(p) return not p.date end) - - local pages_undated = pages_grouped_date[true] or pandoc.MetaList({}) - pages_undated:sort(function(p1, p2) - return pandoc.utils.stringify(p1.title) < - pandoc.utils.stringify(p2.title) - end) +function process_pages(global, pages_by_id) + if pages_by_id == nil then pages_by_id = {} end - local pages_dated = pages_grouped_date[false] or pandoc.MetaList({}) - pages_dated:sort(function(p1, p2) - return pandoc.utils.stringify(p1.date.yyyy_mm_dd) > - pandoc.utils.stringify(p2.date.yyyy_mm_dd) - end) + local pages_list = pandoc.List() - local pages_categorized = pages:filter( - function(p) return p.category ~= nil end) - pages_categorized:sort(function(p1, p2) - return pandoc.utils.stringify(p1.title) < - pandoc.utils.stringify(p2.title) - end) + for _, page in pairs(pages_by_id) do + page = process(global, page) + pages_list:insert(page) + end - local pages_by_year = group_by(pages_dated, function(p) - return pandoc.utils.stringify(p.date.yyyy) - end) - pages_by_year = pandoc.MetaList(table_to_list(pages_by_year, - function(i1, i2) - return i1.key > i2.key - end)) + local pages_categorized = pages_list:filter(function(p) return p.category ~= nil end) + pages_categorized:sort(function(p1, p2) return p1.title < p2.title end) - local pages_by_category = group_by(pages_categorized, function(p) - return pandoc.utils.stringify(p.category.id) - end) + local pages_by_category = group_by(pages_categorized, + function(p) return pandoc.utils.stringify(p.category.id) end) pages_by_category = pandoc.MetaList(table_to_list(pages_by_category, - function(i1, i2) - return i1.key < i2.key - end)) + function(i1, i2) return i1.key < i2.key end)) local pages_data = pandoc.MetaMap({ - all_dated = pages_dated, - all_undated = pages_undated, - by_year = pages_by_year, + all = pages_list, + by_id = pages_by_id, by_category = pages_by_category, - last_update = #pages_dated ~= 0 and pages_dated[1].last_update }) local categories_data = group_by(pages_categorized, function(p) return p.category and pandoc.utils.stringify(p.category.id) end, function(stats, _, p) if not stats then - return {name = pandoc.MetaString(p.category.name), count = 1} + return {name = p.category.name, count = 1} else stats.count = stats.count + 1 end @@ -247,72 +179,52 @@ function organize_subpages(site_url, ref_file, pages) categories_data = pandoc.MetaList(table_to_list(categories_data)) for i = 1, #categories_data do - categories_data[i].value.count = - pandoc.MetaString(("%d"):format(categories_data[i].value.count)) + categories_data[i].value.count = ("%d"):format(categories_data[i].value.count) end return pages_data, categories_data end -function Meta(meta) - meta.content_dir = meta.content_dir:gsub("/$", "") - meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") - meta.rewrites = meta.rewrites or - pandoc.MetaMap({path = pandoc.MetaList({})}) - meta.page_type = meta.page_type or "page" +function process(global, meta) + meta.namespace = resolve_namespace(meta.namespace) + meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") meta.layout = resolve_layout(meta.layout) - meta.section = resolve_section(meta.content_dir, meta.file_in) + meta.url = resolve_url(global.site.url, global.file_out, meta.file_out) - meta.file_out = get_file_out(meta.rewrites, meta.content_dir, meta.file_in) - if meta.relative_to == nil then meta.relative_to = meta.file_in end - meta.relative_to_out = get_file_out(meta.rewrites, meta.content_dir, - meta.relative_to) + if meta.title ~= nil then + meta.title = pandoc.utils.stringify(meta.title) + else + meta.title = "" + end - meta.url = resolve_url(meta.site.url, meta.relative_to_out, meta.file_out) if meta.preview ~= nil then - meta.preview = resolve_url(meta.site.url, meta.relative_to_out, - make_absolute( - pandoc.utils.stringify(meta.preview), - meta.file_out)) + meta.preview = make_absolute(pandoc.utils.stringify(meta.preview), meta.file_out) + meta.preview = resolve_url(global.site.url, global.file_out, meta.preview) end - meta.date = format_date(meta.date) + if meta.date ~= nil then meta.date = format_date(meta.date) end + if meta.last_update ~= nil then meta.last_update = format_date(meta.last_update) else meta.last_update = meta.date end - meta.category = resolve_category(meta.categories[meta.section.id], - meta.category) - meta.categories = nil + meta.category = resolve_category(global.categories[meta.namespace.root.id], meta.category) - if meta.page_type == "feed" then - meta.page = pandoc.MetaMap({ - url = resolve_url(meta.site.url, meta.relative_to_out, - meta.file_out:gsub("%.xml$", ".html")) - }) + if meta.menus ~= nil and meta.menus.main ~= nil then + meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) end - if meta.create_feed then - meta.feed = pandoc.MetaMap({ - url = resolve_url(meta.site.url, meta.relative_to_out, - meta.file_out:gsub("%.html$", ".xml")) - }) - end + local pages, local_categories = process_pages(global, meta.pages) + meta.pages = pages + meta.local_categories = local_categories - if meta.menus and meta.menus.main then - meta.menus.main = prep_main_menu(meta.rewrites, meta.section, - meta.menus.main) - end + return meta +end - if meta.pages then - local pages, categories = organize_subpages(meta.site.url, - meta.relative_to_out, - meta.pages) - meta.pages = pages - meta.categories = categories - end +function Meta(meta) + meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") - return meta + return process(meta, meta) end diff --git a/scripts/subpages.jq b/scripts/subpages.jq new file mode 100644 index 0000000..4850703 --- /dev/null +++ b/scripts/subpages.jq @@ -0,0 +1,8 @@ +[ + .[] + | . as $page + | (.namespace | ltrimstr($namespace + "/") | split("/") | (.[] |= ["pages",.]) | flatten) as $path + | null + | setpath($path; $page) +] + | reduce .[] as $item ({}; . * $item) diff --git a/scripts/watch_content.sh b/scripts/watch_content.sh deleted file mode 100755 index 30f8a6c..0000000 --- a/scripts/watch_content.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -source "site.defaults.conf" - -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 - elif [ ! -d "$file" ]; then - scripts/build_content.sh "delete" "$file" - fi - done diff --git a/scripts/watch_filters.sh b/scripts/watch_filters.sh deleted file mode 100755 index 5dd6ad5..0000000 --- a/scripts/watch_filters.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -source "site.defaults.conf" - -inotifywait -qrme close_write,delete,move --format "%w%f" "${FILTERS_DIR%/}" \ - | while read file - do - scripts/build_content.sh "all_md" - done diff --git a/scripts/watch_metadata.sh b/scripts/watch_metadata.sh deleted file mode 100755 index 2f6efde..0000000 --- a/scripts/watch_metadata.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -source "site.defaults.conf" - -inotifywait -qrme close_write,delete,move --format "%w%f" "${METADATA_DIR%/}" \ - | while read file - do - scripts/build_content.sh "all_md" - done diff --git a/scripts/watch_sass.sh b/scripts/watch_sass.sh deleted file mode 100755 index 7345eb3..0000000 --- a/scripts/watch_sass.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -source "site.defaults.conf" - -inotifywait -qrme close_write,delete,move --format "%w%f" "${ASSETS_DIR}css" \ - | while read file - do - scripts/build_sass.sh - done diff --git a/scripts/watch_templates.sh b/scripts/watch_templates.sh deleted file mode 100755 index 70ae4c3..0000000 --- a/scripts/watch_templates.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -source "site.defaults.conf" - -inotifywait -qrme close_write,delete,move --format "%w%f" "${TEMPLATES_DIR%/}" \ - | while read file - do - scripts/build_content.sh "all_md" - done -- cgit v1.2.3-70-g09d2