Module:Progression rainbow/sandbox: Difference between revisions

Content deleted Content added
fix that
use require('strict') instead of require('Module:No globals')
 
(27 intermediate revisions by 3 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, total)
-- These suck for i18n because the % is forced to the right without spacing,
-- 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
Line 22:
local remaining = total - sum
if remaining > 0 then
return tostringpercent(round(100 * remaining /, total)) .. '%'
else
return nil
Line 30:
local function category_count(category, project)
return mw.site.stats.pagesInCategory(
-- specious use of string.format
string.format('%s %s articles', category, project),
'pages'
Line 36 ⟶ 35:
end
 
-- This is only done once in this module, here for demonstration.
-- Gist: Make it cleaner to initialize 'trivial' variables.
local function set_or_default arg_or_default(args, from_arg, default)
if args[from_arg] and args[from_arg] ~= '' then
return args[from_arg]
Line 45:
end
 
function p.main_main(frameargs)
local args = getArgs(frame)
return p._main(args, frame)
end
 
function p._main(args, frame)
-- frame needs to be passed to p._main for extensionTag and expandTemplate
 
local classes = {
Line 69 ⟶ 63:
}
 
local project = set_or_defaultarg_or_default(args, "project", nil)
local sum_classes = 0
Line 84 ⟶ 78:
end
else
for _i, class in pairsipairs(classes) do
-- Parameter 9 and others is the total non-project, so exclude
-- 'or 0class.count' to keep us safe from a non-numeric value. Cannil considerarg
for i = 1,8 do
classes[i]class.count = tonumber(args[i]) or 0class.count
-- 'or 0' to keep us safe from a non-numeric value. Can consider
sum_classes = sum_classes + classes[i]class.count
-- whether to error here.
classes[i].count = tonumber(args[i]) or 0
sum_classes = sum_classes + classes[i].count
end
end
Line 96 ⟶ 88:
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,
Line 105 ⟶ 97:
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 _, class in pairs(classes) do
for _, class in ipairs(classes) do
if class.count ~= 0 then
local percentage = class_percentpercent(class.count, total)
root:newline() -- sprinkled through to make the HTML easier to read
root:tag('tdli')
:css('background', framecurrent_frame:expandTemplate{
title = 'class/colour', args = { class.class }}
})
:css('width', percentage)
:attr('title', string.format('%s %s', percentage, class.category))
:tag('span')
-- wikitext accessibly-hidden by CSS
:addClass('sr-only')
:wikitext(string.format('%s %s', percentage, class.category))
:done()
:done()
end
end
Line 129 ⟶ 127:
local remaining = percent_remaining(sum_classes, total)
if remaining then
root:tag('tdli')
:addClass('remaining')
:css('width', remaining)
:wikitextattr('title', string.format('%s remaining', remaining))
:tag('span')
:wikitext(string.format('%s remaining', remaining))
:doneaddClass('sr-only')
:wikitext(string.format('%s remaining', remaining))
:done()
:done()
:done()
:newline()
end
root:allDone()
return framecurrent_frame:extensionTag{
name = 'templatestyles',
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._mainmain(args, frame)
local getArgs = return p._main(require('Module:Arguments').getArgs(frame))
end