Jump to content

Module:InfoboxBasic

From Yusupov's House
Revision as of 22:08, 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  -- NEW

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

    -- Title (centered, 125%, optional custom color via CSS var)
    local th = tbl:tag('tr')
        :tag('th')
            :attr('colspan','2')
            :addClass('infobox-title')
            :css('text-align','center')   -- ensure centering
            :css('font-size','125%')      -- ensure size
            :wikitext(title)

    if not isBlank(color) then
        th:css('--ib-title-bg', color)    -- only used in light mode CSS
    end

    -- Optional image
    if not isBlank(image) then
        tbl:tag('tr')
            :tag('td')
            :attr('colspan','2')
            :css('text-align','center')
            :wikitext('[[File:' .. image .. '|frameless|250px]]')
    end

    -- All other params as rows…
    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