Module:InfoboxBuilder

local infobox = require("Module:Infobox")

local InfoboxBuilder = {}

-- Create a new InfoboxBuilder
function InfoboxBuilder.new ()
	-- store parameters and counters
	local obj = {
		params = {},
		iter_subheader = 1,
		iter_image = 1,
		iter_row = 1
	}

	-- Use InfoboxBuilder:__set as a "fallback" function,
	-- Any call InfoboxBuilder:`x`(`y`) where InfoboxBuilder:`x` does not exist is equivalent to calling InfoboxBuilder:__set(`x`, `y`)
	return setmetatable(obj, {
		__index = function(tbl, key)
			-- First, return any defined InfoboxBuilder:`x`
			local val = InfoboxBuilder[key]
			if val ~= nil then 
				return val
			end

			-- Otherwise, return a wrapper for InfoboxBuilder:__set
			return function ( self, val )
				return self:__set(key, val)
			end
		end 
	})
end

-- Set a parameter by name (for internal use)
function InfoboxBuilder:__set ( key, val )
	self.params[key] = val
	return self
end

-- Special handling for 'italic title' since it has a space
-- 'italicTitle' -> 'italic title'
function InfoboxBuilder:italicTitle ( italicTitle )
	self.params["italic title"] = italicTitle
	return self
end

-- Add a subheader to the infobox
-- Supports being called as :subheader("subheader", "subheaderstyle", "subheaderrowclass") and as
--                          :subheader{subheader="subheader", subheaderstyle="subheaderstyle", subheaderrowclass="subheaderrowclass"}
function InfoboxBuilder:subheader ( subheader, subheaderstyle, subheaderrowclass )
	-- support positional and named arguments
	if type(subheader) == "table" then
		return self:subheader(
			subheader["subheader"],
			subheader["subheaderstyle"],
			subheader["subheaderrowclass"]
		)
	end
	
	self.params["subheader" .. self.iter_subheader] = subheader
	self.params["subheaderstyle" .. self.iter_subheader] = subheaderstyle
	self.params["subheaderrowclass" .. self.iter_subheader] = subheaderrowclass
	self.iter_subheader = self.iter_subheader + 1
	return self
end

-- Add an image to the infobox
-- Supports both positional and named arguments just like :subheader
function InfoboxBuilder:image ( image, caption, imagerowclass )
	-- support positional and named arguments
	if type(image) == "table" then
		return self:image(
			image["image"],
			image["caption"],
			image["imagerowclass"]
		)
	end
	
	self.params["image" .. self.iter_image] = image
	self.params["caption" .. self.iter_image] = caption
	self.params["imagerowclass" .. self.iter_image] = imagerowclass
	self.iter_image = self.iter_image + 1
	return self
end

-- Add a header row to the infobox
-- Supports both positional and named arguments just like :subheader
function InfoboxBuilder:header ( header, rowclass, rowstyle )
	-- support positional and named arguments
	if type(header) == "table" then
		return self:header(
			header["header"],
			header["rowclass"],
			header["rowstyle"]
		)
	end
	
	self.params["header" .. self.iter_row] = header
	self.params["rowclass" .. self.iter_row] = rowclass
	self.params["rowstyle" .. self.iter_row] = rowstyle
	self.iter_row = self.iter_row + 1
	return self
end

-- Add a row to the infobox
-- Supports both positional and named arguments just like :subheader
function InfoboxBuilder:row ( data, label, class, rowclass, rowstyle, rowcellstyle )
	-- support positional and named arguments
	if type(data) == "table" then
		return self:row(
			data["data"],
			data["label"],
			data["class"],
			data["rowclass"],
			data["rowstyle"],
			data["rowcellstyle"]
		)
	end
	
	self.params["data" .. self.iter_row] = data
	self.params["label" .. self.iter_row] = label
	self.params["class" .. self.iter_row] = class
	self.params["rowclass" .. self.iter_row] = rowclass
	self.params["rowstyle" .. self.iter_row] = rowstyle
	self.params["rowcellstyle" .. self.iter_row] = rowcellstyle
	self.iter_row = self.iter_row + 1
	return self
end

-- Build the infobox
-- Equivalent to calling require("Module:Infobox").infobox{ ... } where `...` is all arguments previously provided
function InfoboxBuilder:build ()
	return infobox.infobox(self.params)
end

return {
	InfoboxBuilder = InfoboxBuilder
}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.