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 05:05, 27 October 2020 (program compiles but outputs nothing :D). 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 width_classes(param, divisor)
	-- implementation of rounding to first decimal in
	-- http://lua-users.org/wiki/SimpleRound
	return tostring(round(100 * param / divisor)) .. '%'
end

local function width_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'} }
		.. 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 = 1, 8 do
		if args[i] then
			classes[i][1] = tonumber(args[i])
			sum_classes = sum_classes + classes[i][1]
		end
	end
	
	local root = mw.html.create('table')
		:addClass('progression-rainbow')
		:attr('role', 'presentation')
		:tag('tr')
	
	for i = 1,8 do
		if classes[i][1] ~= 0 then
			root:tag('td')
				:css({
					'background',
					main_frame:expandTemplate{
						title = 'class/colour', args = { classes[i][2] }
					},
					'width',
					width_classes(classes[i][1], divisor)
				})
				:done()
		end
	end
	root:tag('td')
		:css('width', width_remaining(sum_classes, divisor))
		:allDone()
	return root
end

return p