summaryrefslogtreecommitdiffstats
path: root/scripts/lib/common.lua
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/common.lua')
-rw-r--r--scripts/lib/common.lua54
1 files changed, 54 insertions, 0 deletions
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 @@
1function dump(o)
2 if type(o) == 'table' then
3 local s = '{ '
4 for k, v in pairs(o) do
5 if type(k) ~= 'number' then k = '"' .. k .. '"' end
6 s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
7 end
8 return s .. '} '
9 else
10 return tostring(o)
11 end
12end
13
14function string.split(str, sep)
15 sep = sep or '%s'
16
17 local parts = pandoc.List()
18
19 for field, s in str:gmatch("([^" .. sep .. "]*)(" .. sep .. "?)") do
20 parts:insert(field)
21 if s == "" then return parts end
22 end
23end
24
25function pandoc.List:flatten()
26 local result = pandoc.List()
27
28 for i = 1, #self do result:extend(self[i]) end
29
30 return result
31end
32
33function pandoc.List:flatMap(fn)
34 local mapped = self:map(fn)
35 local result = pandoc.List()
36
37 for i = 1, #mapped do result:extend(mapped[i]) end
38
39 return result
40end
41
42function pandoc.List:take(n)
43 if n >= #self then return self end
44
45 local result = pandoc.List()
46
47 for i = 1, n do result:insert(self[i]) end
48
49 return result
50end
51
52return {
53 dump = dump
54}