aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--data/List.lua55
-rw-r--r--data/pandoc.lua2
-rw-r--r--src/Text/Pandoc/Lua/PandocModule.hs2
3 files changed, 35 insertions, 24 deletions
diff --git a/data/List.lua b/data/List.lua
index db6233c86..b68ff5119 100644
--- a/data/List.lua
+++ b/data/List.lua
@@ -16,16 +16,11 @@ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
]]
----
--- Lua functions for pandoc scripts.
---
+--- Pandoc's List type and helper methods
+-- @classmod pandoc.List
-- @author Albert Krewinkel
-- @copyright © 2017 Albert Krewinkel
-- @license MIT
-
-------------------------------------------------------------------------
--- Metatable for lists
--- @type List
local List = {
_VERSION = "0.1.0"
}
@@ -42,6 +37,8 @@ function List:__call (o)
end
--- Concatenates two lists.
+-- @param list second list concatenated to the first
+-- @return a new list containing all elements from list1 and list2
function List:__concat (list)
local res = List.clone(self)
List.extend(res, list)
@@ -55,40 +52,40 @@ function List:clone ()
return lst
end
---- Appends the given list to the end of this list.
-function List:includes (needle)
- for i = 1, #self do
- if self[i] == needle then
- return true
- end
- end
- return false
+--- Checks if the list has an item equal to the given needle.
+-- @param needle item to search for
+-- @param init index at which the search is started
+-- @return true if a list item is equal to the needle, false otherwise
+function List:includes (needle, init)
+ return not (List.find(self, needle, init) == nil)
end
--- Returns the value and index of the first occurrence of the given item.
-- @param needle item to search for
+-- @param init index at which the search is started
-- @return first item equal to the needle, or nil if no such item exists.
-- @return index of that element
function List:find (needle, init)
return List.find_if(self, function(x) return x == needle end, init)
end
---- Returns the value and index of the first element for which test returns true.
--- @param test the test function
+--- Returns the value and index of the first element for which the predicate
+--- holds true.
+-- @param pred the predicate function
-- @param init index at which the search is started
-- @return first item for which `test` succeeds, or nil if no such item exists.
-- @return index of that element
-function List:find_if (test, init)
+function List:find_if (pred, init)
init = (init == nil and 1) or (init < 0 and #self - init) or init
for i = init, #self do
- if test(self[i], i) then
+ if pred(self[i], i) then
return self[i], i
end
end
return nil
end
---- Add the given list to the end of this list.
+--- Adds the given list to the end of this list.
-- @param list list to appended
function List:extend (list)
for i = 1, #list do
@@ -96,12 +93,26 @@ function List:extend (list)
end
end
--- Returns a copy of the current list by applying the given function to all
+--- Returns a copy of the current list by applying the given function to all
-- elements.
+-- @param fn function which is applied to all list items.
function List:map (fn)
local res = setmetatable({}, getmetatable(self))
for i = 1, #self do
- res[i] = fn(self[i])
+ res[i] = fn(self[i], i)
+ end
+ return res
+end
+
+--- Returns a new list containing all items satisfying a given condition.
+-- @param pred condition items must satisfy.
+-- @return a new list containing all items for which `test` was true.
+function List:filter (pred)
+ local res = setmetatable({}, getmetatable(self))
+ for i = 1, #self do
+ if pred(self[i], i) then
+ res[#res + 1] = self[i]
+ end
end
return res
end
diff --git a/data/pandoc.lua b/data/pandoc.lua
index 6eafde90a..74263b1fd 100644
--- a/data/pandoc.lua
+++ b/data/pandoc.lua
@@ -26,7 +26,7 @@ local M = {
_VERSION = "0.3.0"
}
-local List = require 'List'
+local List = require 'pandoc.List'
------------------------------------------------------------------------
-- The base class for pandoc's AST elements.
diff --git a/src/Text/Pandoc/Lua/PandocModule.hs b/src/Text/Pandoc/Lua/PandocModule.hs
index ba3193211..4df01f019 100644
--- a/src/Text/Pandoc/Lua/PandocModule.hs
+++ b/src/Text/Pandoc/Lua/PandocModule.hs
@@ -91,7 +91,7 @@ loadListModule :: Maybe FilePath -> Lua ()
loadListModule datadir = do
Lua.getglobal' "package.loaded"
pushListModule datadir
- Lua.setfield (-2) "List"
+ Lua.setfield (-2) "pandoc.List"
Lua.pop 1
walkElement :: (ToLuaStack a, Walkable [Inline] a, Walkable [Block] a)