Jump to content

Module:InfoboxBasic: Difference between revisions

From Yusupov's House
No edit summary
No edit summary
Line 1: Line 1:
-- Module:InfoboxBasic
-- Keys to skip (already handled)
local p = {}
local skip = {
local trim = mw.text.trim
  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 isBlank(v) return v == nil or trim(tostring(v)) == '' end
local function addRow(label, value)
local function labelFromKey(k) k = k:gsub('_',' '); return trim(k) end
   local row = tbl:tag('tr')
 
  row:tag('th'):attr('scope','row'):wikitext(labelFromKey(label))
function p.render(frame)
  row:tag('td'):wikitext(value)
    local parent = frame:getParent() or frame
end
    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
-- Preserve the editor’s order:
    if not isBlank(image) then
if parent and parent.argumentPairs then
        tbl:tag('tr')
  -- argumentPairs returns last→first on many installs; collect then reverse
            :tag('td')
  local seq = {}
            :attr('colspan','2')
  for key, value in parent:argumentPairs() do
            :addClass('infobox-image-cell')
    if not skip[key] and not tonumber(key) and not isBlank(value) then
            :wikitext('[[File:' .. image .. '|frameless]]')
      seq[#seq+1] = {key=key, value=value}
     end
     end
 
  end
    -- Keys to skip (special handling above)
  for i = #seq, 1, -1 do
    local skip = {
     addRow(seq[i].key, seq[i].value)
        name=true, Name=true, title=true, Title=true,
  end
        image=true, Image=true,
elseif parent and parent.getArgumentNames then
        color=true, Color=true, colour=true, Colour=true,
  -- Fallback: MediaWiki provides names in source order
        class=true, style=true
  for _, key in ipairs(parent:getArgumentNames()) do
     }
    local value = args[key]
 
    if not skip[key] and not tonumber(key) and not isBlank(value) then
    local function addRow(label, value)
      addRow(key, value)
        local row = tbl:tag('tr')
        row:tag('th'):attr('scope','row'):wikitext(labelFromKey(label))
        row:tag('td'):wikitext(value)
     end
     end
 
  end
    -- Iterate in the *editor's order*
else
    if parent.argumentPairs then
  -- Last-resort fallback (order may not be exact on very old setups)
        for key, value in parent:argumentPairs() do
  local names = {}
            if not skip[key] and not tonumber(key) and not isBlank(value) then
  for k,_ in pairs(args or {}) do names[#names+1] = k end
                addRow(key, value)
  for _, key in ipairs(names) do
            end
    local v = args[key]
        end
    if not skip[key] and not tonumber(key) and not isBlank(v) then
    else
      addRow(key, v)
        -- 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
     end
 
  end
    return tostring(tbl)
end
end
return p

Revision as of 22:53, 1 October 2025

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

-- Keys to skip (already handled)
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

-- Preserve the editor’s order:
if parent and parent.argumentPairs then
  -- argumentPairs returns last→first on many installs; collect then reverse
  local seq = {}
  for key, value in parent:argumentPairs() do
    if not skip[key] and not tonumber(key) and not isBlank(value) then
      seq[#seq+1] = {key=key, value=value}
    end
  end
  for i = #seq, 1, -1 do
    addRow(seq[i].key, seq[i].value)
  end
elseif parent and parent.getArgumentNames then
  -- Fallback: MediaWiki provides names in source order
  for _, key in ipairs(parent:getArgumentNames()) do
    local value = args[key]
    if not skip[key] and not tonumber(key) and not isBlank(value) then
      addRow(key, value)
    end
  end
else
  -- Last-resort fallback (order may not be exact on very old setups)
  local names = {}
  for k,_ in pairs(args or {}) do names[#names+1] = k end
  for _, key in ipairs(names) do
    local v = args[key]
    if not skip[key] and not tonumber(key) and not isBlank(v) then
      addRow(key, v)
    end
  end
end