Module:InfoboxBasic: Difference between revisions
Appearance
	
	
|  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 edit summary | ||
| Line 1: | Line 1: | ||
| -- Module:InfoboxBasic | -- Module:InfoboxBasic | ||
| local p = {} | local p = {} | ||
| local trim = mw.text.trim | local trim = mw.text.trim | ||
| local function isBlank(v) return v == nil or trim(tostring(v)) == '' end | |||
| local function isBlank(v) | local function labelFromKey(k) k = k:gsub('_',' '); return trim(k) end | ||
| end | |||
| local function labelFromKey(k) | |||
| end | |||
| function p.render(frame) | function p.render(frame) | ||
| Line 21: | Line 11: | ||
|      local title = args.name or args.Name or args.title or args.Title or 'Infobox' |      local title = args.name or args.Name or args.title or args.Title or 'Infobox' | ||
|      local image = args.image or args.Image |      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') |      local tbl = mw.html.create('table') | ||
|          :addClass('infobox') |          :addClass('infobox') | ||
|          :css('float', 'right') |          :css('float','right') | ||
|          :css('clear', 'right') |          :css('clear','right') | ||
|          :css('margin', '0 0 1em 1em') |          :css('margin','0 0 1em 1em') | ||
|      -- Title |      -- Title (centered, 125%, optional custom color via CSS var) | ||
|      tbl:tag('tr') |      local th = tbl:tag('tr') | ||
|          :tag('th') |          :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 |      -- Optional image | ||
| Line 44: | Line 41: | ||
|      end |      end | ||
|      --  |      -- All other params as rows… | ||
|      local names = {} |      local names = {} | ||
|      if parent and parent.getArgumentNames then |      if parent and parent.getArgumentNames then | ||
|          names = parent:getArgumentNames()  |          names = parent:getArgumentNames() | ||
|      else |      else | ||
|          for k,_ in pairs(args) do table.insert(names, k) end |          for k,_ in pairs(args) do table.insert(names,k) end | ||
|          table.sort(names, function(a,b) return tostring(a) < tostring(b) end) |          table.sort(names,function(a,b) return tostring(a)<tostring(b) end) | ||
|      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 | |||
|      for _, key in ipairs(names) do | |||
|          if not skip[key] and not tonumber(key) then |          if not skip[key] and not tonumber(key) then | ||
|              local  |              local v = args[key] | ||
|              if not isBlank( |              if not isBlank(v) then | ||
|                  local row = tbl:tag('tr') |                  local row = tbl:tag('tr') | ||
|                  row:tag('th'):attr('scope','row'):wikitext(labelFromKey(key)) |                  row:tag('th'):attr('scope','row'):wikitext(labelFromKey(key)) | ||
|                  row:tag('td'):wikitext( |                  row:tag('td'):wikitext(v) | ||
|              end |              end | ||
|          end |          end | ||
Revision as of 22:08, 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()
    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
