Module:Infocard

From Halopedia, the Halo wiki

Documentation for this module may be created at ModuleDoc:Infocard

local infocard = {}

function infocard.new()
	-- Create a new, blank infocard
	local result = { [ 'data' ] = {}, [ 'title' ] = nil }
	
	-- Register withTitle function
	function result.withTitle( title )
		result[ 'title' ] = tostring( title )
		return result
	end
	
	-- Register withField function
	function result.withField( key, value )
		table.insert( result[ 'data' ], {
			[ 'key' ] = tostring( key ),
			[ 'value' ] = tostring( value )
		} )
		return result
	end
	
	-- Register render function
	function result.render()
		local output = '{| class="wikitable infocard"\n'
		if result[ 'title' ] ~= nil and result[ 'title' ] ~= '' then
			output = output .. '! colspan=2 | ' .. result[ 'title' ] .. '\n'
			output = output .. '|-\n'
		end
		for i, v in ipairs( result[ 'data' ] ) do
			output = output .. '! ' .. v[ 'key' ] .. '\n'
			output = output .. '| ' .. v[ 'value' ] .. '\n'
			output = output .. '|-\n'
		end
		output = output .. '|}'
		return output
	end
	
	-- Return infocard
	return result
end

return infocard