Jump to content

Module:Sandbox/Ahecht

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Ahecht (talk | contribs) at 21:54, 28 November 2021 (default to location 1). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}
function p.main(frame)
	local output = {}
	output[1] = 'frame: ' .. type(frame) .. ' '
	if frame[1] then
		output[2] = frame[1]
	else
		output[2] = ''
	end
	output[3] = '\<br />frame.args: ' .. type(frame.args) .. ' '
	if frame.args[1] then
		output[4] = frame.args[1]
	else
		output[4] = ''
	end
	output[5] = '<br />frame:getParent().args ' .. type(frame:getParent().args) .. ' '
	if frame:getParent().args[1] then
		output[6] = frame:getParent().args[1]
	else
		output[6] = ''
	end
	if frame.args['test'] == 0 then
		output[7] = '<br />0 the number'
	elseif frame.args['test'] == '0' then
		output[7] = '<br />0 the character'
	else
		output[7] = ''
	end
	
	return table.concat(output)
end

function p.infoboxattraction(frame)
	local output = {"{{Infobox attraction/section/sandbox"}
	local args = {[0]={}}
	for k,v in pairs(frame:getParent().args) do
		local val = mw.text.trim(v)
		if val or "" ~= "" then
			-- val exists and isn't blank
			local loc = tonumber(mw.ustring.sub(k,-1))
			if (loc) and (loc > 0) and (loc <= 10) 
				and (not mw.ustring.match(k, "^location_%d+$"))
				and (not mw.ustring.match(k, "^custom_")) then
				-- last character is a number 1-10, and it's not "location_#" or "custom_"
				if not args[loc] then args[loc] = {} end
				-- strip "location_" and number from key
				local key = mw.ustring.gsub(mw.ustring.gsub(k, "_?%d+$", ""), "^location_", "")
				args[loc][key] = val
			elseif (not loc) and (mw.ustring.match(k, "^location_%D")) then
				-- default to location 1
				if (not args[1]) then args[1] = {} end
				-- strip "location_" from key
				local key = mw.ustring.gsub(k, "^location_", "")
				args[1][key] = val
			else
				args[0][k] = val
			end
		end
	end
	for k, v in pairs(args[0]) do
		table.insert(output, "| " .. k .. " = " .. v)
	end
	for i, a in ipairs(args) do 
		if not args[0]["location_" .. i] then
			-- location subtemplate isn't defined
			table.insert(output, "| location_" .. i .. " = {{Infobox attraction/section/sandbox|child=yes")
			for k, v in pairs(a) do
				table.insert(output, "|     " .. k .. " = " .. v)
			end
			table.insert(output,"}}")
		end
	end
	table.insert(output, "}}")
	
	return table.concat(output, "\n")
end

return p