Jump to content

Module:InfoboxBasic

From Yusupov's House
Revision as of 22:34, 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()
    local args = parent and parent.args or frame.args

    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  -- title BG (light mode only)

    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 background 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')      -- CSS uses this to apply background
        th:css('--ib-title-bg', color)      -- value only used in light mode
    end

    -- Optional image row; give TD a class so CSS can zero the padding and make it 100% wide
    if not isBlank(image) then
        tbl:tag('tr')
            :tag('td')
            :attr('colspan','2')
            :addClass('infobox-image-cell')
            :wikitext('[[File:' .. image .. '|frameless]]')
    end

    -- Render all remaining named parameters in the order supplied
    local names = {}
    if parent and parent.getArgumentNames then
        names = parent:getArgumentNames()
    else
        for k,_ in pairs(args) do table.insert(names,k) end
        table.sort(names, function(a,b) return tostring(a) < tostring(b) end)
    end

    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
    }

    for _, key in ipairs(names) do
        if not skip[key] and not tonumber(key) then
            local v = args[key]
            if not isBlank(v) then
                local row = tbl:tag('tr')
                row:tag('th'):attr('scope','row'):wikitext(labelFromKey(key))
                row:tag('td'):wikitext(v)
            end
        end
    end

    return tostring(tbl)
end

return p