Module:Infobox/vNext: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local h = {}
local util = {
tables = {
concat = require("Module:Util/tables/concat")
}
}


-- Retrieves corresponding infobox configuration (e.g. [[Template:Infobox/Config/Character]])  
-- Retrieves corresponding infobox configuration (e.g. [[Template:Infobox/Config/Character]])  
Line 5: Line 12:
function p.templateSpec(subjectType)
function p.templateSpec(subjectType)
local spec = {}
local spec = {}
local baseConfig = mw.loadData("Module:Infobox/Config")
local config = mw.loadData("Module:Infobox/Config/"..subjectType)
local config = mw.loadData("Module:Infobox/Config/"..subjectType)
local applyOption = h.optionApplicator(baseConfig, config)
local params = {}
table.insert(params, baseConfig.baseParams)
applyOption(params, "addHiddenType")
applyOption(params, "inUniverse")
table.insert(params, config.addParams)
applyOption(params, "addStrategy")
params = util.tables.concat(params)
return spec
return spec
end
function h.optionApplicator(baseConfig, config)
return function(paramSets, optionName)
if config[optionName] == nil or baseConfig.options[optionName] == nil then
return
end
local optionValue = config[optionName]
if type(optionValue) == "table" then -- used by addHiddenTypeParam whose value is a table
optionValue = true
end
local paramNames = baseConfig.options[optionName][optionValue]
if not paramNames then
return
end
local params = {}
for i, v in ipairs(paramNames) do
local param = baseConfig.optInParams[v]
if param then
table.insert(params, param)
end
end
table.insert(paramSets, params)
end
end
end


return p
return p

Revision as of 15:15, 12 May 2024

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

local p = {}
local h = {}

local util = {
	tables = {
		concat = require("Module:Util/tables/concat")
	}
}

-- Retrieves corresponding infobox configuration (e.g. [[Template:Infobox/Config/Character]]) 
-- and converts it the template spec format that [[Module:Documentation]] and [[Module:UtilsArg]] understand.
function p.templateSpec(subjectType)
	local spec = {}

	local baseConfig = mw.loadData("Module:Infobox/Config")
	local config = mw.loadData("Module:Infobox/Config/"..subjectType)
	local applyOption = h.optionApplicator(baseConfig, config)
	
	local params = {}
	table.insert(params, baseConfig.baseParams)
	applyOption(params, "addHiddenType")
	applyOption(params, "inUniverse")
	table.insert(params, config.addParams)
	applyOption(params, "addStrategy")
	params = util.tables.concat(params)

	return spec
end

function h.optionApplicator(baseConfig, config)
	return function(paramSets, optionName)
		if config[optionName] == nil or baseConfig.options[optionName] == nil then
			return
		end
		
		local optionValue = config[optionName]
		if type(optionValue) == "table" then -- used by addHiddenTypeParam whose value is a table
			optionValue = true
		end
		local paramNames = baseConfig.options[optionName][optionValue]
		if not paramNames then
			return
		end
		
		local params = {}
		for i, v in ipairs(paramNames) do
			local param = baseConfig.optInParams[v]
			if param then
				table.insert(params, param)
			end
		end
		table.insert(paramSets, params)
	end
end

return p