Jump to content

Module:Infobox

From Logic World Wiki
Revision as of 01:06, 7 September 2025 by Felipe (talk | contribs)

local capiunto = require 'capiunto'

local p = {}

-- TODO: Move to shared module
function unflattenArgs(flatArgs)
	local args = {}

	for k, v in pairs(flatArgs) do
	  local parent = args
	
	  for token in string.gmatch(k, "[^.]+%.") do
	    local key = string.sub(token, 1, #token - 1)
	    if parent[key] == nil then
	      parent[key] = {}
	    end
	    parent = parent[key]
	  end
	
	  local name = string.match(k, "[^.]+$")
	  parent[name] = v
	end
	
	return args
end

function p.component(frame, args)
	local args = unflattenArgs(args or frame:getParent().args)
	local headerStyle
	if args.headerstyle and args.headerstyle ~= '' then
		headerStyle = string.format('background-color:%s;', args.headerstyle)
	else
		headerStyle = 'background-color:grey;'
	end
	local retval = capiunto.create{
		title = args.title,
		headerStyle = headerStyle,
		bodyClass = "lw-infobox"
	}
	:addImage(args.image or string.format("[[File:%s_thumbnail_render.png|200px]]", args.id), args.caption)
	
	:addHeader("Component info")
	:addRow("Internal ID", "<code>"..args.id.."</code>")

	if args.io ~= nil then
		io = args.io

		retval:addHeader("Input and output")
	
		if io.minInputs ~= io.maxInputs then
			retval:addRow("Input count", string.format("Adjustable from %i to %i", io.minInputs, io.maxInputs))
		else
			retval:addRow("Input count", io.minInputs)
		end
		
		retval:addRow("Output count", io.outputs)
	
		if io.propagationDelay ~= nil then
			local ticksStr = string.format("%i tick", io.propagationDelay)
			if tonumber(io.propagationDelay) ~= 1 then
				ticksStr = ticksStr.."s"
			end
			retval:addRow("Propagation delay", ticksStr)	
		end
	end

	return retval
end

return p