Jump to content

Module:InfoboxBasic: Difference between revisions

From Yusupov's House
No edit summary
No edit summary
Line 1: Line 1:
-- Render all remaining named params in the order they were supplied
-- Module:InfoboxBasic
local skip = {
local p = {}
    name=true, Name=true, title=true, Title=true,
local trim = mw.text.trim
    image=true, Image=true,
    color=true, Color=true, colour=true, Colour=true,
    class=true, style=true
}


local function addRow(label, value)
local function isBlank(v) return v == nil or trim(tostring(v)) == '' end
    local row = tbl:tag('tr')
local function labelFromKey(k) k = k:gsub('_',' '); return trim(k) end
    row:tag('th'):attr('scope','row'):wikitext(labelFromKey(label))
 
    row:tag('td'):wikitext(value)
function p.render(frame)
end
    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


if parent and parent.getArgumentPairs then
     -- Iterate in the *editor's order*
     -- This preserves the author’s order
     if parent.argumentPairs then
     for key, value in parent:getArgumentPairs() do
        for key, value in parent:argumentPairs() do
        if not skip[key] and not tonumber(key) and not isBlank(value) then
            if not skip[key] and not tonumber(key) and not isBlank(value) then
            addRow(key, value)
                addRow(key, value)
            end
         end
         end
     end
     else
else
        -- Fallback for very old Scribunto: use getArgumentNames() if available
    -- Fallback (older MW): order may not be preserved
        local names = (parent.getArgumentNames and parent:getArgumentNames()) or {}
    local names = {}
        for _, key in ipairs(names) do
    for k,_ in pairs(args) do table.insert(names, k) end
            local value = args[key]
    -- do NOT sort; keep whatever order Lua gives us
            if not skip[key] and not tonumber(key) and not isBlank(value) then
    for _, key in ipairs(names) do
                addRow(key, value)
        if not skip[key] and not tonumber(key) then
            end
            local v = args[key]
            if not isBlank(v) then addRow(key, v) end
         end
         end
     end
     end
    return tostring(tbl)
end
end
return p

Revision as of 22:51, 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) 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