Module:Sandbox/Toohool: Difference between revisions
Appearance
Content deleted Content added
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local Redirect = require('Module:Redirect') |
|||
local p = {} |
local p = {} |
||
local function trim(s) |
|||
⚫ | |||
return mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1") |
|||
⚫ | |||
end |
|||
⚫ | |||
local childFrame = {args = {key}, getParent = function() return nil; end} |
|||
function p.makeWrapper(func, opts) |
|||
return Redirect.main(childFrame) |
|||
opts = opts or {} |
|||
local nullableArgNames = {} |
|||
for i, v in ipairs(opts.nullable or {}) do |
|||
nullableArgNames[v] = true |
|||
end |
|||
local noTrimArgNames = {} |
|||
for i, v in ipairs(opts.notrim or opts.noTrim or {}) do |
|||
noTrimArgNames[v] = true |
|||
end |
|||
⚫ | |||
local origArgs |
|||
if frame == mw.getCurrentFrame() then |
|||
-- We're being called via #invoke. If the invoking template passed any args, use |
|||
-- them. Otherwise, use the args that were passed into the template. |
|||
origArgs = frame:getParent().args |
|||
for k, v in pairs(frame.args) do |
|||
origArgs = frame.args |
|||
break |
|||
end |
|||
else |
|||
-- We're being called from another module or from the debug console, so assume |
|||
-- the args are passed in directly. |
|||
origArgs = frame |
|||
end |
end |
||
local args = {} |
|||
⚫ | |||
-- ParserFunctions considers the empty string to be false, so to preserve the previous |
|||
-- behavior of the template, change any empty arguments to nil, so Lua will consider |
|||
-- them false too. |
|||
⚫ | |||
local v = origArgs[k] |
|||
if v and not noTrimArgNames[k] then |
|||
v = trim(v) |
|||
end |
|||
if v ~= '' or nullableArgNames[k] then |
|||
return v |
|||
else |
|||
return nil |
|||
end |
|||
end, |
|||
__pairs = function(t) |
|||
local nextFunc = function(tbl, k) |
|||
local v |
|||
repeat |
|||
k = next(origArgs, k) |
|||
v = args[k] |
|||
until (not k) or v |
|||
return k, v |
|||
end |
|||
return nextFunc, t, nil |
|||
end, |
|||
__ipairs = function(t) |
|||
-- TODO |
|||
end |
|||
}) |
|||
return func(args) |
|||
end |
end |
||
end |
|||
}) |
|||
return p |
return p |
Latest revision as of 07:17, 2 April 2013
local p = {}
local function trim(s)
return mw.ustring.gsub(s, "^%s*(.-)%s*$", "%1")
end
function p.makeWrapper(func, opts)
opts = opts or {}
local nullableArgNames = {}
for i, v in ipairs(opts.nullable or {}) do
nullableArgNames[v] = true
end
local noTrimArgNames = {}
for i, v in ipairs(opts.notrim or opts.noTrim or {}) do
noTrimArgNames[v] = true
end
return function(frame)
local origArgs
if frame == mw.getCurrentFrame() then
-- We're being called via #invoke. If the invoking template passed any args, use
-- them. Otherwise, use the args that were passed into the template.
origArgs = frame:getParent().args
for k, v in pairs(frame.args) do
origArgs = frame.args
break
end
else
-- We're being called from another module or from the debug console, so assume
-- the args are passed in directly.
origArgs = frame
end
local args = {}
setmetatable(args, {
-- ParserFunctions considers the empty string to be false, so to preserve the previous
-- behavior of the template, change any empty arguments to nil, so Lua will consider
-- them false too.
__index = function(t, k)
local v = origArgs[k]
if v and not noTrimArgNames[k] then
v = trim(v)
end
if v ~= '' or nullableArgNames[k] then
return v
else
return nil
end
end,
__pairs = function(t)
local nextFunc = function(tbl, k)
local v
repeat
k = next(origArgs, k)
v = args[k]
until (not k) or v
return k, v
end
return nextFunc, t, nil
end,
__ipairs = function(t)
-- TODO
end
})
return func(args)
end
end
return p