Module:Progression rainbow: Difference between revisions

Content deleted Content added
poke
Override the default A-class color
 
(22 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 class_percentpercent(param, divisortotal)
-- These suck for i18n because the % is forced to the right without spacing,
return tostring(round(100 * param / divisor)) .. '%'
-- both in a required context (CSS) and unrequired (to-be-displayed text).
-- I.e., there should be a separate version of this
return tostring(round(100 * param / total)) .. '%'
end
 
local function percent_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
Line 28:
end
 
local function category_count(category, project, frame)
return tonumber(frame:callParserFunctionmw.site.stats.pagesInCategory(
string.format('%s %s articles', category, project),
'PAGESINCATEGORY',
'pages'
category .. project .. 'articles',
)
'R'
))
end
 
-- This is only done once in this module, here for demonstration.
function p.main(frame)
-- Gist: Make it cleaner to initialize 'trivial' variables.
local args = getArgs(frame)
local function arg_or_default(args, from_arg, default)
return p._main(args, frame)
if args[from_arg] and args[from_arg] ~= '' then
-- pretty sure that this call to extensionTag should be in _main
return args[from_arg]
-- since a caller from a module should get the associated styles also
end
 
function p._main(args, frame)
-- is there a way that I can avoid passing a reference to the parent frame?
-- I need to be able to access extensionTag and callParserFunction in p._main
-- these are defined on frame
local project
if args['project'] and args['project'] ~= '' then
project = args['project']
else
return default
project = nil
end
end
 
function p._main(args)
 
local classes = {
{count = 0, class = 'List', category = 'List-Class'},
Line 70 ⟶ 62:
{count = 0, class = 'Unassessed', category = 'Unassessed'}
}
 
local project = arg_or_default(args, "project", nil)
-- set the value from arguments in classes, first column
local sum_classes = 0
if project then
for i, class in pairs(classes) do
for _, class in ipairs(classes) do
if project then
class['.count'] = category_count(class['.category'], project, frame)
if class['.class'] == 'FA' then
class['.count'] = class['.count'] + category_count(
project_classes[1]['.category'],
project,
frame
)
end
sum_classes = sum_classes + class.count
elseif args[i] then
end
class['count'] = tonumber(args[i])
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
sum_classes = sum_classes + class['count']
end
 
local divisortotal
if project then
-- It makes more sense to do this sum here rather than in the project
divisor = sum_classes + category_count(
-- loop above because total is initialized here in the non-project case
project_classes[2]['category'],
total = sum_classes + category_count(
project,
project_classes[2].category,
frame
project
)
else
divisortotal = tonumber(args[9]) or 100
end
local rootheight = mw.html.createarg_or_default(args, 'tableheight', nil)
local root = mw.html.create('ul')
root:addClass('progression-rainbow')
if height then
:attr('role', 'presentation')
root:css('height', height)
end
 
local current_frame = mw.getCurrentFrame()
for i, class in pairs(classes) do
for _, class in ipairs(classes) do
if class['count'] ~= 0 then
if class.count ~= 0 then
local percentage = class_percent(class['count'], divisor)
local percentage = percent(class.count, total)
root:newline()
-- The default Start-class color is difficult to distinguish next to
:tag('td')
-- the Stub-class color, so we pick a midpoint between Stub and C
:css('background', frame:expandTemplate{
titleif = 'class/colour', args.class == { class['classStart'] }}then
root:newline() -- sprinkled through to make the HTML easier to read
)
root:csstag('widthli', percentage)
:tagcss('spanbackground', '#ffca66')
:css('color','inherit')
:wikitext(percentage .. " " .. class['category'])
:donecss('width', percentage)
:attr('title', string.format('%s %s', percentage, class.category))
:done()
: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:newline()
local remaining = percent_remaining(sum_classes, divisortotal)
if remaining then
root:tag('tdli')
:addClass('remaining')
:css('width', remaining)
:attr('title', string.format('%s remaining', remaining))
:tag('span')
:tag('span')
:wikitext(remaining .. " remaining")
:doneaddClass('sr-only')
:wikitext(string.format('%s remaining', remaining))
:done()
:done()
:done()
:newline()
end
root:allDone()
-- not sure if I should be stringifying p._main return instead of returning
return current_frame:extensionTag{
-- a table
name = 'templatestyles', args = { src = 'Module:Progression rainbow/styles.css'}
return frame:extensionTag{
} .. current_frame:extensionTag{
name = 'templatestyles',
name = 'templatestyles', args = { src = 'ProgressionScreen rainbowreader-only/styles.css'}
} .. '\n' .. tostring(root) .. mw.dumpObject(classes)
end
 
function p.main(frame)
return p._main(require('Module:Arguments').getArgs(frame))
end