Jump to content
This site is currently in alpha, so you will run into rough edges.

Module:InfoboxMinimal: Difference between revisions

From The Deep Tech Wiki
No edit summary
No edit summary
Line 1: Line 1:
-- Module:InfoboxMinimal (patched)
-- Module:InfoboxMinimal
local p = {}
local p = {}


local function isBlank(s) return not s or mw.text.trim(tostring(s)) == '' end
local function blank(s) return not s or mw.text.trim(tostring(s)) == '' end
local function get(inv, par, key) return (inv and inv[key]) or (par and par[key]) end


local function get(args1, args2, key)
local function addRow(root, label, data)
-- prefer wrapper (#invoke) args, then parent (template) args
if blank(data) then return end
return (args1 and args1[key]) or (args2 and args2[key])
local tr = root:tag('tr')
end
 
local function addRow(tbl, label, data)
if isBlank(data) then return end
local tr = tbl:tag('tr')
tr:tag('th'):attr('scope','row'):addClass('infobox-label'):wikitext(label or '')
tr:tag('th'):attr('scope','row'):addClass('infobox-label'):wikitext(label or '')
tr:tag('td'):addClass('infobox-data'):wikitext(data)
tr:tag('td'):addClass('infobox-data'):wikitext(data)
Line 17: Line 13:


function p.infobox(frame)
function p.infobox(frame)
local invokeArgs  = frame.args                       -- from {{#invoke:...| key = value }}
local inv = frame.args                                 -- args set in #invoke (wrapper template)
local parentArgs  = frame:getParent() and frame:getParent().args or {} -- from {{Infobox_Startup|...}}
local par = frame:getParent() and frame:getParent().args or {} -- args from the calling page


local classes = 'infobox'
local classes = 'infobox'
local bodyclass = get(invokeArgs, parentArgs, 'bodyclass')
local bodyclass = get(inv, par, 'bodyclass')
if not isBlank(bodyclass) then classes = classes .. ' ' .. bodyclass end
if not blank(bodyclass) then classes = classes .. ' ' .. bodyclass end


local root = mw.html.create('table'):addClass(classes):attr('role','presentation')
local root = mw.html.create('table'):addClass(classes):attr('role','presentation')


local title = get(invokeArgs, parentArgs, 'title') or
local title = get(inv, par, 'title') or get(inv, par, 'name') or mw.title.getCurrentTitle().baseText
              get(invokeArgs, parentArgs, 'name') or
              mw.title.getCurrentTitle().baseText
root:tag('caption'):addClass('infobox-title'):wikitext(title)
root:tag('caption'):addClass('infobox-title'):wikitext(title)


-- scan generously; stops naturally on blank data
for i = 1, 100 do
for i = 1, 100 do
local label = get(invokeArgs, parentArgs, 'label'..i)
addRow(root, get(inv, par, 'label'..i), get(inv, par, 'data'..i))
local data  = get(invokeArgs, parentArgs, 'data' ..i)
addRow(root, label, data)
end
end



Revision as of 06:25, 9 November 2025

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

-- Module:InfoboxMinimal
local p = {}

local function blank(s) return not s or mw.text.trim(tostring(s)) == '' end
local function get(inv, par, key) return (inv and inv[key]) or (par and par[key]) end

local function addRow(root, label, data)
	if blank(data) then return end
	local tr = root:tag('tr')
	tr:tag('th'):attr('scope','row'):addClass('infobox-label'):wikitext(label or '')
	tr:tag('td'):addClass('infobox-data'):wikitext(data)
end

function p.infobox(frame)
	local inv = frame.args                                 -- args set in #invoke (wrapper template)
	local par = frame:getParent() and frame:getParent().args or {} -- args from the calling page

	local classes = 'infobox'
	local bodyclass = get(inv, par, 'bodyclass')
	if not blank(bodyclass) then classes = classes .. ' ' .. bodyclass end

	local root = mw.html.create('table'):addClass(classes):attr('role','presentation')

	local title = get(inv, par, 'title') or get(inv, par, 'name') or mw.title.getCurrentTitle().baseText
	root:tag('caption'):addClass('infobox-title'):wikitext(title)

	-- scan generously; stops naturally on blank data
	for i = 1, 100 do
		addRow(root, get(inv, par, 'label'..i), get(inv, par, 'data'..i))
	end

	return tostring(root)
end

return p