No edit summary |
No edit summary |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 29: | Line 29: | ||
function p.getArgs(frame) |
function p.getArgs(frame) |
||
local t |
local t |
||
− | local res |
+ | local res |
− | local next = next |
||
if frame[1] or frame.dbg or frame.name then |
if frame[1] or frame.dbg or frame.name then |
||
t = frame |
t = frame |
||
end |
end |
||
− | if not |
+ | if not t and frame.args then |
for _,v in pairs(frame.args) do if type(v) == 'string' then t = frame.args end break end |
for _,v in pairs(frame.args) do if type(v) == 'string' then t = frame.args end break end |
||
end |
end |
||
− | if not |
+ | if not t and type(frame.getParent) == 'function' and frame:getParent().args then |
for _,v in pairs(frame:getParent().args) do if type(v) == 'string' then t = frame:getParent().args end break end |
for _,v in pairs(frame:getParent().args) do if type(v) == 'string' then t = frame:getParent().args end break end |
||
end |
end |
||
+ | |||
− | |||
if t then |
if t then |
||
+ | res = {} |
||
for k, v in pairs(t) do |
for k, v in pairs(t) do |
||
if k and v then |
if k and v then |
||
Line 65: | Line 65: | ||
end |
end |
||
− | --if next(res) == nil then return nil end |
||
return res |
return res |
||
end |
end |
||
Line 80: | Line 79: | ||
return p |
return p |
||
− | -- [[Category:Modules]] |
Revision as of 19:18, 7 May 2021
local p = {}
---@param t table table to concat
---@return string
---@protected
function p.concat(t, sep)
if type(t) ~= 'table' then
return t or ''
end
local s = ''
for k, v in ipairs(t) do
s = s .. (k ~= 1 and sep or '') .. v
end
return s
end
---@param s string to format
---@return string|nil
function p.format(s, ...)
local ran, val_or_err = pcall(string.format, s, ...)
return ran and val_or_err or nil
end
--- converts input to arguments. returns itself, its arguments, its parent arguments or nil
---@param frame table input with arguments or the mw frame object
---@return table|nil argument list as table or nil
function p.getArgs(frame)
local t
local res
if frame[1] or frame.dbg or frame.name then
t = frame
end
if not t and frame.args then
for _,v in pairs(frame.args) do if type(v) == 'string' then t = frame.args end break end
end
if not t and type(frame.getParent) == 'function' and frame:getParent().args then
for _,v in pairs(frame:getParent().args) do if type(v) == 'string' then t = frame:getParent().args end break end
end
if t then
res = {}
for k, v in pairs(t) do
if k and v then
local key = type(k) == 'string' and k:gsub('%s+', '_') or k
if type(v) == 'boolean' then
res[key] = v
elseif tonumber(v) then
res[key] = tonumber(v)
elseif type(v) == 'string' then
res[key] = mw.ustring.match(v, '^%s*(.*%S)') or ''
local value = res[key]:lower()
if mw.ustring.match(value, '%w') == nil or value == '' or value == 'default' then
res[key] = nil
elseif value == 'true' then
res[key] = true
elseif value == 'false' then
res[key] = false
end
end
end
end
end
return res
end
function p.split(str, sep)
if not str or str == '' then return nil end
local res = {}
for m in mw.text.gsplit(str, sep or '', true) do
res[#res+1] = mw.ustring.match(m, '^%s*(.*%S)') or m
end
if res[#res] == "" then res[#res] = nil end
return #res == 0 and {str} or res
end
return p