From 6c20e2fd01856072cae3df727a4ef88314bffe28 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 11 May 2021 20:43:53 +0200 Subject: Simplified metadata processing, improved design --- scripts/metadata_filter.lua | 67 +++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 39 deletions(-) (limited to 'scripts/metadata_filter.lua') diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index b8a9059..5f2d9af 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -6,20 +6,24 @@ function format_date(date) if not year then return nil end local time = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)}) - return pandoc.MetaMap({ + return { 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) +function table_to_list(t, kv, cmp) local l = pandoc.List() - for key, value in pairs(t) do l:insert(pandoc.MetaMap({key = key, value = value})) end + if kv then + for key, value in pairs(t) do l:insert({key = key, value = value}) end + else + for _, value in pairs(t) do l:insert(value) end + end l:sort(cmp or function(i1, i2) return i1.key < i2.key end) @@ -96,13 +100,13 @@ 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 = abs, rel = rel, full = (site_url .. abs)}) + return {abs = abs, rel = rel, full = (site_url .. abs)} end function resolve_layout(layout) if layout then layout = pandoc.utils.stringify(layout) - return pandoc.MetaMap({id = layout, ["is_" .. layout] = pandoc.MetaBool(true)}) + return {id = layout, ["is_" .. layout] = true} end end @@ -112,16 +116,13 @@ function resolve_namespace(namespace) local root = "index" if namespace ~= "" then root = namespace:gsub("^/([^/]*).*$", "%1") end - return pandoc.MetaMap({ - root = pandoc.MetaMap({id = root, ["is_" .. root] = pandoc.MetaBool(true)}), - full = namespace, - }) + return {root = {id = root, ["is_" .. root] = true}, full = namespace} end function resolve_category(categories, category) if categories and category then category = pandoc.utils.stringify(category) - data = pandoc.MetaMap(categories[category]) + data = categories[category] data.id = category return data end @@ -133,14 +134,14 @@ function prep_menu(active_id, main_menu) for i = 1, #main_menu do local item = main_menu[i] local active = pandoc.utils.stringify(item.id) == active_id - item.active = pandoc.MetaBool(active) + item.active = active if active then active_item = item end end - return pandoc.MetaMap({ + return { items = main_menu:filter(function(item) return not item.hidden or item.active end), active = active_item, - }) + } end function process_pages(global, pages_by_id) @@ -167,33 +168,23 @@ function process_pages(global, pages_by_id) end end) - local pages_categorized = pages_list:filter(function(p) return p.category ~= nil end) - - local pages_by_category = group_by(pages_categorized, function(p) return p.category.id end) - pages_by_category = - table_to_list(pages_by_category, function(i1, i2) return i1.key < i2.key end) - - local pages_data = pandoc.MetaMap({ - all = pages_list, - by_id = pages_by_id, - by_category = pages_by_category, - }) - - local categories_data = group_by(pages_categorized, - function(p) return p.category and p.category.id end, function(stats, _, p) - if not stats then - return {name = p.category.name, count = 1} + local pages_by_category = pages_list:filter(function(p) return p.category ~= nil end) + pages_by_category = group_by(pages_by_category, function(p) return p.category.id end, + function(data, _, p) + if not data then + local l = pandoc.List() + l:insert(p) + return {name = pandoc.utils.stringify(p.category.name), pages = l} else - stats.count = stats.count + 1 + data.pages:insert(p) end end) - categories_data = table_to_list(categories_data) + pages_by_category = table_to_list(pages_by_category, false, + function(i1, i2) return i1.name < i2.name end) - for i = 1, #categories_data do - categories_data[i].value.count = ("%d"):format(categories_data[i].value.count) - end + local pages_data = {all = pages_list, by_id = pages_by_id, by_category = pages_by_category} - return pages_data, categories_data + return pages_data end function process(global, meta) @@ -222,9 +213,7 @@ function process(global, meta) meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) end - local pages, local_categories = process_pages(global, meta.pages) - meta.pages = pages - meta.local_categories = local_categories + meta.pages = process_pages(global, meta.pages) return meta end -- cgit v1.2.3-54-g00ecf