Module:Sandbox/ChaoticShadow/InfoboxBuilder: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 117: Line 117:


--- Sets both the text and background color of the header
--- Sets both the text and background color of the header
-- @param param string
-- @param param_name string
-- Parameter name that helps map the colors
-- Parameter name that helps map the colors
-- @param color_table { text, bg }
-- @param color_table { text, bg }
-- text string
-- text string
Line 126: Line 126:
-- @return self
-- @return self
-- The current object
-- The current object
function InfoboxBuilder:setHeaderColorsByParam(param, color_table)
function InfoboxBuilder:setHeaderColorsByParam(param_name, color_table)
if param == nil then
if param_name == nil then
error("Parameter name must not be nil")
error("Parameter name must not be nil")
elseif color_table == nil then
elseif color_table == nil then
Line 133: Line 133:
end
end
local raw_param_value = self.raw_args[param]
local raw_param_value = self.raw_args[param_name]
local colors = color_table[raw_param_value]
local colors = color_table[raw_param_value]
Line 202: Line 202:


--- Gets the content associated with a parameter
--- Gets the content associated with a parameter
-- @param param string
-- @param param_name string
-- The param name, not nil or empty
-- The param name, not nil or empty
-- @return content string
-- @return content string
-- A string containing the content
-- A string containing the content
function InfoboxBuilder:getContent(param)
function InfoboxBuilder:getContent(param_name)
if param == nil or param == "" then
if param_name == nil or param_name == "" then
error("Param must not be nil or empty")
error("Param must not be nil or empty")
end
end
if self.proc_args[param] then
if self.proc_args[param_name] then
return self.proc_args[param]
return self.proc_args[param_name]
end
end
local content = nil
local content = nil
local current_param = self.params[param]
local current_param = self.params[param_name]
if current_param == nil then
if current_param == nil then
error(string.format("No such param: %s", param))
error(string.format("No such param with name: %s", param_name))
end
end
local raw_param_value = self.raw_args[param] or current_param.default
local raw_param_value = self.raw_args[param_name] or current_param.default
if raw_param_value == nil then
if raw_param_value == nil then
Line 239: Line 239:
return content
return content
end

--- Gets the non-nil content from a list of param names
-- @param param_names { param_name, ... }
-- param_name string
-- The param name
-- @return actual_values { content, ... }
-- content string
-- The processed content associated with a param
function InfoboxBuilder:getNonNilContent(param_names)
local actual_values = {}
for i,v in ipairs(param_names) do
table.insert(actual_values, self:getContent(v))
end
return actual_values
end
end


--- Adds a header
--- Adds a header
-- @param arg { content, attr, colspan, rowspan, css }
-- @param arg { content, attr, colspan, rowspan, css }
-- content string or nil The wikitext to be rendered
-- content string or nil
-- attr {...} or nil The attributes of the cell in table form
-- The wikitext to be rendered
-- colspan number or nil The colspan of the cell
-- attr {...} or nil
-- roswpan number or nil The rowspan of the cell
-- The attributes of the cell in table form
-- css {...} or nil The css of the cell in table form
-- colspan number or nil
-- The colspan of the cell
-- roswpan number or nil
-- The rowspan of the cell
-- css {...} or nil
-- The css of the cell in table form
-- @return self
-- @return self
-- The current object
-- The current object
Line 286: Line 307:
--- Adds an image, or switchable images
--- Adds an image, or switchable images
-- @param ... { { tag, content, title }, ... }
-- @param ... { { tag, content, title }, ... }
-- tag "artd" or "td" Whether or not an it is based off a parameter
-- tag "artd" or "td"
-- content string The content or the parameter name
-- Whether or not an it is based off a parameter
-- title string or nil The title, if using switchable images
-- content string
-- The content or the parameter name
-- title string or nil
-- The title, if using switchable images
-- @return self
-- @return self
-- The current object
-- The current object
Line 332: Line 356:


--- Adds a row, with columns up to 30 columns spanned
--- Adds a row, with columns up to 30 columns spanned
-- @param should_hide boolean
-- The row will be hidden if all varying columns are nil
-- @param cols { { tag, content, hide, attr, colspan, rowspan, css }, ... }
-- @param cols { { tag, content, hide, attr, colspan, rowspan, css }, ... }
-- tag "th", "td", "argth", "argtd"
-- tag "th", "td", "argth", "argtd"
-- A string containing one of the above, "th" or
-- A string containing one of the above, "th" or "td" uses
-- "td" uses content as the wikitext, "argth" or
-- content as the wikitext, "argth" or "argtd" uses content as
-- "argtd" uses content as the parameter name
-- the parameter name to produce the suitable content
-- content string
-- to produce the suitable content
-- content string Content to be used as wikitext or a parameter
-- Content to be used as wikitext or a parameter name
-- name
-- attr {...} or nil
-- attr {...} or nil The attributes of the cell in table form
-- The attributes of the cell in table form
-- colspan number or nil The colspan of the cell
-- colspan number or nil
-- rowspan number or nil The rowspan of the cell
-- The colspan of the cell
-- css {...} or nil The css of the cell in table form
-- rowspan number or nil
-- The rowspan of the cell
-- css {...} or nil
-- The css of the cell in table form
-- @param options { hideIfEmpty }
-- hideIfEmpty { param_name, ... }
-- param_name string
-- The param_name that will be used to check if corresponding
-- content is nil
-- @return self
-- @return self
-- The current object
-- The current object
function InfoboxBuilder:addRow(should_hide, cols, options)
function InfoboxBuilder:addRow(cols, options)
if options then
if options then
if options.hideIfEmpty and #options.hideIfEmpty > 0 then
if options.hideIfEmpty and #options.hideIfEmpty > 0 then
local actual_values = {}
local actual_values = self:getNonNilContent(options.hideIfEmpty)
for i,v in ipairs(options.hideIfEmpty) do
table.insert(actual_values, self:getContent(v))
end
if #actual_values == 0 then
if #actual_values == 0 then