Jump to content

Module:InfoboxBasic

From Yusupov's House
Revision as of 22:51, 1 October 2025 by Mvuijlst (talk | contribs)

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

-- Module:InfoboxBasic
local p = {}
local trim = mw.text.trim

local function isBlank(v) return v == nil or trim(tostring(v)) == '' end
local function labelFromKey(k) k = k:gsub('_',' '); return trim(k) end

function p.render(frame)
    local parent = frame:getParent() or frame
    local args   = parent.args or {}   -- never nil

    local title = args.name or args.Name or args.title or args.Title or 'Infobox'
    local image = args.image or args.Image
    local color = args.color or args.Color or args.colour or args.Colour

    local tbl = mw.html.create('table')
        :addClass('infobox')
        :css('float','right')
        :css('clear','right')
        :css('margin','0 0 1em 1em')

    -- Title (centered, 125%); only add BG class if |color= was set
    local th = tbl:tag('tr'):tag('th')
        :attr('colspan','2')
        :addClass('infobox-title')
        :css('text-align','center')
        :css('font-size','125%')
        :wikitext(title)

    if not isBlank(color) then
        th:addClass('has-title-color')
        th:css('--ib-title-bg', color)
    end

    -- Optional image row
    if not isBlank(image) then
        tbl:tag('tr')
            :tag('td')
            :attr('colspan','2')
            :addClass('infobox-image-cell')
            :wikitext('[[File:' .. image .. '|frameless]]')
    end

    -- Keys to skip (special handling above)
    local skip = {
        name=true, Name=true, title=true, Title=true,
        image=true, Image=true,
        color=true, Color=true, colour=true, Colour=true,
        class=true, style=true
    }

    local function addRow(label, value)
        local row = tbl:tag('tr')
        row:tag('th'):attr('scope','row'):wikitext(labelFromKey(label))
        row:tag('td'):wikitext(value)
    end

    -- Iterate in the *editor's order*
    if parent.argumentPairs then
        for key, value in parent:argumentPairs() do
            if not skip[key] and not tonumber(key) and not isBlank(value) then
                addRow(key, value)
            end
        end
    else
        -- Fallback for very old Scribunto: use getArgumentNames() if available
        local names = (parent.getArgumentNames and parent:getArgumentNames()) or {}
        for _, key in ipairs(names) do
            local value = args[key]
            if not skip[key] and not tonumber(key) and not isBlank(value) then
                addRow(key, value)
            end
        end
    end

    return tostring(tbl)
end

return p