Module:Infobox Player: Difference between revisions
Appearance
Uses parent Infobox module for consistency Tag: Reverted |
fixes categories appearing above Tag: Reverted |
||
| Line 4: | Line 4: | ||
local function humanLabel(key) | local function humanLabel(key) | ||
key = mw.ustring.gsub(key, "%-", " ") | key = mw.ustring.gsub(key, "%-", " ") | ||
return mw.ustring.upper(mw.ustring.sub(key,1,1)) .. mw.ustring.sub(key,2) | return mw.ustring.upper(mw.ustring.sub(key,1,1)) | ||
.. mw.ustring.sub(key,2) | |||
end | end | ||
| Line 12: | Line 13: | ||
function p.infobox(frame) | function p.infobox(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
local used = {} -- mark which keys we've output | |||
local used | local outArgs = { title = args.username } | ||
local outArgs | local rowCount = 0 | ||
local rowCount | local catCount = 0 | ||
local catCount | |||
for sectionName, keys in pairs(sections) do | for sectionName, keys in pairs(sections) do | ||
local | local has = false | ||
for _, k in ipairs(keys) do | for _, k in ipairs(keys) do | ||
if args[k] and args[k] ~= "" then | if args[k] and args[k] ~= "" then has = true; break end | ||
end | end | ||
if | if has then | ||
catCount = catCount + 1 | catCount = catCount + 1 | ||
outArgs["category" .. catCount] = sectionName | outArgs["category" .. catCount] = sectionName | ||
for _, k in ipairs(keys) do | for _, k in ipairs(keys) do | ||
local v = args[k] | |||
if v and v ~= "" then | |||
rowCount = rowCount + 1 | rowCount = rowCount + 1 | ||
outArgs["label" .. rowCount] = humanLabel(k) | outArgs["label" .. rowCount] = humanLabel(k) | ||
outArgs["data" .. rowCount] = | outArgs["data" .. rowCount] = v | ||
used[k] = true | used[k] = true | ||
end | end | ||
| Line 39: | Line 40: | ||
end | end | ||
local | -- gather any truly “extra” params | ||
local extra = {} | |||
for k, v in pairs(args) do | for k, v in pairs(args) do | ||
if k ~= "username" and v and v ~= "" | if k ~= "username" | ||
table.insert( | and not used[k] | ||
and not mw.ustring.match(k, "^category") | |||
and v and v ~= "" | |||
then | |||
table.insert(extra, k) | |||
end | end | ||
end | end | ||
if # | |||
if #extra > 0 then | |||
catCount = catCount + 1 | catCount = catCount + 1 | ||
outArgs["category" .. catCount] = "Additional Data" | outArgs["category" .. catCount] = "Additional Data" | ||
table.sort( | table.sort(extra) | ||
for _, k in ipairs( | for _, k in ipairs(extra) do | ||
rowCount = rowCount + 1 | rowCount = rowCount + 1 | ||
outArgs["label" .. rowCount] = humanLabel(k) | outArgs["label" .. rowCount] = humanLabel(k) | ||
Revision as of 18:17, 21 June 2025
Documentation for this module may be created at Module:Infobox Player/doc
local infobox = require('Module:Infobox')
local p = {}
local function humanLabel(key)
key = mw.ustring.gsub(key, "%-", " ")
return mw.ustring.upper(mw.ustring.sub(key,1,1))
.. mw.ustring.sub(key,2)
end
local sections = {
["Basic Information"] = { "season-joined", "nationality", "discord" },
}
function p.infobox(frame)
local args = frame:getParent().args
local used = {} -- mark which keys we've output
local outArgs = { title = args.username }
local rowCount = 0
local catCount = 0
for sectionName, keys in pairs(sections) do
local has = false
for _, k in ipairs(keys) do
if args[k] and args[k] ~= "" then has = true; break end
end
if has then
catCount = catCount + 1
outArgs["category" .. catCount] = sectionName
for _, k in ipairs(keys) do
local v = args[k]
if v and v ~= "" then
rowCount = rowCount + 1
outArgs["label" .. rowCount] = humanLabel(k)
outArgs["data" .. rowCount] = v
used[k] = true
end
end
end
end
-- gather any truly “extra” params
local extra = {}
for k, v in pairs(args) do
if k ~= "username"
and not used[k]
and not mw.ustring.match(k, "^category")
and v and v ~= ""
then
table.insert(extra, k)
end
end
if #extra > 0 then
catCount = catCount + 1
outArgs["category" .. catCount] = "Additional Data"
table.sort(extra)
for _, k in ipairs(extra) do
rowCount = rowCount + 1
outArgs["label" .. rowCount] = humanLabel(k)
outArgs["data" .. rowCount] = args[k]
end
end
outArgs.rows = rowCount
outArgs.categories = catCount
return infobox.infobox{
getParent = function() return { args = outArgs } end
}
end
return p