Module:ProjectBox: Difference between revisions

From Nasqueron Agora
Created page with "local p = {} local function renderRow(label, value) if not value or value == "" then return "" end return string.format( '<tr><th>%s</th><td>%s</td></tr>', label, value ) end function p.render(frame) local args = frame:getParent().args local html = {} table.insert(html, '{| class="infobox project-infobox"') -- Title (project name) if args.project then table.insert(html, string.format('! colspan="2" c..."
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
-- Module:ProjectInfobox
-- Renders a structured infobox for a project page.
-- Usage: {{#invoke:ProjectInfobox|render}}
local p = {}
local p = {}


local function renderRow(label, value)
-- Split a comma-separated string into a trimmed list.
     if not value or value == "" then
local function splitList(value)
         return ""
     local items = {}
    for item in value:gmatch("[^,]+") do
         table.insert(items, mw.text.trim(item))
     end
     end
     return string.format(
     return items
         '<tr><th>%s</th><td>%s</td></tr>',
end
         label, value
 
     )
-- 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
end


Line 14: Line 34:
     local args = frame:getParent().args
     local args = frame:getParent().args


     local html = {}
     local group        = mw.text.trim(args.group        or "")
     table.insert(html, '{| class="infobox project-infobox"')
    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 "")


     -- Title (project name)
    local root  = mw.html.create("table"):addClass("project-infobox")
     if args.project then
    local tbody = root:tag("tbody")
         table.insert(html, string.format('! colspan="2" class="infobox-title" | %s', args.project))
 
     -- 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
     end


     table.insert(html, '|-')
     if status ~= "" then
        tbody:node(row("Status", status))
    end


     -- Rows
     if participants ~= "" then
    table.insert(html, renderRow("Group", args.group))
        tbody:node(row("Participants", participantCell(participants)))
    table.insert(html, renderRow("Participants", args.participants))
    end


     table.insert(html, '|}')
     if description ~= "" then
        tbody:tag("tr")
            :tag("td")
                :attr("colspan", "2")
                :addClass("project-infobox__description")
                :wikitext(description)
    end


     return table.concat(html, '\n')
     return tostring(root)
end
end


return p
return p

Latest revision as of 17:52, 9 April 2026

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