Module:Progression rainbow: Difference between revisions

Content deleted Content added
remove that one
from sandbox
Line 3:
 
]]
require ('Module:No globals')
 
local getArgs = require ('Module:Arguments').getArgs
local p = {}
 
Line 14:
 
local function class_percent(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 or a function parameter
-- which takes a boolean. :(
return tostring(round(100 * param / total)) .. '%'
end
 
local function percent_remaining(sum, total)
local remaining = sumtotal - totalsum
if remaining ~=> 0 then -- find this a bit specious
return tostring(round(-100 * remaining / total)) .. '%'
else
return nil
Line 28 ⟶ 32:
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.
-- Gist: Make it cleaner to initialize 'trivial' variables.
local function arg_or_default(args, from_arg, default)
if args['project'from_arg] and args['project'from_arg] ~= '' then
return args[from_arg]
else
return default
end
end
 
Line 39 ⟶ 53:
 
function p._main(args, frame)
-- frame needs to be available for extensionTag and expandTemplate
-- 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 ⟶ 71:
}
 
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 pairsipairs(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 a non-numeric value in args.
-- parameters 1 to 8, which I could also do instead as 'for 1,8...'' ]]
class['.count'] = tonumber(args[i]) or class.count
-- unfortunately can't do for i, class in ipairs(args) because of
sum_classes = sum_classes + class['.count']
-- parameter 9, which is the total non-project
for i, class in pairs(classes) do
if args[i] then
class['count'] = tonumber(args[i])
sum_classes = sum_classes + class['count']
end
end
end
Line 96 ⟶ 95:
local total
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 total here in the non-project case
-- project case
total = sum_classes + category_count(
project_classes[2]['.category'],
project
)
else
total = tonumber(args[9]) or 100
end
Line 111 ⟶ 109:
:attr('role', 'presentation')
 
for i_, class in pairs(classes) do
if class['.count'] ~= 0 then
local percentage = class_percent(class['.count'], total)
root:newline() -- sprinkled through to make the HTML easier to read
-- I don't understand why we don't need to create a 'tr'. When
-- added deliberately, we get a <tr class='empty'></tr> hanging
-- out. Possibly a consequence of Remex instead of HTML Tidy
-- or maybe mw.html is just smarter than me.
:tag('td')
:css('background', frame:expandTemplate{
title = 'class/colour', args = { class['.class'] }}
})
:css('width', percentage)
:tag('span')
-- wikitext accessibly-hidden by CSS
:wikitext(percentage .string.format('%s "%s', "percentage, class.. class['category']))
-- what is the more friendly way for i18n?
:done()
:done()
Line 136 ⟶ 138:
:css('width', remaining)
:tag('span')
:wikitext(remaining .string.format('%s "remaining', remaining"))
:done()
:done()
:newline()
end
-- Not sure if p._main should be stringified instead of returning an object.
return frame:extensionTag{
name = 'templatestyles',
args = { src = 'Progression rainbow/styles.css'}
} .. '\n' .. tostring(root)
-- not sure how to add \n before root is initialized, because root must be
-- an HTML element, and \n is not
end