From 6de1ad4cdc8b1fb10cf0a5c61d5ed03893f131c1 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Fri, 5 Aug 2022 18:06:20 +0200 Subject: Show related content --- scripts/metadata.lua | 4 ---- scripts/page.lua | 33 +++++++++++++++++++++++++-------- templates/layouts/exhibit.html | 2 ++ templates/layouts/page.html | 4 ++-- templates/partials/related.html | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 templates/partials/related.html diff --git a/scripts/metadata.lua b/scripts/metadata.lua index d0a4f45..49a9c55 100644 --- a/scripts/metadata.lua +++ b/scripts/metadata.lua @@ -84,12 +84,10 @@ function process_pages(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, - random = pages_random, by_id = meta.pages } end @@ -167,8 +165,6 @@ function process(global, build, 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 f3802a2..dcbb36f 100644 --- a/scripts/page.lua +++ b/scripts/page.lua @@ -96,7 +96,7 @@ function generate_list(meta) if meta.depth < 1 then return pandoc.List() end if meta.depth == 1 then - return meta.pages.all + return meta.pages.all:map(function (p) return d1_page_to_list_item(p) end) end if meta.depth == 2 then @@ -130,6 +130,22 @@ function generate_list(meta) end end +function generate_related(meta) + if not meta.parent then return nil end + + meta = meta.parent + + local items = meta.pages.random:take(2):map(function (p) return d1_page_to_list_item(p) end) + + if #items == 0 then return nil end + + return { + url = meta.url, + layout = meta.list_layout, + items = items + } +end + function deref_page(pages) return function(ref) return pages[utils.stringify(ref)] @@ -139,7 +155,7 @@ 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)) + meta.pages.random = meta.pages.all :clone():shuffle() end function resolve_urls(global, build, meta) @@ -177,12 +193,10 @@ function process(global, build, meta, dir) process_base(meta) resolve_urls(global, build, meta) - 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 + for _, p in pairs(meta.pages.by_id) do + p.parent = meta + process(global, build, p, 1) + p.parent = nil end process_pages(meta) @@ -217,6 +231,8 @@ function prune(meta, up, down) end function Meta(meta) + math.randomseed(os.time()) + local build = {} for key, value in pairs(meta) do @@ -250,6 +266,7 @@ function Meta(meta) process(global, build, meta, 0) meta.list = generate_list(meta) + meta.related = generate_related(meta) if global.menus and global.menus.main then global.menus.main = prep_menu(meta.namespace.full, global.menus.main) diff --git a/templates/layouts/exhibit.html b/templates/layouts/exhibit.html index fcc92a4..7a50635 100644 --- a/templates/layouts/exhibit.html +++ b/templates/layouts/exhibit.html @@ -53,6 +53,8 @@ $body$ + + $partials/related()$ diff --git a/templates/layouts/page.html b/templates/layouts/page.html index f26c4bc..b1f7835 100644 --- a/templates/layouts/page.html +++ b/templates/layouts/page.html @@ -30,8 +30,6 @@ $body$ $if(references)$ $endif$ + + $partials/related()$
diff --git a/templates/partials/related.html b/templates/partials/related.html new file mode 100644 index 0000000..b0d3f61 --- /dev/null +++ b/templates/partials/related.html @@ -0,0 +1,35 @@ +$if(related)$ + +$endif$ -- cgit v1.2.3-70-g09d2