Module:Arguments: Difference between revisions

Fredg999 (talk | contribs)
Copied from Wikipedia
 
ArrowHead294 (talk | contribs)
mNo edit summary
 
Line 3: Line 3:
-- called from #invoke directly.
-- called from #invoke directly.


local libraryUtil = require('libraryUtil')
local libraryUtil = require("libraryUtil")
local checkType = libraryUtil.checkType
local checkType = libraryUtil.checkType


Line 12: Line 12:


local function tidyValDefault(key, val)
local function tidyValDefault(key, val)
if type(val) == 'string' then
if type(val) == "string" then
val = val:match('^%s*(.-)%s*$')
val = val:match("^%s*(.-)%s*$")
if val == '' then
if val == "" then
return nil
return nil
else
else
Line 25: Line 25:


local function tidyValTrimOnly(key, val)
local function tidyValTrimOnly(key, val)
if type(val) == 'string' then
if type(val) == "string" then
return val:match('^%s*(.-)%s*$')
return val:match("^%s*(.-)%s*$")
else
else
return val
return val
Line 33: Line 33:


local function tidyValRemoveBlanksOnly(key, val)
local function tidyValRemoveBlanksOnly(key, val)
if type(val) == 'string' then
if type(val) == "string" then
if val:find('%S') then
if val:find("%S") then
return val
return val
else
else
Line 49: Line 49:


local function matchesTitle(given, title)
local function matchesTitle(given, title)
local tp = type( given )
local tp = type(given)
return (tp == 'string' or tp == 'number') and mw.title.new( given ).prefixedText == title
return (tp == "string" or tp == "number") and mw.title.new( given ).prefixedText == title
end
end


Line 56: Line 56:


function arguments.getArgs(frame, options)
function arguments.getArgs(frame, options)
checkType('getArgs', 1, frame, 'table', true)
checkType("getArgs", 1, frame, "table", true)
checkType('getArgs', 2, options, 'table', true)
checkType("getArgs", 2, options, "table", true)
frame = frame or {}
frame = frame or {}
options = options or {}
options = options or {}
Line 95: Line 95:
--]]
--]]
local fargs, pargs, luaArgs
local fargs, pargs, luaArgs
if type(frame.args) == 'table' and type(frame.getParent) == 'function' then
if type(frame.args) == "table" and type(frame.getParent) == "function" then
if options.wrappers then
if options.wrappers then
--[[
--[[
Line 112: Line 112:
fargs = frame.args
fargs = frame.args
else
else
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 matchesTitle(options.wrappers, title) then
found = true
found = true
elseif type(options.wrappers) == 'table' then
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 matchesTitle(v, title) then
Line 165: Line 165:
local tidyVal = options.valueFunc
local tidyVal = options.valueFunc
if tidyVal then
if tidyVal then
if type(tidyVal) ~= 'function' then
if type(tidyVal) ~= "function" then
error(
error(
"bad value assigned to option 'valueFunc'"
"bad value assigned to option \"valueFunc\""
.. '(function expected, got '
.. "(function expected, got "
.. type(tidyVal)
.. type(tidyVal)
.. ')',
.. ")",
2
2
)
)
Line 206: Line 206:
for _, t in ipairs(tables) do
for _, t in ipairs(tables) do
for key, val in pairs(t) do
for key, val in pairs(t) do
if metaArgs[key] == nil and nilArgs[key] ~= 'h' 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] = 's'
nilArgs[key] = "s"
else
else
metaArgs[key] = tidiedVal
metaArgs[key] = tidiedVal
Line 242: Line 242:
-- must be nil.
-- must be nil.
--]]
--]]
if type(key) == 'string' then
if type(key) == "string" then
key = options.translate[key]
key = options.translate[key]
end
end
Line 258: Line 258:
end
end
end
end
nilArgs[key] = 'h'
nilArgs[key] = "h"
return nil
return nil
end
end
Line 265: Line 265:
-- This function is called when a module tries to add a new value to the
-- This function is called when a module tries to add a new value to the
-- args table, or tries to change an existing value.
-- args table, or tries to change an existing value.
if type(key) == 'string' then
if type(key) == "string" then
key = options.translate[key]
key = options.translate[key]
end
end
if options.readOnly then
if options.readOnly then
error(
error(
'could not write to argument table key "'
"could not write to argument table key \""
.. tostring(key)
.. tostring(key)
.. '"; the table is read-only',
.. "\"; the table is read-only",
2
2
)
)
elseif options.noOverwrite and args[key] ~= nil then
elseif options.noOverwrite and args[key] ~= nil then
error(
error(
'could not write to argument table key "'
"could not write to argument table key \""
.. tostring(key)
.. tostring(key)
.. '"; overwriting existing arguments is not permitted',
.. "\"; overwriting existing arguments is not permitted",
2
2
)
)
Line 291: Line 291:
--]]
--]]
metaArgs[key] = nil
metaArgs[key] = nil
nilArgs[key] = 'h'
nilArgs[key] = "h"
else
else
metaArgs[key] = val
metaArgs[key] = val
Line 302: Line 302:
if k == nil then
if k == nil then
return nil
return nil
elseif type(k) ~= 'string' or not options.backtranslate then
elseif type(k) ~= "string" or not options.backtranslate then
return k, v
return k, v
else
else