Editing Module:Arguments

Jump to: navigation, search

Warning: You are not logged in.

Your IP address will be recorded in this page's edit history.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 46: Line 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
  
Line 87: Line 82:
 
local title = parent:getTitle():gsub('/sandbox$', '')
 
local title = parent:getTitle():gsub('/sandbox$', '')
 
local found = false
 
local found = false
if matchesTitle(options.wrappers, title) then
+
if type(options.wrappers) == 'table' then
found = true
+
elseif type(options.wrappers) == 'table' then
+
 
for _,v in pairs(options.wrappers) do
 
for _,v in pairs(options.wrappers) do
if matchesTitle(v, title) then
+
if v == title then
 
found = true
 
found = true
 
break
 
break
 
end
 
end
 
end
 
end
 +
elseif options.wrappers == title then
 +
found = true
 
end
 
end
 
 
Line 170: Line 165:
 
setmetatable(args, metatable)
 
setmetatable(args, metatable)
  
local function mergeArgs(tables)
+
local function mergeArgs(iterator, 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. If a value is already present it is not overwritten;
+
-- into one table using the specified iterator. If a value is already
-- tables listed earlier have precedence. We are also memoizing nil
+
-- present it is not overwritten; tables listed earlier have precedence.
-- values, which can be overwritten if they are 's' (soft).
+
-- We are also memoizing nil values, but those values can be
 +
-- overwritten.
 
--]]
 
--]]
 
for _, t in ipairs(tables) do
 
for _, t in ipairs(tables) do
for key, val in pairs(t) do
+
for key, val in iterator(t) do
if metaArgs[key] == nil and nilArgs[key] ~= 'h' then
+
if metaArgs[key] == nil then
 
local tidiedVal = tidyVal(key, val)
 
local tidiedVal = tidyVal(key, val)
 
if tidiedVal == nil then
 
if tidiedVal == nil then
nilArgs[key] = 's'
+
nilArgs[key] = true
 
else
 
else
 
metaArgs[key] = tidiedVal
 
metaArgs[key] = tidiedVal
Line 223: Line 219:
 
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
Line 258: Line 255:
 
--]]
 
--]]
 
metaArgs[key] = nil
 
metaArgs[key] = nil
nilArgs[key] = 'h'
+
nilArgs[key] = true
 
else
 
else
 
metaArgs[key] = val
 
metaArgs[key] = val
Line 267: Line 264:
 
-- 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(argTables)
+
mergeArgs(pairs, 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 (t)
+
metatable.__ipairs = function ()
 
-- Called when ipairs is run on the args table.
 
-- Called when ipairs is run on the args table.
return inext, t, 0
+
if not metatable.doneIpairs then
 +
mergeArgs(ipairs, argTables)
 +
metatable.doneIpairs = true
 +
end
 +
return ipairs(metaArgs)
 
end
 
end
  
 

Please note that all contributions to The Crowdsourced Resource-Based Economy Knowledgebase are considered to be released under the Creative Commons Zero (Public Domain) (see RBEMWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox
Share