Module:Sandbox/Ahecht/flag

This is an old revision of this page, as edited by Ahecht (talk | contribs) at 05:31, 12 June 2020 (simplify). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function p._main(args, depth)
	local params = {}

	for k,v in pairs(args) do
		k = mw.text.split(k, "-", true)
		if not params[k[1]] then
			params[k[1]] = {}
		end
		if (k[2] or '') ~= '' then
			params[k[1]][table.concat(k,"-",2)] = v
		else
			params[k[1]]['#default'] = v
		end
	end
	
	for k,v in pairs(params) do
		local count = 0
		for _ in pairs (v) do count = count + 1 end
		if (count == 1) and (params[k]['#default']) then
			params[k] = params[k]['#default']
		end
	end
	
	local output = {}
	
	for k,v in pairs(params) do
		local key  = mw.ustring.rep('\t', depth) .. '["' .. k .. '"] = '
		if type(v) == "string" then
			output[#output+1] = key .. '"' .. v .. '"'
		elseif type(v) == "table" then
			local subout = p._main(v, depth+1)
			output[#output+1] = key .. '{\n' .. table.concat(subout,",\n") .. '\n' .. mw.ustring.rep('\t', depth) ..'}'
		end
	end

	return output
end


function p.main(frame)
	local args={}
	
	for k,v in pairs(frame:getParent().args) do
		if (v or '') ~= '' then
			args[k] = v
		end
	end
	for k,v in pairs(frame.args) do
		if (v or '') ~= '' then
			args[k] = v
		end
	end
	
	if args.alias then
		local output = p._main(args, 1)
		return '<pre>["' .. args.alias .. '"] = {\n' .. table.concat(output,",\n") .. '\n},\n</pre>'
	else
		return ""
	end
end

return p