Jump to content

Module:RfX template maker

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 13:35, 30 January 2014 (only access methods if necessary). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

-- This module allows people to make templates that display data about current RfA and RfB discussions,
-- without them having to know how to program in Lua.

local getArgs = require('Module:Arguments').getArgs
local currentRfx = require('Module:Current RfX')

local p = {}

local function err(msg)
	return string.format('<strong class="error">Error: %s.</strong>', msg)
end

local function cleanVal(val)
	if val then
		return tostring(val)
	else
		return ''
	end
end

local rfxProperties = {
	['$SUPPORTS'] = 'supports',
	['$OPPOSES'] = 'opposes',
	['$NEUTRALS'] = 'neutrals',
	['$PERCENT'] = 'percent',
	['$ENDTIME'] = 'endTime',
	['$USER'] = 'user'
}

local rfxMethods = {
	['$PAGE'] = function (obj)
		local title = obj:getTitleObject()
		return title.prefixedText
	end,
	['$DUPES'] = function (obj)
		local dupes = obj:dupesExist()
		if dupes then
			return 'yes'
		else
			return 'no'
		end
	end,
	['$TIMELEFT'] = function (obj)
		return obj:getTimeLeft()
	end,
	['$REPORT'] = function (obj)
		local report = obj:getReport()
		return tostring(report)
	end,
	['$STATUS'] = function (obj)
		return obj:getStatus()
	end
}

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

function p._main(args)
	local display = args.display
	local rfxType = args.type
	
	if not display then
		return err('no display value specified')
	end
	
	local rfxes = currentRfx.rfx()
	local rfas = rfxes.rfa
	local rfbs = rfxes.rfb
	
	local rfxTable
	if rfxType == 'rfa' then
		rfxTable = rfas
	elseif rfxType == 'rfb' then
		rfxTable = rfbs
	else
		return err('type parameter not specified; must be "rfa" or "rfb"')
	end

	local ret = {}
	local renderRow = p.renderRow
	for _, rfxObj in ipairs(rfxTable) do
		ret[#ret + 1] = renderRow(rfxObj, display)
	end
	return table.concat(ret, '\n')
end

function p.renderRow(obj, display)
	local gsub = mw.ustring.gsub
	for var, property in pairs(rfxProperties) do
		local val = obj[property]
		val = cleanVal(val)
		display = gsub(display, var, val)
	end
	for var, func in pairs(rfxMethods) do
		display = gsub(display, var, function ()
			local val = func(obj)
			return cleanVal(val)
		end)
	end
	return display
end

return p