From 944a735c39a27fc86569c585f2805837b61adb60 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 2 Aug 2022 20:56:45 +0200 Subject: Slightly tidied up metadata handling --- scripts/lib/common.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 scripts/lib/common.lua (limited to 'scripts/lib') diff --git a/scripts/lib/common.lua b/scripts/lib/common.lua new file mode 100644 index 0000000..80b81ba --- /dev/null +++ b/scripts/lib/common.lua @@ -0,0 +1,54 @@ +function dump(o) + if type(o) == 'table' then + local s = '{ ' + for k, v in pairs(o) do + if type(k) ~= 'number' then k = '"' .. k .. '"' end + s = s .. '[' .. k .. '] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return tostring(o) + end +end + +function string.split(str, sep) + sep = sep or '%s' + + local parts = pandoc.List() + + for field, s in str:gmatch("([^" .. sep .. "]*)(" .. sep .. "?)") do + parts:insert(field) + if s == "" then return parts end + end +end + +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() + + for i = 1, #mapped do result:extend(mapped[i]) end + + return result +end + +function pandoc.List:take(n) + if n >= #self then return self end + + local result = pandoc.List() + + for i = 1, n do result:insert(self[i]) end + + return result +end + +return { + dump = dump +} -- cgit v1.2.3-70-g09d2