From 73760eb54cc570fc5835bbe336a26f6fd356a48c Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 11 May 2021 08:41:47 +0200 Subject: Minor improvements in metadata processing script --- Makefile | 2 +- scripts/metadata_filter.lua | 72 ++++++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index 7f4ff95..2be2e21 100644 --- a/Makefile +++ b/Makefile @@ -117,7 +117,7 @@ watch: all done serve: all - python -m http.server --directory out 8000 + python -m http.server --bind 127.0.0.1 --directory out 8000 dev: watch serve diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 21f7759..b8a9059 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -1,9 +1,9 @@ function format_date(date) - if date == nil then return date end + if not date then return date end date = pandoc.utils.normalize_date(pandoc.utils.stringify(date)) local year, month, day = date:match("(%d%d%d%d)-(%d%d)-(%d%d)") - if year == nil then return nil end + if not year then return nil end local time = os.time({year = tonumber(year), month = tonumber(month), day = tonumber(day)}) return pandoc.MetaMap({ @@ -41,9 +41,9 @@ function group_by(l, field, insert) local item = l[i] local f = field(item) - if f ~= nil then + if f then local out = insert(groups[f], f, item) - if out ~= nil then groups[f] = out end + if out then groups[f] = out end end end @@ -86,7 +86,7 @@ function relative_to(dir, target) end function make_absolute(rel, base) - return rel:find("^/") ~= nil and rel or base:gsub("^(.*)/.-$", "%1") .. "/" .. rel + return rel:find("^/") and rel or base:gsub("^(.*)/.-$", "%1") .. "/" .. rel end function resolve_url(site_url, ref_file, target_file) @@ -100,7 +100,7 @@ function resolve_url(site_url, ref_file, target_file) end function resolve_layout(layout) - if layout ~= nil then + if layout then layout = pandoc.utils.stringify(layout) return pandoc.MetaMap({id = layout, ["is_" .. layout] = pandoc.MetaBool(true)}) end @@ -119,7 +119,7 @@ function resolve_namespace(namespace) end function resolve_category(categories, category) - if categories ~= nil and category ~= nil then + if categories and category then category = pandoc.utils.stringify(category) data = pandoc.MetaMap(categories[category]) data.id = category @@ -144,7 +144,7 @@ function prep_menu(active_id, main_menu) end function process_pages(global, pages_by_id) - if pages_by_id == nil then pages_by_id = {} end + if not pages_by_id then pages_by_id = {} end local pages_list = pandoc.List() @@ -153,13 +153,25 @@ function process_pages(global, pages_by_id) pages_list:insert(page) end + pages_list:sort(function(p1, p2) + if p1.date then + if p2.date then + return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd + else + return true + end + elseif p2.date then + return false + else + return p1.title < p2.title + end + 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) - pages_by_category = pandoc.MetaList(table_to_list(pages_by_category, - function(i1, i2) return i1.key < i2.key 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, @@ -167,16 +179,15 @@ function process_pages(global, pages_by_id) by_category = pages_by_category, }) - 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 = p.category.name, count = 1} - else - stats.count = stats.count + 1 - end - end) - categories_data = pandoc.MetaList(table_to_list(categories_data)) + 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} + else + stats.count = stats.count + 1 + end + end) + categories_data = table_to_list(categories_data) for i = 1, #categories_data do categories_data[i].value.count = ("%d"):format(categories_data[i].value.count) @@ -190,21 +201,16 @@ function process(global, meta) meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") meta.layout = resolve_layout(meta.layout) meta.url = resolve_url(global.site.url, global.file_out, meta.file_out) + meta.title = (meta.title and pandoc.utils.stringify(meta.title)) or "" - if meta.title ~= nil then - meta.title = pandoc.utils.stringify(meta.title) - else - meta.title = "" - end - - if meta.preview ~= nil then + if meta.preview then 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 - if meta.date ~= nil then meta.date = format_date(meta.date) end + if meta.date then meta.date = format_date(meta.date) end - if meta.last_update ~= nil then + if meta.last_update then meta.last_update = format_date(meta.last_update) else meta.last_update = meta.date @@ -212,7 +218,7 @@ function process(global, meta) meta.category = resolve_category(global.categories[meta.namespace.root.id], meta.category) - if meta.menus ~= nil and meta.menus.main ~= nil then + if meta.menus and meta.menus.main then meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) end -- cgit v1.2.3-70-g09d2