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 type(frame) ~= 'table' then return frame end
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 value == 'true' then
res[key] = true
elseif value == 'false' then
res[key] = false
elseif value == '' or value == 'default' or mw.ustring.match(value, '%S') == nil then
res[key] = nil
end
end
end
end
end
return res
end
-- Generate actual table from read-only tables returned from mw.loadData
-- Use this only on sub-values of the data table, not the whole table itself (use require instead)
function p.mwDataToTable(t)
if type(t) ~= 'table' then return {t} end
local res = {}
for k,v in pairs(t) do
if type(v) == 'table' then
res[k] = p.mwDataToTable(v)
else
res[k] = v
end
end
return res
end
-- calculate length of array-like tables
-- intended for use on mw.loadData tables
-- returns 0 on non-table values
function p.length(value)
local l = 0
if type(value) == 'table' then
for k in ipairs(value) do l = k end
end
return l
end
function p.orderedList(t)
local keys={}
local n=0
for k,_ in pairs(t) do
n=n+1
keys[n]=k
end
table.sort(
keys,
function(a, b)
return a:lower() < b:lower()
end
)
return keys
end
-- Template:Progression for lua modules
function p.p(frame) return p.progression(frame) end
function p.progression(frame)
local fd = require('Module:fd')
local args = p.getArgs(frame)
local res = {}
if args.tooltip or args.texttip then
local function tt(v, t)
return p.format('<span style="cursor:help; border-bottom:1px dotted;" title="%s">%s</span>', t, v)
end
local i,j = 1,2
while args[i] and args[j] do
res[#res+1] = tt(fd.get{args[i]}, args[j])
i = i+2
j = j+2
end
else
for _,v in ipairs(args) do
res[#res+1] = v
end
end
return p.concat(res, ' / ')
end
-- Split string into table of substrings
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
Advertisement
Module:Shared
Advertisement