Module:Progression rainbow/sandbox: Difference between revisions

Content deleted Content added
already init the one in the table so use that value
use require('strict') instead of require('Module:No globals')
 
(20 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 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
-- TODO: Digest what Johnuniq said on [[WT:Lua]] about the frame.
 
local classes = {
Line 86 ⟶ 79:
else
for i, class in ipairs(classes) do
-- 'or class.count' to keep us safe from anil non-numeric value in args.arg
class.count = tonumber(args[i]) or class.count
sum_classes = sum_classes + class.count
Line 104 ⟶ 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 pairsipairs(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{
-- I don't understand why we don't need to create a 'tr'. When
-- title added= deliberately'class/colour', weargs get= a <tr{ class='empty'></tr>.class hanging}
})
-- out. Possibly a consequence of Remex instead of HTML Tidy
:css('width', percentage)
-- or maybe mw.html is just smarter than me.
:wikitextattr('title', string.format('%s %s', percentage, class.category))
:tag('td')
:tag('span')
:css('background', frame:expandTemplate{
:addClass('sr-only')
title = 'class/colour', args = { class.class }}
:wikitext(string.format('%s remaining%s', remainingpercentage, class.category))
)
:cssdone('width', percentage)
:tagdone('span')
-- wikitext accessibly-hidden by CSS
:wikitext(string.format('%s %s', percentage, class.category))
:done()
:done()
end
end
Line 133 ⟶ 127:
local remaining = percent_remaining(sum_classes, total)
if remaining then
root:tag('tdli')
:addClass('remaining')
:css('width', remaining)
:attr('title', string.format('%s remaining', remaining))
:tag('span')
: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