Module:RfX template maker

-- 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 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,
	secondsleft = function (obj)
		return obj:getSecondsLeft()
	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)
	local template = args.template
	if not template then
		return err('template not specified')
	end

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

	-- Work out what properties and methods were specified in the arguments, so that
	-- we don't have to generate data from the rfx object needlessly, and so that 
	-- we don't have to check the arguments for every row.
	local propertiesToUse, methodsToUse = {}, {}
	for argName, property in pairs(rfxProperties) do
		if args[argName] then
			propertiesToUse[argName] = property
		end
	end
	for argName, func in pairs(rfxMethods) do
		if args[argName] then
			methodsToUse[argName] = func
		end
	end

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

function p.renderRow(obj, propertiesToUse, methodsToUse, template, frame)
	local targs = {}
	for argName, property in pairs(propertiesToUse) do
		targs[argName] = obj[property]
	end
	for argName, func in pairs(methodsToUse) do
		targs[argName] = func(obj)
	end
	return frame:expandTemplate{title = template, args = targs}
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.