Jump to content

📢 Please make sure to read our Style Guide before editing. For questions, suggestions, or technical issues, please contact Noah.

Module:Infobox Player: Difference between revisions

From CrabCraft Wiki
store categories in lua script
Uses parent Infobox module for consistency
Tag: Reverted
Line 1: Line 1:
local infobox = require('Module:Infobox')
local p = {}
local p = {}


local function humanLabel(name)
local function humanLabel(key)
     local s = mw.ustring.gsub(name, "%-", " ")
     key = mw.ustring.gsub(key, "%-", " ")
     return mw.ustring.upper(mw.ustring.sub(s,1,1)) .. mw.ustring.sub(s,2)
     return mw.ustring.upper(mw.ustring.sub(key,1,1)) .. mw.ustring.sub(key,2)
end
end


local sections = {
local sections = {
     ["Basic Information"] = {
     ["Basic Information"] = { "season-joined", "nationality", "discord" },
        "season-joined",
        "nationality",
        "discord",
    },
}
}


function p.infobox(frame)
function p.infobox(frame)
     local args = frame:getParent().args
     local args = frame:getParent().args
    local used = {} -- track which keys we’ve output
    local out = {}


     -- start the table
     local used        = {}
     table.insert(out, '{| class="infobox" style="width:250px;"')
     local outArgs    = { title = args.username }
    -- title row
    local rowCount    = 0
    if args.username and args.username ~= "" then
     local catCount    = 0
        table.insert(out, '|-\n! colspan="2" style="font-size:120%; background:#ccc;" | ' .. args.username)
     end


     -- helper to output a section header
     for sectionName, keys in pairs(sections) do
    local function addHeader(text)
         local hasData = false
         table.insert(out, '|-\n! colspan="2" style="background:#F4BB44;" | ' .. text)
        for _, k in ipairs(keys) do
    end
            if args[k] and args[k] ~= "" then hasData = true; break end
    -- helper to output one row
    local function addRow(key)
        local val = args[key]
        if val and val ~= "" then
            table.insert(out, '|-\n| ' .. humanLabel(key) .. ' || ' .. val)
            used[key] = true
         end
         end
    end
        if hasData then
            catCount = catCount + 1
            outArgs["category" .. catCount] = sectionName


    -- first, emit all fixed sections in order
    for section, keys in pairs(sections) do
        -- check if any of those keys exist
        local has = false
        for _, k in ipairs(keys) do
            if args[k] and args[k] ~= "" then
                has = true; break
            end
        end
        if has then
            addHeader(section)
             for _, k in ipairs(keys) do
             for _, k in ipairs(keys) do
                 addRow(k)
                 if args[k] and args[k] ~= "" then
                    rowCount = rowCount + 1
                    outArgs["label" .. rowCount] = humanLabel(k)
                    outArgs["data"  .. rowCount] = args[k]
                    used[k] = true
                end
             end
             end
         end
         end
     end
     end


    -- gather anything the user passed that’s not in 'sections' or 'username'
     local extraKeys = {}
     local extra = {}
     for k, v in pairs(args) do
     for key, val in pairs(args) do
         if k ~= "username" and v and v ~= "" and not used[k] then
         if key ~= 'username' and val and val ~= '' and not used[key] then
             table.insert(extraKeys, k)
             table.insert(extra, key)
         end
         end
     end
     end
 
     if #extraKeys > 0 then
     if #extra > 0 then
         catCount = catCount + 1
         addHeader("Additional Data")
        outArgs["category" .. catCount] = "Additional Data"
         table.sort(extra)
         table.sort(extraKeys)
         for _, k in ipairs(extra) do
         for _, k in ipairs(extraKeys) do
             addRow(k)
             rowCount = rowCount + 1
            outArgs["label" .. rowCount] = humanLabel(k)
            outArgs["data"  .. rowCount] = args[k]
         end
         end
     end
     end


     -- close up
     outArgs.rows      = rowCount
     table.insert(out, '|}')
     outArgs.categories = catCount
     return table.concat(out, "\n")
 
     return infobox.infobox{
        getParent = function() return { args = outArgs } end
    }
end
end


return p
return p

Revision as of 18:15, 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        = {}
    local outArgs     = { title = args.username }
    local rowCount    = 0
    local catCount    = 0

    for sectionName, keys in pairs(sections) do
        local hasData = false
        for _, k in ipairs(keys) do
            if args[k] and args[k] ~= "" then hasData = true; break end
        end
        if hasData then
            catCount = catCount + 1
            outArgs["category" .. catCount] = sectionName

            for _, k in ipairs(keys) do
                if args[k] and args[k] ~= "" then
                    rowCount = rowCount + 1
                    outArgs["label" .. rowCount] = humanLabel(k)
                    outArgs["data"  .. rowCount] = args[k]
                    used[k] = true
                end
            end
        end
    end

    local extraKeys = {}
    for k, v in pairs(args) do
        if k ~= "username" and v and v ~= "" and not used[k] then
            table.insert(extraKeys, k)
        end
    end
    if #extraKeys > 0 then
        catCount = catCount + 1
        outArgs["category" .. catCount] = "Additional Data"
        table.sort(extraKeys)
        for _, k in ipairs(extraKeys) 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