From 944a735c39a27fc86569c585f2805837b61adb60 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 2 Aug 2022 20:56:45 +0200 Subject: Slightly tidied up metadata handling --- Makefile | 15 +++++------ filters/lib/common.lua | 26 +----------------- filters/vars.lua | 2 +- scripts/lib/common.lua | 54 ++++++++++++++++++++++++++++++++++++++ scripts/metadata.lua | 71 +++++++++++++++----------------------------------- 5 files changed, 84 insertions(+), 84 deletions(-) create mode 100644 scripts/lib/common.lua diff --git a/Makefile b/Makefile index cfc583f..863fca1 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ static_files: $(STATIC_FILES) .SECONDEXPANSION: -namespace = $(patsubst %/index,%,$(patsubst %.json,%,$(patsubst $(2)%,%,$(1)))) +namespace = $(patsubst %/index,%,$(basename $(patsubst $(2)%,%,$(1)))) subpages = $(patsubst content/%.md,.cache/meta/%.json, \ $(shell test -d $(patsubst .cache/meta%,content%,$(1)) && find $(patsubst .cache/meta%,content%,$(1)) -maxdepth 1 -type f -name "*.md" ! -name "index.md") \ @@ -64,7 +64,6 @@ subpages = $(patsubst content/%.md,.cache/meta/%.json, \ --no-highlight \ --metadata-file "$@.pages" \ --template scripts/metadata.json \ - --metadata mode="$(MODE)" \ --metadata namespace="$(NAMESPACE)" \ --metadata file_out="$(patsubst .cache/meta/%.json,out/%.html,$@)" \ $(PANDOC_FILTERS) \ @@ -78,8 +77,8 @@ subpages = $(patsubst content/%.md,.cache/meta/%.json, \ --lua-filter scripts/subpages.lua \ $(GLOBAL_METADATA) \ --metadata-file "$@.meta" \ - --metadata mode="$(MODE)" \ - --metadata file_out="$@" \ + --metadata build.mode="$(MODE)" \ + --metadata build.file_out="$@" \ $(PANDOC_FILTERS) \ -o "$@.content" "$<" jq '. + { content: $$content }' --rawfile content "$@.content" "$@.meta" > "$@" @@ -102,9 +101,9 @@ out/%.html: content/%.md .cache/meta/%.json .cache/assets.json $(CONTENT_TEMPLAT --lua-filter scripts/metadata.lua \ $(GLOBAL_METADATA) \ --metadata-file .cache/assets.json \ - --metadata mode="$(MODE)" \ --metadata-file "$(filter .cache/meta/%.json,$^)" \ - --metadata file_out="$@" \ + --metadata build.mode="$(MODE)" \ + --metadata build.file_out="$@" \ $(PANDOC_FILTERS) \ -o "$@" "$<" @@ -121,8 +120,8 @@ out/%.xml: content/%.md .cache/meta/%.json $(FEED_TEMPLATES_SRC) metadata/*.yaml --lua-filter scripts/metadata.lua \ $(GLOBAL_METADATA) \ --metadata-file "$(filter .cache/meta/%.json,$^)" \ - --metadata mode="$(MODE)" \ - --metadata file_out="$@" \ + --metadata build.mode="$(MODE)" \ + --metadata build.file_out="$@" \ $(PANDOC_FILTERS) \ -o "$@" "$<" diff --git a/filters/lib/common.lua b/filters/lib/common.lua index 4d44ff3..d6f92e0 100644 --- a/filters/lib/common.lua +++ b/filters/lib/common.lua @@ -12,30 +12,6 @@ function wide(el) return els end -function dump(o) - if type(o) == 'table' then - local s = '{ ' - for k, v in pairs(o) do - if type(k) ~= 'number' then k = '"' .. k .. '"' end - s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' - end - return s .. '} ' - else - return tostring(o) - end -end - -function string.split(str, sep) - sep = sep or '%s' - - local parts = pandoc.List() - - for field, s in str:gmatch("([^" .. sep .. "]*)(" .. sep .. "?)") do - parts:insert(field) - if s == "" then return parts end - end -end - return { - wide = wide, dump = dump + wide = wide } diff --git a/filters/vars.lua b/filters/vars.lua index b0fa66c..99cedc4 100644 --- a/filters/vars.lua +++ b/filters/vars.lua @@ -1,4 +1,4 @@ -local common = require 'filters.lib.common' +local common = require 'scripts.lib.common' local vars = {} diff --git a/scripts/lib/common.lua b/scripts/lib/common.lua new file mode 100644 index 0000000..80b81ba --- /dev/null +++ b/scripts/lib/common.lua @@ -0,0 +1,54 @@ +function dump(o) + if type(o) == 'table' then + local s = '{ ' + for k, v in pairs(o) do + if type(k) ~= 'number' then k = '"' .. k .. '"' end + s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end +end + +function string.split(str, sep) + sep = sep or '%s' + + local parts = pandoc.List() + + for field, s in str:gmatch("([^" .. sep .. "]*)(" .. sep .. "?)") do + parts:insert(field) + if s == "" then return parts end + end +end + +function pandoc.List:flatten() + local result = pandoc.List() + + for i = 1, #self do result:extend(self[i]) end + + return result +end + +function pandoc.List:flatMap(fn) + local mapped = self:map(fn) + local result = pandoc.List() + + for i = 1, #mapped do result:extend(mapped[i]) end + + return result +end + +function pandoc.List:take(n) + if n >= #self then return self end + + local result = pandoc.List() + + for i = 1, n do result:insert(self[i]) end + + return result +end + +return { + dump = dump +} diff --git a/scripts/metadata.lua b/scripts/metadata.lua index a451040..cb6c6e6 100644 --- a/scripts/metadata.lua +++ b/scripts/metadata.lua @@ -1,44 +1,5 @@ local path = require 'pandoc.path' - -function pandoc.List:flatten() - local result = pandoc.List() - - for i = 1, #self do result:extend(self[i]) end - - return result -end - -function pandoc.List:flatMap(fn) - local mapped = self:map(fn) - local result = pandoc.List() - - for i = 1, #mapped do result:extend(mapped[i]) end - - return result -end - -function pandoc.List:take(n) - if n >= #self then return self end - - local result = pandoc.List() - - for i = 1, n do result:insert(self[i]) end - - return result -end - -function dump(o) - if type(o) == 'table' then - local s = '{ ' - for k, v in pairs(o) do - if type(k) ~= 'number' then k = '"' .. k .. '"' end - s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' - end - return s .. '} ' - else - return tostring(o) - end -end +local common = require 'scripts.lib.common' function page_sort(order) return function(p1, p2) @@ -145,13 +106,13 @@ function prep_menu(active_id, main_menu) return { items = items, active = active_item } end -function process_pages(global, order, pages_by_id) +function process_pages(global, build, order, pages_by_id) local pages_all = pandoc.List() local pages_date_desc = pandoc.List() if pages_by_id then for _, page in pairs(pages_by_id) do - local p = process(global, page) + local p = process(global, build, page) if not p.unlisted then pages_all:insert(p) if p.last_update then pages_date_desc:insert(p) end @@ -286,13 +247,13 @@ function generate_list(meta) end end -function process(global, meta) +function process(global, build, meta) meta.namespace = resolve_namespace(meta.namespace) meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") - meta.unlisted = meta.unlisted or (meta.draft and global.mode ~= "dev") + meta.unlisted = meta.unlisted or (meta.draft and build.mode ~= "dev") meta.redirect = meta.url and true meta.url = meta.url and pandoc.utils.stringify(meta.url) - meta.url = resolve_url(global.site.url, global.file_out, meta.url or meta.file_out) + meta.url = resolve_url(global.site.url, build.file_out, meta.url or meta.file_out) meta.title = (meta.title and pandoc.utils.stringify(meta.title)) or "" meta.slug = slug(meta.title) meta.schema_type = (meta.schema_type and pandoc.utils.stringify(meta.schema_type)) or "CreativeWork" @@ -304,11 +265,11 @@ function process(global, meta) if meta.feed then if meta.file_out:match(".html$") then meta.feed = { - url = resolve_url(global.site.url, global.file_out, meta.file_out:gsub(".html$", ".xml")), + url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".html$", ".xml")), } else meta.page = { - url = resolve_url(global.site.url, global.file_out, meta.file_out:gsub(".xml$", ".html")), + url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".xml$", ".html")), } end end @@ -316,14 +277,14 @@ function process(global, meta) if meta.thumbnail then meta.thumbnail = make_absolute("thumbnail." .. pandoc.utils.stringify(meta.thumbnail), meta.file_out) - meta.thumbnail = resolve_url(global.site.url, global.file_out, meta.thumbnail) + meta.thumbnail = resolve_url(global.site.url, build.file_out, meta.thumbnail) end if meta.menus and meta.menus.main then meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) end - meta.pages = process_pages(global, meta.list_order, meta.pages) + meta.pages = process_pages(global, build, meta.list_order, meta.pages) meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) if meta.last_update then @@ -348,8 +309,18 @@ function process(global, meta) end function Meta(meta) + local build = {} + + for key, value in pairs(meta) do + local suffix = key:match('^build.(.*)$') + + if suffix then + build[suffix] = value + end + end + meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") find_depth(meta) - return process(meta, meta) + return process(meta, build, meta) end -- cgit v1.2.3-70-g09d2