Jump to content

Module:InfoboxBasic: Difference between revisions

From Yusupov's House
Created page with "-- 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) -- keep author's casing; just swap underscores for spaces and trim k = k:gsub('_', ' ') k = trim(k) return 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..."
(No difference)

Revision as of 21:52, 1 October 2025

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)
    -- keep author's casing; just swap underscores for spaces and trim
    k = k:gsub('_', ' ')
    k = trim(k)
    return 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 tbl = mw.html.create('table')
        :addClass('infobox')
        :css('float', 'right')
        :css('clear', 'right')
        :css('margin', '0 0 1em 1em')

    -- Title
    tbl:tag('tr')
        :tag('th')
        :attr('colspan','2')
        :addClass('infobox-title')
        :wikitext(title)

    -- 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

    -- Render all other named params, in the order they were supplied
    local names = {}
    if parent and parent.getArgumentNames then
        names = parent:getArgumentNames()  -- preserves input order
    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, class=true, style=true
    }

    for _, key in ipairs(names) do
        -- ignore special keys, empty values, and numeric (positional) params
        if not skip[key] and not tonumber(key) then
            local value = args[key]
            if not isBlank(value) then
                local row = tbl:tag('tr')
                row:tag('th'):attr('scope','row'):wikitext(labelFromKey(key))
                row:tag('td'):wikitext(value)
            end
        end
    end

    return tostring(tbl)
end

return p