Module:Progression rainbow/sandbox: Difference between revisions

Content deleted Content added
poke
 
let's see how completely broken this is
Line 14:
 
local function class_percent(param, total)
return string.format('%s %%', tostring(round(100 * param / total)) .. '%')
end
 
local function percent_remaining(sum, total)
local remaining = sum - total
if remaining ~=> 0 then -- find this a bit specious
return string.format('%s %%', tostring(round(-100 * remaining / total)) .. '%')
else
return nil
Line 28:
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
local function set_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 ⟶ 48:
 
function p._main(args, frame)
-- frame needs to be passed to p._main 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 is on the frame object.
 
local classes = {
Line 58 ⟶ 66:
}
 
local project = set_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 _, class in pairsipairs(classes) do
class.count = category_count(class.category, project)
if class.class == 'FA' then
Line 82 ⟶ 81:
end
else
for iarg, classvalue in pairsipairs(classesargs) do
-- I'm not sure if it's clear in this for loop that I want to get
-- parameterParameter 9, which is the total non-project, so exclude
-- parameters 1 to 8, which I could also do instead as 'for 1,8...''
if args[i]arg ~= 9 then
-- unfortunately can't do for i, class in ipairs(args) because of
-- 'or 0' to keep us safe from some maniac puttinga non-numeric value. Can consider
-- parameter 9, which is the total non-project
-- whether to error here.
for i, class in pairs(classes) do
classclasses[arg].count = tonumber(args[i]value) or 0
if args[i] then
-- 'or 0' to keep us safe from some maniac putting non-numeric
-- input in the parameter of args. Can consider whether to error
-- here for the usually-innocent maniacas.
class.count = tonumber(args[i]) or 0
sum_classes = sum_classes + class.count
end
Line 99 ⟶ 94:
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,
Line 125 ⟶ 119:
:tag('span')
-- wikitext accessibly-hidden by CSS
:wikitext(percentage .string.format('%s "%s', " ..percentage, class.category))
-- what is the more friendly way for i18n?
:done()
:done()
Line 139 ⟶ 132:
: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