From 347d7c0da13079fefce6d8741a5604adb89eb97c Mon Sep 17 00:00:00 2001 From: Volpeon Date: Thu, 4 Aug 2022 10:26:57 +0200 Subject: Overhauled metadata handling --- scripts/lib/common.lua | 10 ++++++++++ scripts/lib/sort.lua | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 scripts/lib/sort.lua (limited to 'scripts/lib') diff --git a/scripts/lib/common.lua b/scripts/lib/common.lua index 80b81ba..5ed3e31 100644 --- a/scripts/lib/common.lua +++ b/scripts/lib/common.lua @@ -49,6 +49,16 @@ function pandoc.List:take(n) return result end +function pandoc.List:skip(n) + local result = pandoc.List() + + if n >= #self then return result end + + for i = n + 1, #self do result:insert(self[i]) end + + return result +end + return { dump = dump } diff --git a/scripts/lib/sort.lua b/scripts/lib/sort.lua new file mode 100644 index 0000000..393e5d5 --- /dev/null +++ b/scripts/lib/sort.lua @@ -0,0 +1,36 @@ +local utils = require 'pandoc.utils' + +function page_sort(order, pages) + order = order and utils.stringify(order) + + return function(ref1, ref2) + local p1 = pages and pages[utils.stringify(ref1)] or ref1 + local p2 = pages and pages[utils.stringify(ref2)] or ref2 + + if p1.position then + if p2.position then + return tonumber(utils.stringify(p1.position)) < tonumber(utils.stringify(p2.position)) + else + return true + end + elseif p2.position then + return false + elseif order == "date_desc" then + if p1.last_update then + if p2.last_update then + return utils.stringify(p1.last_update.yyyy_mm_dd) > utils.stringify(p2.last_update.yyyy_mm_dd) + else + return true + end + else + return false + end + else + return utils.stringify(p1.title):upper() < utils.stringify(p2.title):upper() + end + end +end + +return { + page_sort = page_sort +} -- cgit v1.2.3-70-g09d2