aboutsummaryrefslogtreecommitdiff
path: root/data
diff options
context:
space:
mode:
authorAlbert Krewinkel <albert@zeitkraut.de>2018-01-09 07:29:56 +0100
committerAlbert Krewinkel <albert@zeitkraut.de>2018-01-09 07:44:09 +0100
commitbf944e0aeb60ed831549b7dce485e703dc8363c9 (patch)
tree443d732a02266893e506dedebabe455bb02fb5c7 /data
parent1d8f2edff04068b4d404b4a0338ef3f90173efd4 (diff)
downloadpandoc-bf944e0aeb60ed831549b7dce485e703dc8363c9.tar.gz
data/pandoc.lua: slightly de-complicate accessor code
Change: minor
Diffstat (limited to 'data')
-rw-r--r--data/pandoc.lua40
1 files changed, 20 insertions, 20 deletions
diff --git a/data/pandoc.lua b/data/pandoc.lua
index 670e4a738..44b8b0f1b 100644
--- a/data/pandoc.lua
+++ b/data/pandoc.lua
@@ -38,13 +38,15 @@ local List = require 'pandoc.List'
--- Create a new indexing function.
-- @param template function template
--- @param indices list of indices, starting with the most significant
+-- @param indices list of indices, starting with the most deeply nested
-- @return newly created function
-- @local
function make_indexing_function(template, indices)
local loadstring = loadstring or load
local bracketed = {}
- for i = 1, #indices do bracketed[i] = string.format('[%d]', indices[i]) end
+ for i = 1, #indices do
+ bracketed[i] = string.format('[%d]', indices[#indices - i + 1])
+ end
local fnstr = string.format('return ' .. template, table.concat(bracketed))
return assert(loadstring(fnstr))()
end
@@ -57,15 +59,12 @@ end
local function create_accessor_functions (fn_template, accessors)
local res = {}
function add_accessors(acc, ...)
- local indices = {...} -- ensure a fresh indices table
if type(acc) == "string" then
- res[acc] = make_indexing_function(fn_template, indices)
+ res[acc] = make_indexing_function(fn_template, {...})
else
- local ind_len = #indices
local unpack = table.unpack or unpack
for i = 1, #(acc or {}) do
- indices[ind_len + 1] = i
- add_accessors(acc[i], unpack(indices))
+ add_accessors(acc[i], i, ...)
end
end
end
@@ -165,6 +164,8 @@ AstElement.__call = function(t, ...)
end
end
+--- Make a new subtype which constructs a new value when called.
+-- @local
function AstElement:make_subtype(...)
local newtype = Type.make_subtype(self, ...)
newtype.__call = self.__call
@@ -226,27 +227,26 @@ M.MetaValue = AstElement:make_subtype('MetaValue')
--- Meta blocks
-- @function MetaBlocks
-- @tparam {Block,...} blocks blocks
+M.MetaBlocks = M.MetaValue:create_constructor(
+ 'MetaBlocks',
+ function (content) return List:new(content) end
+)
--- Meta inlines
-- @function MetaInlines
-- @tparam {Inline,...} inlines inlines
+M.MetaInlines = M.MetaValue:create_constructor(
+ 'MetaInlines',
+ function (content) return List:new(content) end
+)
--- Meta list
-- @function MetaList
-- @tparam {MetaValue,...} meta_values list of meta values
-M.meta_value_list_types = {
- "MetaBlocks",
- "MetaInlines",
- "MetaList",
-}
-for i = 1, #M.meta_value_list_types do
- M[M.meta_value_list_types[i]] = M.MetaValue:create_constructor(
- M.meta_value_list_types[i],
- function(content)
- return List:new(content)
- end
- )
-end
+M.MetaList = M.MetaValue:create_constructor(
+ 'MetaList',
+ function (content) return List:new(content) end
+)
--- Meta map
-- @function MetaMap