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/page.lua | 181 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 94 insertions(+), 87 deletions(-) (limited to 'scripts/page.lua') 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