git.iko.soy/typeable/pandoc
on master help sign in
pandoc.List.lua: make `pandoc.List` a callable constructor
e98921aAlbert Krewinkel
data/pandoc.List.lua+20−18
@@ -1,28 +1,27 @@
1--[[
1--[[
2List.lua
2List.lua
3
3
4−Copyright © 2017–2018 Albert Krewinkel
4+Copyright © 2017–2020 Albert Krewinkel
5
5
6−Permission to use, copy, modify, and/or distribute this software for any purpose
6+Permission to use, copy, modify, and/or distribute this software for any
7−with or without fee is hereby granted, provided that the above copyright notice
7+purpose with or without fee is hereby granted, provided that the above
8−and this permission notice appear in all copies.
8+copyright notice and this permission notice appear in all copies.
9
9
10−THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11−REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12−FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13−INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
13+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14−OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15−TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
16+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ]]
16−THIS SOFTWARE.
17−]]
18
17
19--- Pandoc's List type and helper methods
18--- Pandoc's List type and helper methods
20-- @classmod pandoc.List
19-- @classmod pandoc.List
21-- @author Albert Krewinkel
20-- @author Albert Krewinkel
22−-- @copyright © 2017–2018 Albert Krewinkel
21+-- @copyright © 2017–2020 Albert Krewinkel
23-- @license MIT
22-- @license MIT
24local List = {
23local List = {
25− _VERSION = "0.1.0"
24+ _VERSION = "1.0.0"
26}
25}
27
26
28function List:new (o)
27function List:new (o)
@@ -32,10 +31,6 @@ function List:new (o)
32 return o
31 return o
33end
32end
34
33
35−function List:__call (o)
36− return self:new(o)
37−end
38−
39--- Concatenates two lists.
34--- Concatenates two lists.
40-- @param list second list concatenated to the first
35-- @param list second list concatenated to the first
41-- @return a new list containing all elements from list1 and list2
36-- @return a new list containing all elements from list1 and list2
@@ -117,4 +112,11 @@ function List:filter (pred)
117 return res
112 return res
118end
113end
119
114
115+-- Set metatable with __call metamethod. This allows the use of `List`
116+-- as a constructor function.
117+local ListMT = {
118+ __call = List.new
119+}
120+setmetatable(List, ListMT)
121+
120return List
122return List
doc/lua-filters.md+23−1
@@ -2662,7 +2662,16 @@ Usage:
2662
2662
2663# Module pandoc.List
2663# Module pandoc.List
2664
2664
2665−Pandoc\'s List type and helper methods
2665+The this module defines pandoc's list type. It comes with useful
2666+methods and convenience functions.
2667+
2668+## Constructor
2669+
2670+[`pandoc.List([table])`]{#pandoc.List}
2671+
2672+: Create a new List. If the optional argument `table` is given,
2673+ set the metatable of that value to `pandoc.List`. This is an
2674+ alias for [`pandoc.List:new([table])`](#pandoc.list:new).
2666
2675
2667## Metamethods
2676## Metamethods
2668
2677
@@ -2684,6 +2693,19 @@ Pandoc\'s List type and helper methods
2684
2693
2685: Returns a (shallow) copy of the list.
2694: Returns a (shallow) copy of the list.
2686
2695
2696+[`pandoc.List:new([table])`]{#pandoc.List:new}
2697+
2698+: Create a new List. If the optional argument `table` is given,
2699+ set the metatable of that value to `pandoc.List`.
2700+
2701+ Parameters:
2702+
2703+ `table`:
2704+ : table which should be treatable as a list; defaults to an
2705+ empty table
2706+
2707+ Returns: the updated input value
2708+
2687[`pandoc.List:includes (needle, init)`]{#pandoc.List:includes}
2709[`pandoc.List:includes (needle, init)`]{#pandoc.List:includes}
2688
2710
2689: Checks if the list has an item equal to the given needle.
2711: Checks if the list has an item equal to the given needle.
test/lua/module/pandoc-list.lua+9
@@ -6,6 +6,15 @@ local test = tasty.test_case
6local group = tasty.test_group
6local group = tasty.test_group
7
7
8return {
8return {
9+ group 'List as function' {
10+ test('equivalent to List:new', function (x)
11+ local new = List:new {'ramen'}
12+ local list = List {'ramen'}
13+ assert.are_same(new, list)
14+ assert.are_equal(getmetatable(new), getmetatable(list))
15+ end)
16+ },
17+
9 group 'new' {
18 group 'new' {
10 test('make table usable as list', function ()
19 test('make table usable as list', function ()
11 local test = List:new{1, 1, 2, 3, 5}
20 local test = List:new{1, 1, 2, 3, 5}