Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:VgData/sandbox

From Wikipedia, the free encyclopedia
require('strict')

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

--	pull arguments from Invoke and process them appropriately
function p.main(frame)
	local args = getArgs(frame)
	args.ct = tostring(frame.args["ct"])
	args.all = not(not(frame.args["all"]))
	args.col = frame.args["col"] or 3
	args.ver = tostring(frame.args["ver"]) or "none"
	
	local page
	local caption
	if args.ct == "platform" then
		page = "VgPlatforms"
		caption = "Video game platforms"
	elseif args.ct == "genre" then
		page = "VgGenres"
		caption = "Video game genres"
	elseif args.ct == "release" then
		page = "VgRelease"
		caption = "Types of releases"
		args.col = 1
	else
		return "Table type not found"
	end

	local max = 0
	for i, j in ipairs(args) do
		max = max + 1
	end
	return p.import(args, page, caption, max)
end

--	get the specific table block, and iterate each row (lines starting with |)
function p.import(args, page, caption, max)
	local content = mw.title.makeTitle( 'Template', page ):getContent()
	local twiki
	if args.ct == "genre" then
		if args.ver == nil or args.ver == "none" then
			twiki = content:match("<%!%-%- " .. args.ct .. "%-%->(.-)<%!%-%- /" .. args.ct .. "%-%->")	
		else
			twiki = content:match("<%!%-%- " .. args.ver .. "%-%->(.-)<%!%-%- /" .. args.ver .. "%-%->")	
		end
	else
		twiki = content:match("<%!%-%- " .. args.ct .. "%-%->(.-)<%!%-%- /" .. args.ct .. "%-%->")
	end

	if args.all == true then
		args = {}
	end
	local input = {}
	for row in twiki:gmatch("%|(.-)\n") do
		local cells = {}

		local 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 args.all == true then
			table.insert(args, cells[1])
			max = max + 1
		end
		input[cells[1]] = cells[2]
	end
	return p.out(args, caption, max, input)
end

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

return p