「モジュール:Arguments」の版間の差分

ナビゲーションに移動 検索に移動
en:Module:Arguments 22:31, 31 January 2015‎(UTC)より
template>Fryed-peach
(en:Module:Arguments 最新版より)
template>K-iczn
(en:Module:Arguments 22:31, 31 January 2015‎(UTC)より)
46行目: 46行目:
local function tidyValNoChange(key, val)
local function tidyValNoChange(key, val)
return val
return val
end
local function matchesTitle(given, title)
local tp = type( given )
return (tp == 'string' or tp == 'number') and mw.title.new( given ).prefixedText == title
end
end


82行目: 87行目:
local title = parent:getTitle():gsub('/sandbox$', '')
local title = parent:getTitle():gsub('/sandbox$', '')
local found = false
local found = false
if type(options.wrappers) == 'table' then
if matchesTitle(options.wrappers, title) then
found = true
elseif type(options.wrappers) == 'table' then
for _,v in pairs(options.wrappers) do
for _,v in pairs(options.wrappers) do
if v == title then
if matchesTitle(v, title) then
found = true
found = true
break
break
end
end
end
end
elseif options.wrappers == title then
found = true
end
end
165行目: 170行目:
setmetatable(args, metatable)
setmetatable(args, metatable)


local function mergeArgs(iterator, tables)
local function mergeArgs(tables)
--[[
--[[
-- Accepts multiple tables as input and merges their keys and values
-- Accepts multiple tables as input and merges their keys and values
-- into one table using the specified iterator. If a value is already
-- into one table. If a value is already present it is not overwritten;
-- present it is not overwritten; tables listed earlier have precedence.
-- tables listed earlier have precedence. We are also memoizing nil
-- We are also memoizing nil values, but those values can be
-- values, which can be overwritten if they are 's' (soft).
-- overwritten.
--]]
--]]
for _, t in ipairs(tables) do
for _, t in ipairs(tables) do
for key, val in iterator(t) do
for key, val in pairs(t) do
if metaArgs[key] == nil then
if metaArgs[key] == nil and nilArgs[key] ~= 'h' then
local tidiedVal = tidyVal(key, val)
local tidiedVal = tidyVal(key, val)
if tidiedVal == nil then
if tidiedVal == nil then
nilArgs[key] = true
nilArgs[key] = 's'
else
else
metaArgs[key] = tidiedVal
metaArgs[key] = tidiedVal
219行目: 223行目:
for _, argTable in ipairs(argTables) do
for _, argTable in ipairs(argTables) do
local argTableVal = tidyVal(key, argTable[key])
local argTableVal = tidyVal(key, argTable[key])
if argTableVal == nil then
if argTableVal ~= nil then
nilArgs[key] = true
else
metaArgs[key] = argTableVal
metaArgs[key] = argTableVal
return argTableVal
return argTableVal
end
end
end
end
nilArgs[key] = 'h'
return nil
return nil
end
end
255行目: 258行目:
--]]
--]]
metaArgs[key] = nil
metaArgs[key] = nil
nilArgs[key] = true
nilArgs[key] = 'h'
else
else
metaArgs[key] = val
metaArgs[key] = val
264行目: 267行目:
-- Called when pairs is run on the args table.
-- Called when pairs is run on the args table.
if not metatable.donePairs then
if not metatable.donePairs then
mergeArgs(pairs, argTables)
mergeArgs(argTables)
metatable.donePairs = true
metatable.donePairs = true
metatable.doneIpairs = true
end
end
return pairs(metaArgs)
return pairs(metaArgs)
end
local function inext(t, i)
-- This uses our __index metamethod
local v = t[i + 1]
if v ~= nil then
return i + 1, v
end
end
end


metatable.__ipairs = function ()
metatable.__ipairs = function (t)
-- Called when ipairs is run on the args table.
-- Called when ipairs is run on the args table.
if not metatable.doneIpairs then
return inext, t, 0
mergeArgs(ipairs, argTables)
metatable.doneIpairs = true
end
return ipairs(metaArgs)
end
end


匿名利用者

案内メニュー