From ac027192285ad741b3ac1b837e1c363beb8f3791 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sun, 14 Nov 2021 09:28:25 +0100 Subject: Generate special list metadata --- scripts/metadata_filter.lua | 96 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 8 deletions(-) (limited to 'scripts') diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index ebd69f1..b16d472 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -1,5 +1,14 @@ local path = require 'pandoc.path' +function pandoc.List:flatMap(fn) + local mapped = self:map(fn) + local result = pandoc.List() + + for i = 1, #mapped do result:extend(mapped[i]) end + + return result +end + function format_date(date) if not date then return date end @@ -49,14 +58,12 @@ function resolve_url(site_url, ref_file, target_file) end function resolve_layout(depth) - local layout = "deep_categorized_list" + local layout = "categorized_list" if depth == "0" then layout = "page" elseif depth == "1" then layout = "list" - elseif depth == "2" then - layout = "categorized_list" end return layout @@ -91,14 +98,14 @@ function prep_menu(active_id, main_menu) return { items = items, active = active_item } end -function process_pages(global, order, pages_by_id) +function process_pages(global, parent, order, pages_by_id) if not pages_by_id then return nil end local pages_all = pandoc.List() local pages_date_desc = pandoc.List() for _, page in pairs(pages_by_id) do - local p = process(global, page) + local p = process(global, parent, page) if not p.unlisted then pages_all:insert(p) if p.date then pages_date_desc:insert(p) end @@ -176,7 +183,77 @@ function find_depth(pages) return tostring(depth) end -function process(global, meta) +function generate_list(meta) + if meta.depth == "1" then + return meta.pages.all:map(function(p) + return { + title = p.title, + subtitle = p.subtitle, + date = p.date, + url = p.url, + icon = p.icon or meta.icon, + post_icon = meta.list_post_icon, + indicator = meta.list_read_indicators, + } + end) + elseif meta.depth == "2" then + return meta.pages.all:map(function(cat) + return { + title = cat.title, + content = pandoc.utils.stringify(cat.content), + url = cat.url, + grid = cat.list_grid, + items = cat.pages.all:map(function(p) + return { + title = p.title, + subtitle = p.subtitle, + date = p.date, + url = p.url, + icon = p.icon or cat.icon, + post_icon = cat.list_post_icon or meta.list_post_icon, + indicator = cat.list_read_indicators, + } + end), + } + end) + elseif meta.depth == "3" then + return meta.pages.all:map(function(cat) + return { + title = cat.title, + content = pandoc.utils.stringify(cat.content), + url = cat.url, + grid = cat.list_grid, + items = cat.pages.all:flatMap(function(c) + if c.pages then + return c.pages.all:map(function(p) + return { + title = p.title, + subtitle = p.subtitle or c.title, + url = p.url, + icon = p.icon or c.icon, + post_icon = c.list_post_icon or cat.list_post_icon, + indicator = c.list_read_indicators, + } + end) + else + local l = pandoc.List() + l:insert({ + title = c.title, + subtitle = c.subtitle, + url = c.url, + icon = c.icon or meta.icon, + post_icon = meta.list_post_icon, + indicator = meta.list_read_indicators, + }) + return l + end + end), + } + end) + end +end + +function process(global, parent, meta) meta.namespace = resolve_namespace(meta.namespace) meta.file_out = pandoc.utils.stringify(meta.file_out):gsub("^out", "") meta.url = meta.url and pandoc.utils.stringify(meta.url) @@ -209,7 +286,8 @@ function process(global, meta) meta.menus.main = prep_menu(meta.namespace.root.id, meta.menus.main) end - meta.pages = process_pages(global, meta.list_order, meta.pages) + meta.pages = + process_pages(global, { parent = parent, meta = meta }, meta.list_order, meta.pages) meta.depth = (meta.pages and find_depth(meta.pages.all)) or "0" meta.layout = prep_layout(meta.layout or resolve_layout(meta.depth)) @@ -227,11 +305,13 @@ function process(global, meta) meta.last_update = meta.date end + meta.list = generate_list(meta) + return meta end function Meta(meta) meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") - return process(meta, meta) + return process(meta, nil, meta) end -- cgit v1.2.3-54-g00ecf