From e3240a57eef6bf0afa193ae83ecd8c5cf627c72f Mon Sep 17 00:00:00 2001 From: Volpeon Date: Wed, 26 May 2021 08:54:19 +0200 Subject: Category list improvements, generate ATOM feeds --- scripts/metadata_filter.lua | 76 +++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 20 deletions(-) (limited to 'scripts') diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 000fda6..ebbd6d4 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -5,7 +5,7 @@ function format_date(date) local year, month, day = date:match("(%d%d%d%d)-(%d%d)-(%d%d)") if not year 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 { yyyy_mm_dd = os.date("%F", time), yyyy = os.date("%Y", time), @@ -21,7 +21,7 @@ function table_to_list(t, kv, cmp) local l = pandoc.List() if kv then - for key, value in pairs(t) do l:insert({key = key, value = value}) end + 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 @@ -34,7 +34,7 @@ end function group_by(l, field, insert) insert = insert or function(group, _, item) if not group then - group = l:new({item}) + group = l:new({ item }) return group end group:insert(item) @@ -101,13 +101,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 {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 {id = layout, ["is_" .. layout] = true} + return { id = layout, ["is_" .. layout] = true } end end @@ -117,7 +117,7 @@ function resolve_namespace(namespace) local root = "index" if namespace ~= "" then root = namespace:gsub("^/([^/]*).*$", "%1") end - return {root = {id = root, ["is_" .. root] = true}, full = namespace} + return { root = { id = root, ["is_" .. root] = true }, full = namespace } end function prep_menu(active_id, main_menu) @@ -137,17 +137,22 @@ function prep_menu(active_id, main_menu) end function process_pages(global, pages_by_id) - if not pages_by_id then pages_by_id = {} end + if not pages_by_id then return nil end local pages_list = pandoc.List() - for _, page in pairs(pages_by_id) do - page = process(global, page) - pages_list:insert(page) - end + for _, page in pairs(pages_by_id) do pages_list:insert(process(global, page)) end pages_list:sort(function(p1, p2) - if p1.date then + if p1.position then + if p2.position then + return p1.position < p2.position + else + return true + end + elseif p2.position then + return false + elseif p1.date then if p2.date then return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd else @@ -166,7 +171,7 @@ function process_pages(global, pages_by_id) if not data then local l = pandoc.List() l:insert(p) - return {name = pandoc.utils.stringify(p.category.name), pages = l} + return { name = pandoc.utils.stringify(p.category.name), pages = l } else data.pages:insert(p) end @@ -174,11 +179,24 @@ function process_pages(global, pages_by_id) pages_by_category = table_to_list(pages_by_category, false, function(i1, i2) return i1.name < i2.name end) - local pages_data = {all = pages_list, by_id = pages_by_id, by_category = pages_by_category} + local pages_data = { all = pages_list, by_id = pages_by_id, by_category = pages_by_category } return pages_data end +function pages_last_update(all_pages) + local last_update = format_date("1990-01-01") + + for i = 1, #all_pages do + local page = all_pages[i] + if page.last_update and page.last_update.yyyy_mm_dd > last_update.yyyy_mm_dd then + last_update = page.last_update + end + end + + return last_update +end + function process(global, meta) meta.namespace = resolve_namespace(meta.namespace) meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") @@ -186,6 +204,22 @@ function process(global, meta) 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.position then meta.position = pandoc.utils.stringify(meta.position) end + + if meta.create_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")), + } + else + meta.page = { + url = resolve_url(global.site.url, global.file_out, + meta.file_out:gsub(".xml$", ".html")), + } + end + end + 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) @@ -193,18 +227,20 @@ function process(global, meta) if meta.date then meta.date = format_date(meta.date) end - if meta.last_update then - meta.last_update = format_date(meta.last_update) - else - meta.last_update = meta.date - 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.pages) + if meta.last_update then + meta.last_update = format_date(meta.last_update) + elseif meta.date then + meta.last_update = meta.date + elseif meta.pages then + meta.last_update = pages_last_update(meta.pages.all) + end + return meta end -- cgit v1.2.3-70-g09d2