From 6c81b7e3e6119e91160581a2e8f393df8424abe6 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sat, 11 Jun 2022 18:44:07 +0200 Subject: Fixed page ordering in level 3 lists --- scripts/metadata_filter.lua | 229 +++++++++++++++++++++++--------------------- 1 file changed, 121 insertions(+), 108 deletions(-) (limited to 'scripts') diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 4ac4a4d..fcceffa 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua @@ -1,5 +1,13 @@ local path = require 'pandoc.path' +function pandoc.List:flatten() + local result = pandoc.List() + + for i = 1, #self do result:extend(self[i]) end + + return result +end + function pandoc.List:flatMap(fn) local mapped = self:map(fn) local result = pandoc.List() @@ -19,6 +27,32 @@ function pandoc.List:take(n) return result end +function page_sort(order) + return function(p1, p2) + if p1.position then + if p2.position then + return tonumber(p1.position) < tonumber(p2.position) + else + return true + end + elseif p2.position then + return false + elseif order == "date_desc" then + if p1.date then + if p2.date then + return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd + else + return true + end + else + return false + end + else + return p1.title:upper() < p2.title:upper() + end + end +end + function slug(str) return str:lower():gsub("[^ a-z]", ""):gsub("[ ]+", "-") end @@ -135,41 +169,8 @@ function process_pages(global, order, pages_by_id) end end - pages_all:sort(function(p1, p2) - if p1.position then - if p2.position then - return tonumber(p1.position) < tonumber(p2.position) - else - return true - end - elseif p2.position then - return false - elseif order == "date_desc" then - if p1.date then - if p2.date then - return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd - else - return true - end - else - return false - end - else - return p1.title:upper() < p2.title:upper() - end - end) - - pages_date_desc:sort(function(p1, p2) - if p1.position then - if p2.position then - return tonumber(p1.position) < tonumber(p2.position) - else - return true - end - else - return p1.date.yyyy_mm_dd > p2.date.yyyy_mm_dd - end - end) + pages_all:sort(page_sort(order)) + pages_date_desc:sort(page_sort("date_desc")) local pages_data = { all = pages_all, date_desc = pages_date_desc, by_id = pages_by_id } @@ -199,6 +200,7 @@ function generate_list(meta) title = p.title, subtitle = p.subtitle, date = p.date, + position = p.position, url = p.url, slug = p.slug, thumbnail = p.thumbnail, @@ -208,85 +210,96 @@ function generate_list(meta) } end) elseif meta.depth == "2" then - return meta.pages.all:map(function(cat) - local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or - 9999 - local allItems = ((cat.pages and cat.pages.all) or pandoc.List()):map(function(p) + return meta.pages.all + :map(function(cat) + local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or + 9999 + local allItems = ((cat.pages and cat.pages.all) or pandoc.List()):map(function(p) + return { + title = p.title, + subtitle = p.subtitle, + date = p.date, + position = p.position, + url = p.url, + 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, + } + end) + local items = allItems:take(limit) + local omitted = #allItems - #items + return { - title = p.title, - subtitle = p.subtitle, - date = p.date, - url = p.url, - 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 = cat.title, + description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or + (not cat.no_description and cat.content), + url = cat.url, + slug = cat.slug, + layout = cat.list_layout, + items = items, + total = tostring(#allItems), + omitted = omitted ~= 0 and tostring(omitted), } end) - 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), - url = cat.url, - slug = cat.slug, - layout = cat.list_layout, - items = items, - total = tostring(#allItems), - omitted = omitted ~= 0 and tostring(omitted), - } - end):filter(function(cat) return #cat.items ~= 0 end) + :filter(function(cat) return #cat.items ~= 0 end) elseif meta.depth == "3" then - return meta.pages.all:map(function(cat) - local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or - 9999 - local allItems = (cat.pages and cat.pages.all or pandoc.List()):flatMap(function(c) - if c.pages then - return c.pages.all:map(function(p) - return { - title = p.title, - subtitle = p.subtitle, - category = c.title, - url = p.url, - slug = p.slug, - thumbnail = p.thumbnail, - icon = p.icon or c.icon, - post_icon = p.post_icon or c.list_post_icon or cat.list_post_icon, - indicator = c.list_read_indicators, - } + return meta.pages.all + :map(function(cat) + local limit = (cat.list_limit and tonumber(pandoc.utils.stringify(cat.list_limit))) or + 9999 + local allItems = (cat.pages and cat.pages.all or pandoc.List()) + :flatMap(function(c) + if c.pages then + return c.pages.all:map(function(p) + return { + title = p.title, + subtitle = p.subtitle, + date = p.date, + position = p.position, + category = c.title, + url = p.url, + slug = p.slug, + thumbnail = p.thumbnail, + icon = p.icon or c.icon, + post_icon = p.post_icon or 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, + date = c.date, + position = c.position, + url = c.url, + slug = c.slug, + icon = c.icon or cat.icon, + post_icon = c.post_icon or cat.list_post_icon, + indicator = cat.list_read_indicators, + }) + return l + end end) - else - local l = pandoc.List() - l:insert({ - title = c.title, - subtitle = c.subtitle, - url = c.url, - slug = c.slug, - icon = c.icon or cat.icon, - post_icon = c.post_icon or cat.list_post_icon, - indicator = cat.list_read_indicators, - }) - return l - end - end) - local items = allItems:take(limit) - local omitted = #allItems - #items + allItems:sort(page_sort(cat.list_order)) + 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), - url = cat.url, - slug = cat.slug, - layout = cat.list_layout, - items = items, - total = tostring(#allItems), - omitted = omitted ~= 0 and tostring(omitted), - } - end):filter(function(cat) return #cat.items ~= 0 end) + return { + title = cat.title, + description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or + (not cat.no_description and cat.content), + url = cat.url, + slug = cat.slug, + layout = cat.list_layout, + items = items, + total = tostring(#allItems), + omitted = omitted ~= 0 and tostring(omitted), + } + end) + :filter(function(cat) return #cat.items ~= 0 end) end end -- cgit v1.2.3-70-g09d2