Module:Sandbox/13akoors

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
local getArgs = require('Module:Arguments').getArgs
local p = {}

--	pull arguments from Invoke and process them appropriately
function p.main(frame)
	args = getArgs(frame)
	ct = tostring(frame.args["ct"])
	all = not(not(frame.args["all"]))
	col = frame.args["col"] or 2
	if ct == "platform" then
		page = "VgPlatforms"
		caption = "Video game platforms"
	elseif ct == "genre" then
		page = "VgGenres"
		caption = "Genre notes"
		ver = tostring(frame.args["ver"])
	elseif ct == "release" then
		page = "VgRelease"
		caption = "Types of releases"
		col = 1
	else
		return "Table type not found"
	end
	max = 0
	for i, j in ipairs(args) do
		max = max + 1
	end
	return p.import()
end

--	get the specific table block, and iterate each row (lines starting with |)
function p.import()
	content = mw.title.makeTitle( 'Template', page ):getContent()
	if ct == "genre" then
		if ver == nil or ver == "none" then
			twiki = content:match("<%!%-%- "..ct.."%-%->(.-)<%!%-%- /"..ct.."%-%->")	
		else
			twiki = content:match("<%!%-%- "..ver.."%-%->(.-)<%!%-%- /"..ver.."%-%->")	
		end
	else
		twiki = content:match("<%!%-%- "..ct.."%-%->(.-)<%!%-%- /"..ct.."%-%->")
	end
	if all == true then
		args = {}
	end
	input = {}
	for row in twiki:gmatch("%|(.-)\n") do
		cells = {}
		temp = row:gsub("|","\t",1)		--	split on cell separators (converts first "|" to \t and splits on that)
		for cell in temp:gmatch("[^\t]+") do
			table.insert(cells, mw.text.trim(cell))
		end
		if all == true then
			table.insert(args, cells[1])
			max = max + 1
		end
		input[cells[1]] = cells[2]
	end
	return p.out(input)
end

--	filter input data based on args and create a new table
function p.out(input)
	output = "<table class=\"wikitable\" style=\"width:100%;background:#ffffff;font-size:85%\"><caption>"..caption.."</caption>"
	coloutput = ""
	key = 1
	while key <= max do
		coloutput = "<tr>"
		for l=1,col do
			if key <= max then
				if input[args[key]] == nil then
					coloutput = coloutput.."<td style=background:#e3e3e3><b>"..args[key].."</b></td><td>".."Term not found".."</td>"	
				else
					coloutput = coloutput.."<td style=background:#e3e3e3><b>"..args[key].."</b></td><td>"..input[args[key]].."</td>"
				end
			else
				coloutput = coloutput.."<td style=background:#e3e3e3></td><td></td>"
			end
			key = key + 1
		end
		output = output..coloutput.."</tr>"
	end
	output = output.."</table>"
	return output
end


return p