Jump to content

Module:Progression rainbow

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Izno (talk | contribs) at 07:29, 27 October 2020 (add spans so we can nuke this text off the screen). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[
This implements {{progression rainbow/manual}}

Next up, {{progression rainbow}} fully?

]]
require ('Module:No globals')

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

-- rounding to first decimal, from http://lua-users.org/wiki/SimpleRound
local function round(num)
	return math.floor(num * 10 + 0.5) / 10
end

local function class_percent(param, divisor)
	return tostring(round(100 * param / divisor)) .. '%'
end

local function percent_remaining(sum, divisor)
	sum = sum - divisor
	if sum ~= 0 then -- find this a bit specious
		return tostring(round(-100 * sum / divisor)) .. '%'
	else
		return nil
	end
end	

function p.main(frame)
	local args = getArgs(frame)
	local main_frame = frame
	return frame:extensionTag{ name = 'templatestyles',
		args = { src = 'Progression rainbow/styles.css'} }
		.. '\n' .. tostring(p._main(args, main_frame))
		-- pretty sure that this call to extensionTag should be in _main
end

-- we should implement this more accessibly,
-- like providing the values for blind people and then hiding with CSS
-- HTML <meter> could implement this? not sure
function p._main(args, main_frame)
	local divisor = args['9'] or 100
	local classes = {
		{0, 'List'},
		{0, 'Stub'},
		{0, 'Start'},
		{0, 'C'},
		{0, 'B'},
		{0, 'GA'},
		{0, 'A'},
		{0, 'FA'}
	}
	-- set the value from arguments in classes, first column
	local sum_classes = 0
	for i, class in pairs(classes) do
		if args[i] then
			class[1] = tonumber(args[i])
			sum_classes = sum_classes + class[1]
		end
	end
	
	local root = mw.html.create('table')
	root:addClass('progression-rainbow')
		:attr('role', 'presentation')
	
	for i, class in pairs(classes) do
		if class[1] ~= 0 then
			local percentage = class_percent(class[1], divisor)
			root:newline()
				:tag('td')
				:css('background', main_frame:expandTemplate{
					title = 'class/colour', args = { class[2] }})
				:css('width', percentage)
				:tag('span')
				:wikitext(percentage .. " " .. class[2] .. "-class")
				:done()
				:done()
		end
	end
	root:newline()
	
	local remaining = percent_remaining(sum_classes, divisor)
	if remaining then
		root:tag('td')
		:addClass('remaining')
		:css('width', remaining)
		:tag('span')
		:wikitext(remaining .. " remaining")
		:done()
		:done()
		:newline()
	end
	return root
end

return p