diff options
Diffstat (limited to 'scripts/lib')
-rw-r--r-- | scripts/lib/common.lua | 54 |
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 @@ | |||
1 | function 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 | ||
12 | end | ||
13 | |||
14 | function 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 | ||
23 | end | ||
24 | |||
25 | function pandoc.List:flatten() | ||
26 | local result = pandoc.List() | ||
27 | |||
28 | for i = 1, #self do result:extend(self[i]) end | ||
29 | |||
30 | return result | ||
31 | end | ||
32 | |||
33 | function 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 | ||
40 | end | ||
41 | |||
42 | function 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 | ||
50 | end | ||
51 | |||
52 | return { | ||
53 | dump = dump | ||
54 | } | ||