Jump to content

Module:Forward parameters to template call

From Wikipedia, the free encyclopedia

local p = {}
local getArgs = require('Module:Arguments').getArgs
local sparseIpairs = require('Module:TableTools').sparseIpairs

local function convert(args)
	local filter = {}
	local newParams = {}
	for arg, val in pairs(args) do
		if arg == tostring(arg) then
			if arg:match("^_convert_prefix") then
				filter[val] = {}
			else
				if arg:match("^_forward_name") then
					newParams[val] = args[val]
	end end end end
	for arg, val in pairs(args) do
		if arg == tonumber(arg) then
			newParams[#newParams + 1] = val
		else
			for prefix, _ in pairs(filter) do
				if arg:match("^" .. prefix) then
					if arg == prefix then
						filter[prefix][0] = val
					else
						local prefixnum = arg:match("^" .. prefix .. "(%d)")
						if prefixnum then
							filter[prefix][tonumber(prefixnum)] = val
	end end end end end end
	for _, values in pairs(filter) do
		for _, val in sparseIpairs(values) do
			newParams[#newParams + 1] = val
	end end
	return newParams
end
	
function p._main(args)
	local markup = "{{" .. args._template
	local convertedArgs = convert(args)
	for i, val in ipairs(convertedArgs) do
		markup = markup .. "|" .. i .. "=" .. val
	end
	for param, val in pairs(convertedArgs) do
		if param == tostring(param) and not param:match("^_") then
			markup = markup .. "|" .. param .. "=" .. val
	end end
	return markup .. "}}"
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

return p