From 9acab4200ad454a030edefd0f41e39b3e4936c84 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Thu, 4 Aug 2022 21:27:32 +0200 Subject: Improved metadata processing --- scripts/lib/common.lua | 23 +++++++ scripts/metadata.lua | 39 ++++++++--- scripts/page.lua | 181 +++++++++++++++++++++++++------------------------ 3 files changed, 147 insertions(+), 96 deletions(-) diff --git a/scripts/lib/common.lua b/scripts/lib/common.lua index 5ed3e31..ee7c6aa 100644 --- a/scripts/lib/common.lua +++ b/scripts/lib/common.lua @@ -39,6 +39,20 @@ function pandoc.List:flatMap(fn) return result end +function pandoc.List:filterMap(fn) + local mapped = self:map(fn) + local result = pandoc.List() + + for i = 1, #mapped do + local val = mapped[i] + if val then + result:insert(val) + end + end + + return result +end + function pandoc.List:take(n) if n >= #self then return self end @@ -59,6 +73,15 @@ function pandoc.List:skip(n) return result end +function pandoc.List:shuffle() + for i = #self, 2, -1 do + local j = math.random(i) + self[i], self[j] = self[j], self[i] + end + + return self +end + return { dump = dump } diff --git a/scripts/metadata.lua b/scripts/metadata.lua index afd0ca5..5d372fc 100644 --- a/scripts/metadata.lua +++ b/scripts/metadata.lua @@ -71,13 +71,11 @@ function prep_menu(active_id, main_menu) return { items = items, active = active_item } end -function process_pages(global, build, meta) +function process_pages(meta) local pages_all = pandoc.List() local pages_date_desc = pandoc.List() for id, p in pairs(meta.pages) do - process(global, build, p, meta) - if not p.unlisted then pages_all:insert(id) if p.last_update then pages_date_desc:insert(id) end @@ -86,8 +84,14 @@ function process_pages(global, build, meta) pages_all:sort(sort.page_sort(meta.list_order, meta.pages)) pages_date_desc:sort(sort.page_sort("date_desc", meta.pages)) + local pages_random = pages_all:clone():shuffle() - meta.pages = { all = pages_all, date_desc = pages_date_desc, by_id = meta.pages } + meta.pages = { + all = pages_all, + date_desc = pages_date_desc, + random = pages_random, + by_id = meta.pages + } end function find_depth(meta) @@ -126,7 +130,7 @@ function resolve_dates(meta) meta.was_updated = meta.date and meta.last_update and meta.date.yyyy_mm_dd ~= meta.last_update.yyyy_mm_dd end -function process_base(build, meta, parent) +function process_base(build, meta) meta.namespace = prep_namespace(meta.namespace) meta.file_out = utils.stringify(meta.file_out) meta.unlisted = meta.unlisted or (meta.draft and build.mode ~= "dev") @@ -140,16 +144,33 @@ function process_base(build, meta, parent) meta.position = meta.position and tonumber(utils.stringify(meta.position)) meta.thumbnail = meta.thumbnail and make_absolute("thumbnail." .. utils.stringify(meta.thumbnail), meta.file_out) meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) - meta.show_date = parent and parent.list_order == "date_desc" + + meta.description = (meta.description and pandoc.MetaBlocks(pandoc.Para(meta.description))) or + (not meta.no_description and meta.content) + meta.show_date = meta.parent and meta.parent.list_order == "date_desc" + meta.category = meta.parent and meta.parent.title + meta.icon = meta.icon or (meta.parent and meta.parent.icon) + meta.post_icon = meta.post_icon or (meta.parent and meta.parent.list_post_icon) or + (meta.parent and meta.parent.parent and meta.parent.parent.list_post_icon) + meta.indicator = meta.parent and meta.parent.list_read_indicators end -function process(global, build, meta, parent) - process_base(build, meta, parent) - process_pages(global, build, meta) +function process(global, build, meta) + process_base(build, meta) + + for _, p in pairs(meta.pages) do + p.parent = meta + process(global, build, p) + p.parent = nil + end + + process_pages(meta) resolve_dates(meta) end function Meta(meta) + math.randomseed(os.time()) + local build = {} for key, value in pairs(meta) do diff --git a/scripts/page.lua b/scripts/page.lua index 417e4a0..fd12230 100644 --- a/scripts/page.lua +++ b/scripts/page.lua @@ -35,93 +35,60 @@ function prep_menu(namespace, main_menu) return { items = items, active = active_item } end -function process_pages(global, build, meta) - for _, p in pairs(meta.pages.by_id) do - process_pages(global, build, p) - end - - meta.pages = { - all = meta.pages.all:map(function (ref) return meta.pages.by_id[utils.stringify(ref)] end), - date_desc = meta.pages.date_desc:map(function (ref) return meta.pages.by_id[utils.stringify(ref)] end), - by_id = meta.pages.by_id - } -end - -function resolve_urls(global, build, meta) - meta.url = resolve_url(global.site.url, build.file_out, meta.url) - - if meta.feed then - if meta.file_out:match(".html$") then - meta.feed = { - url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".html$", ".xml")), - } - else - meta.page = { - url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".xml$", ".html")), - } - end - end - - if meta.thumbnail then - meta.thumbnail = resolve_url(global.site.url, build.file_out, meta.thumbnail) - end -end - -function d1_page_to_list_item(meta, p) +function d1_page_to_list_item(meta) return { - title = p.title, - subtitle = p.subtitle, - date = p.date, - last_update = p.last_update, - show_date = p.show_date, - schema_type = p.schema_type, - draft = p.draft, - position = p.position, - url = p.url, - rel = p.rel, - slug = p.slug, - thumbnail = p.thumbnail, - icon = p.icon or meta.icon, - post_icon = p.post_icon or meta.list_post_icon, - indicator = meta.list_read_indicators, + title = meta.title, + subtitle = meta.subtitle, + date = meta.date, + last_update = meta.last_update, + show_date = meta.show_date, + schema_type = meta.schema_type, + draft = meta.draft, + position = meta.position, + url = meta.url, + rel = meta.rel, + slug = meta.slug, + thumbnail = meta.thumbnail, + icon = meta.icon, + post_icon = meta.post_icon, + indicator = meta.indicator, } end -function d2_page_to_list_item(meta, cat, p, set_cat_title) +function d2_page_to_list_item(meta, with_category) return { - title = p.title, - subtitle = p.subtitle, - category = set_cat_title and cat.title, - date = p.date, - last_update = p.last_update, - show_date = p.show_date, - schema_type = p.schema_type, - draft = p.draft, - position = p.position, - url = p.url, - rel = p.rel, - slug = p.slug, - thumbnail = p.thumbnail, - icon = p.icon or cat.icon, - post_icon = p.post_icon or cat.list_post_icon or meta.list_post_icon, - indicator = cat.list_read_indicators, + title = meta.title, + subtitle = meta.subtitle, + category = with_category and meta.category, + date = meta.date, + last_update = meta.last_update, + show_date = meta.show_date, + schema_type = meta.schema_type, + draft = meta.draft, + position = meta.position, + url = meta.url, + rel = meta.rel, + slug = meta.slug, + thumbnail = meta.thumbnail, + icon = meta.icon, + post_icon = meta.post_icon, + indicator = meta.indicator, } end -function cat_to_list_cat(cat, allItems) - local limit = cat.list_limit or 9999 +function cat_to_list_cat(meta, allItems) + local limit = meta.list_limit or 9999 local items = allItems:take(limit) local omitted = #allItems - #items return { - title = cat.title, - description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or - (not cat.no_description and cat.content), - last_update = cat.last_update, - schema_type = cat.schema_type, - url = cat.url, - slug = cat.slug, - layout = cat.list_layout, + title = meta.title, + description = meta.description, + last_update = meta.last_update, + schema_type = meta.schema_type, + url = meta.url, + slug = meta.slug, + layout = meta.list_layout, items = items, total = tostring(#allItems), omitted = omitted ~= 0 and tostring(omitted), @@ -132,13 +99,13 @@ function generate_list(meta) if meta.depth < 1 then return pandoc.List() end if meta.depth == 1 then - return meta.pages.all:map(function(p) return d1_page_to_list_item(meta, p) end) + return meta.pages.all end if meta.depth == 2 then return meta.pages.all :map(function(cat) - local allItems = cat.pages.all:map(function(p) return d2_page_to_list_item(meta, cat, p, false) end) + local allItems = cat.pages.all:map(function(p) return d2_page_to_list_item(p, false) end) return cat_to_list_cat(cat, allItems) end) @@ -150,9 +117,9 @@ function generate_list(meta) :map(function(cat) local allItems = cat.pages.all:flatMap(function(c) if cat.list_flatten and c.depth ~= 0 then - return c.pages.all:map(function(p) return d2_page_to_list_item(cat, c, p, true) end) + return c.pages.all:map(function(p) return d2_page_to_list_item(p, true) end) else - return pandoc.List({ d1_page_to_list_item(cat, c) }) + return pandoc.List({ d1_page_to_list_item(c) }) end end) @@ -166,6 +133,38 @@ function generate_list(meta) end end +function deref_page(pages) + return function(ref) + return pages[utils.stringify(ref)] + end +end + +function process_pages(meta) + meta.pages.all = meta.pages.all:filterMap(deref_page(meta.pages.by_id)) + meta.pages.date_desc = meta.pages.date_desc:filterMap(deref_page(meta.pages.by_id)) + -- meta.pages.random = meta.pages.random:filterMap(deref_page(meta.pages.by_id)) +end + +function resolve_urls(global, build, meta) + meta.url = resolve_url(global.site.url, build.file_out, meta.url) + + if meta.feed then + if meta.file_out:match(".html$") then + meta.feed = { + url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".html$", ".xml")), + } + else + meta.page = { + url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".xml$", ".html")), + } + end + end + + if meta.thumbnail then + meta.thumbnail = resolve_url(global.site.url, build.file_out, meta.thumbnail) + end +end + function process_base(meta) meta.depth = tonumber(utils.stringify(meta.depth)) meta.file_out = utils.stringify(meta.file_out) @@ -177,13 +176,25 @@ function process_base(meta) meta.thumbnail = meta.thumbnail and utils.stringify(meta.thumbnail) end -function process(global, build, meta) +function process(global, build, meta, dir) process_base(meta) resolve_urls(global, build, meta) - for _, p in pairs(meta.pages.by_id) do - process(global, build, p) + if dir >= 0 then + for _, p in pairs(meta.pages.by_id) do + p.parent = meta + process(global, build, p, 1) + p.parent = nil + end end + + process_pages(meta) + + meta.list = generate_list(meta) + + -- if dir <= 0 and meta.parent then + -- process(global, build, meta.parent, -1) + -- end end function Meta(meta) @@ -206,8 +217,6 @@ function Meta(meta) meta = meta.tree - process(global, build, meta) - local parts = utils.stringify(build.namespace):split('/'):skip(1) for i = 1, #parts do local part = parts[i] @@ -217,9 +226,7 @@ function Meta(meta) meta = p end - process_pages(global, build, meta) - - meta.list = generate_list(meta) + process(global, build, meta, 0) if global.menus and global.menus.main then global.menus.main = prep_menu(meta.namespace.full, global.menus.main) -- cgit v1.2.3-54-g00ecf