Jump to content

📢 Please make sure to read our Style Guide before editing. For questions, suggestions, or technical issues, please contact Noah.

Module:Infobox: Difference between revisions

From CrabCraft Wiki
Added logic to remove unused sections
adjusted component variable names
Line 6: Line 6:
local has_fields = false
local has_fields = false
for i, component in ipairs(components) do
for i, comp in ipairs(components) do
if component[0] == 's' then
if comp[0] == 's' then
if has_fields then
if has_fields then
for _, c in ipairs(buffer) do table.insert(out, c) end
for _, c in ipairs(buffer) do table.insert(out, c) end
Line 14: Line 14:
has_fields = false
has_fields = false


elseif component[1] == 'f' then
elseif comp[1] == 'f' then
if compmonent[3] and component[3] ~= '' then
if component[3] and comp[3] ~= '' then
has_fields = true
has_fields = true
table.insert(buffer, component)
table.insert(buffer, comp)
end
end
elseif component[1] == 't' or component[1] == 'i' then
elseif comp[1] == 't' or comp[1] == 'i' then
table.insert(out, component)
table.insert(out, comp)
end
end
end
end
Line 68: Line 68:
end
end
for i, component in ipairs(args) do
for i, comp in ipairs(args) do
if component[1] == 't' then add_title(component[2])
if comp[1] == 't' then add_title(comp[2])
elseif component[1] == 's' then add_section(component[2])
elseif comp[1] == 's' then add_section(comp[2])
elseif component[1] == 'f' then add_field(component[2], component[3])
elseif comp[1] == 'f' then add_field(comp[2], comp[3])
elseif component[1] == 'i' then add_image(component[2])
elseif comp[1] == 'i' then add_image(comp[2])
end
end
end
end

Revision as of 22:16, 21 June 2025

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

local p = {}

local function filter_sections(components)
	local out = {}
	local current_section = {}
	local has_fields = false
	
	for i, comp in ipairs(components) do
		if comp[0] == 's' then
			if has_fields then
				for _, c in ipairs(buffer) do table.insert(out, c) end
			end
			buffer = { comp }
			has_fields = false

		elseif comp[1] == 'f' then
			if component[3] and comp[3] ~= '' then
				has_fields = true
				table.insert(buffer, comp)
			end
			
		elseif comp[1] == 't' or comp[1] == 'i' then
			table.insert(out, comp)
		end
	end
	
	return out
end

function p.infobox(frame)
	local args = frame:getParent().args
	args = filter_sections(args)
	local out = {}

	table.insert(out,
	  '{| class="infobox"'
	    .. ' style="'
	      .. 'width:300px; '
	      .. 'float:right; '
	      .. 'margin:0 0 0.5em 0.5em; '
	      .. 'border:1px solid #aaa; '
	      .. 'border-collapse:separate; '
	      .. 'border-spacing:0.5em;'
	    .. '"'
	)
    
	local function add_title(data)
		if not data then return end
		table.insert(out, '|-\n! colspan="2" style="font-size:120%; background:#F4BB44;" | ' .. data)
	end
	
	local function add_section(data)
		if not data then return end
		table.insert(out, '|-\n! colspan="2" style="background: #F4BB44;" | ' .. data)
	end

	local function add_field(label, data)
		if not data then return end
	    local left  = 'style="width:50%; text-align:left; font-weight:bold; vertical-align:top;" | ' .. label
	    local right = 'style="width:50%; text-align:left; vertical-align:top;"       | ' .. data
	    table.insert(out, '|-\n|'..left..'||'..right)
	end

	
	local function add_image(data)
		if not data then return end
		table.insert(out, '|-\n| colspan="2" style="text-align:center;" | ' .. data)
	end
	
	for i, comp in ipairs(args) do
		if comp[1] == 't' then add_title(comp[2])
		elseif comp[1] == 's' then add_section(comp[2])
		elseif comp[1] == 'f' then add_field(comp[2], comp[3])
		elseif comp[1] == 'i' then add_image(comp[2])
		end
	end

	table.insert(out, '|}')

	return table.concat(out, '\n')
end

return p