Module:Progression rainbow: Difference between revisions

Content deleted Content added
newline at top
Override the default A-class color
 
(38 intermediate revisions by 6 users not shown)
Line 1:
--[[
This implements {{progression rainbow/manual}}
 
Next up, {{progression rainbow}} fully?
 
]]
require ('Module:No globalsstrict')
 
local getArgs = require ('Module:Arguments').getArgs
local p = {}
 
Line 15 ⟶ 12:
end
 
local function width_classespercent(param, divisortotal)
-- These suck for i18n because the % is forced to the right without spacing,
-- implementation of rounding to first decimal in
-- both in a required context (CSS) and unrequired (to-be-displayed text).
-- http://lua-users.org/wiki/SimpleRound
-- I.e., there should be a separate version of this
return tostring(round(100 * param / divisor)) .. '%'
return tostring(round(100 * param / total)) .. '%'
end
 
local function width_remainingpercent_remaining(sum, divisortotal)
sumlocal remaining = sumtotal - divisorsum
if sumremaining ~=> 0 then -- find this a bit specious
return tostringpercent(round(-100remaining, * sum / divisortotal)) .. '%'
else
return nil
end
end
 
local function p.maincategory_count(framecategory, project)
return mw.site.stats.pagesInCategory(
local args = getArgs(frame)
string.format('%s %s articles', category, project),
local main_frame = frame
'pages'
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
 
-- This is only done once in this module, here for demonstration.
-- we should implement this more accessibly,
-- Gist: Make it cleaner to initialize 'trivial' variables.
-- like providing the values for blind people and then hiding with CSS
local function arg_or_default(args, from_arg, default)
-- HTML <meter> could implement this? not sure
if args[from_arg] and args[from_arg] ~= '' then
function p._main(args, main_frame)
return args[from_arg]
local divisor = args['9'] or 100
else
return default
end
end
 
function p._main(args)
 
local classes = {
{count = 0, class = 'List', category = 'List-Class'},
{count = 0, class = 'Stub', category = 'Stub-Class'},
{count = 0, class = 'Start', category = 'Start-Class'},
{count = 0, class = 'C', category = 'C-Class'},
{count = 0, class = 'B', category = 'B-Class'},
{count = 0, class = 'GA', category = 'GA-Class'},
{count = 0, class = 'A', category = 'A-Class'},
{count = 0, class = 'FA', category = 'FA-Class'}
}
-- set the value from arguments in classes, first column
local project_classes = {
{count = 0, class = 'FL', category = 'FL-Class'},
{count = 0, class = 'Unassessed', category = 'Unassessed'}
}
 
local project = arg_or_default(args, "project", nil)
local sum_classes = 0
if project then
for i = 1, 8 do
for _, class in ipairs(classes) do
if args[i] then
class.count = category_count(class.category, project)
classes[i][1] = tonumber(args[i])
if class.class == 'FA' then
sum_classes = sum_classes + classes[i][1]
class.count = class.count + category_count(
project_classes[1].category,
project
)
end
sum_classes = sum_classes + class.count
end
else
for i, class in ipairs(classes) do
-- 'or class.count' to keep us safe from nil arg
class.count = tonumber(args[i]) or class.count
sum_classes = sum_classes + class.count
end
end
 
local total
if project then
-- It makes more sense to do this sum here rather than in the project
-- loop above because total is initialized here in the non-project case
total = sum_classes + category_count(
project_classes[2].category,
project
)
else
total = tonumber(args[9]) or 100
end
local rootheight = mw.html.createarg_or_default(args, 'tableheight', nil)
root:addClass('progression-rainbow')
:attr('role', 'presentation')
local root = mw.html.create('ul')
for i = 1,8 do
root:addClass('progression-rainbow')
if classes[i][1] ~= 0 then
if height then
root:newline()
root:tagcss('tdheight', height)
end
:css('background', main_frame:expandTemplate{
 
title = 'class/colour', args = { classes[i][2] }})
local current_frame = mw.getCurrentFrame()
:css('width', width_classes(classes[i][1], divisor))
for _, class in ipairs(classes) do
:done()
if class.count ~= 0 then
local percentage = percent(class.count, total)
-- The default Start-class color is difficult to distinguish next to
-- the Stub-class color, so we pick a midpoint between Stub and C
if class.class == 'Start' then
root:newline() -- sprinkled through to make the HTML easier to read
root:tag('li')
:css('background', '#ffca66')
:css('color','inherit')
:css('width', percentage)
:attr('title', string.format('%s %s', percentage, class.category))
:tag('span')
:addClass('sr-only')
:wikitext(string.format('%s %s', percentage, class.category))
:done()
:done()
-- The default A-class color is difficult to distinguish next to
-- the GA-class color, so we pick a midpoint between GA and FA
elseif class.class == 'A' then
root:newline() -- sprinkled through to make the HTML easier to read
root:tag('li')
:css('background', '#64e2ff')
:css('color','inherit')
:css('width', percentage)
:attr('title', string.format('%s %s', percentage, class.category))
:tag('span')
:addClass('sr-only')
:wikitext(string.format('%s %s', percentage, class.category))
:done()
:done()
else
root:newline() -- sprinkled through to make the HTML easier to read
root:tag('li')
:css('background', current_frame:expandTemplate{
title = 'class/colour', args = { class.class }
})
:css('color','inherit')
:css('width', percentage)
:attr('title', string.format('%s %s', percentage, class.category))
:tag('span')
:addClass('sr-only')
:wikitext(string.format('%s %s', percentage, class.category))
:done()
:done()
end
end
end
root:tagnewline('td')
:addClass('remaining')
:css('width', local remaining = width_remainingpercent_remaining(sum_classes, divisor)total)
if remaining then
:done()
root:tag('li')
:addClass('remaining')
:css('width', remaining)
:attr('title', string.format('%s remaining', remaining))
:tag('span')
:addClass('sr-only')
:wikitext(string.format('%s remaining', remaining))
:done()
:done()
:newline()
end
return root
root:allDone()
return current_frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Progression rainbow/styles.css'}
} .. current_frame:extensionTag{
name = 'templatestyles', args = { src = 'Screen reader-only/styles.css'}
} .. '\n' .. tostring(root)
end
 
function p.main(frame)
return p._main(require('Module:Arguments').getArgs(frame))
end