Module:ProjectBox

From Nasqueron Agora

Documentation for this module may be created at Module:ProjectBox/doc

-- Module:ProjectInfobox
-- Renders a structured infobox for a project page.
-- Usage: {{#invoke:ProjectInfobox|render}}

local p = {}

-- Split a comma-separated string into a trimmed list.
local function splitList(value)
    local items = {}
    for item in value:gmatch("[^,]+") do
        table.insert(items, mw.text.trim(item))
    end
    return items
end

-- Wrap a value in a table cell pair (label / value).
local function row(label, value)
    return mw.html.create("tr")
        :tag("th"):addClass("project-infobox__label"):wikitext(label):done()
        :tag("td"):addClass("project-infobox__value"):wikitext(value):done()
end

-- Render participants as a comma-separated inline list.
local function participantCell(raw)
    local names = splitList(raw)
    local linked = {}
    for _, name in ipairs(names) do
        table.insert(linked, "[[User:" .. name .. "|" .. name .. "]]")
    end
    return table.concat(linked, ", ")
end

function p.render(frame)
    local args = frame:getParent().args

    local group        = mw.text.trim(args.group        or "")
    local project      = mw.text.trim(args.project      or "")
    local participants = mw.text.trim(args.participants or "")
    local status       = mw.text.trim(args.status      or "")
    local description  = mw.text.trim(args.description or "")

    local root  = mw.html.create("table"):addClass("project-infobox")
    local tbody = root:tag("tbody")

    -- Header row: project name as caption
    root:tag("caption")
        :addClass("project-infobox__title")
        :wikitext(project ~= "" and project or "Project")

    if group ~= "" then
        tbody:node(row("Group", group))
    end

    if project ~= "" then
        tbody:node(row("Project", project))
    end

    if status ~= "" then
        tbody:node(row("Status", status))
    end

    if participants ~= "" then
        tbody:node(row("Participants", participantCell(participants)))
    end

    if description ~= "" then
        tbody:tag("tr")
            :tag("td")
                :attr("colspan", "2")
                :addClass("project-infobox__description")
                :wikitext(description)
    end

    return tostring(root)
end

return p