Module:Progression rainbow: Difference between revisions

Content deleted Content added
add some comments with questions I have
Override the default A-class color
 
(13 intermediate revisions by 6 users not shown)
Line 3:
 
]]
require ('Module:No globalsstrict')
 
local getArgs = require ('Module:Arguments').getArgs
local p = {}
 
Line 13 ⟶ 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 ⟶ 30:
local function category_count(category, project)
return mw.site.stats.pagesInCategory(
category string.. format('%s %s articles', ..category, project .. ' articles'),
'pages'
)
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
return args[from_arg]
else
return default
end
end
 
function p._main(args, frame)
-- Is there a way to avoid passing a reference to the parent frame?
-- extensionTag must be available in p._main, which on the frame object.
 
local classes = {
Line 58 ⟶ 63:
}
 
local project = arg_or_default(args, "project", nil)
-- generally, I think if there were more 'project' orthogonal attributes
-- there would be a case for object-orientation here
-- I'm not sure what that would look like
local project
-- is there a more idiomatic way to initialize project?
if args['project'] and args['project'] ~= '' then
project = args['project']
else
project = nil
end
local sum_classes = 0
if project then
for i_, class in pairsipairs(classes) do
class['.count'] = category_count(class['.category'], project)
if class['.class'] == 'FA' then
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
-- I'm not sure if it's clear in this for loop that I want to get
-- 'or class.count' to keep us safe from nil arg
-- parameters 1 to 8, which I could also do instead as 'for 1,8...'' ]]
for i, class.count in= pairstonumber(classesargs[i]) door class.count
sum_classes = sum_classes + class.count
if args[i] then
class['count'] = tonumber(args[i])
sum_classes = sum_classes + class['count']
end
end
end
 
local divisortotal
if project then
-- I think itIt makes more sense to do this sum here rather than in the project
-- project loop above, because Itotal is initialized divisor here in the non-project case
total = sum_classes + category_count(
-- project case
project_classes[2].category,
divisor = sum_classes + category_count(
project_classes[2]['category'],
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() -- sprinkled through to make the HTML easier to read
-- 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 accessibly-hidden by CSS
:css('width', percentage)
:wikitext(percentage .. " " .. class['category'])
:attr('title', string.format('%s %s', percentage, class.category))
-- what is the more friendly way for i18n?
:donetag('span')
:doneaddClass('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')
-- and same question here for i18n
:addClass('sr-only')
:wikitext(remaining .. " remaining")
:wikitext(string.format('%s remaining', remaining))
:done()
:done()
:done()
:newline()
end
root:allDone()
-- Not sure if p._main should be stringified instead of returning an object.
return framecurrent_frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Progression rainbow/styles.css'}
} .. current_frame:extensionTag{
args = { src = 'Progression rainbow/styles.css'}
name = 'templatestyles', args = { src = 'Screen reader-only/styles.css'}
} .. '\n' .. tostring(root)
end
-- not sure how to add \n before root is initialized, because root must be
 
-- an HTML element, and \n is not
function p.main(frame)
return p._main(require('Module:Arguments').getArgs(frame))
end