Module:Progression rainbow/sandbox: Difference between revisions

Content deleted Content added
add a done at the end?
use require('strict') instead of require('Module:No globals')
 
(7 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 or a function parameter
-- which takes a boolean. :(
return tostring(round(100 * param / total)) .. '%'
end
Line 24 ⟶ 22:
local remaining = total - sum
if remaining > 0 then
return tostringpercent(round(100 * remaining /, total)) .. '%'
else
return nil
Line 47 ⟶ 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 available for extensionTag and expandTemplate
-- TODO: Digest what Johnuniq said on [[WT:Lua]] about the frame.
 
local classes = {
Line 88 ⟶ 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 106 ⟶ 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:tagcss('trheight', height)
end
:addClass('progression-rainbow-mystery')
-- We add a tr here. MediaWiki (Remex we think) adds a second. One of
-- them becomes <tr class='mw-empty-elt'></tr>. Removing this one
-- results in only
 
local current_frame = mw.getCurrentFrame()
for _, class in ipairs(classes) do
if class.count ~= 0 then
local percentage = class_percentpercent(class.count, total)
-- tr problem is unrelated to this newline()
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 136 ⟶ 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:doneallDone() -- close the tr?
return framecurrent_frame:extensionTag{
name = 'templatestyles', args = { src = 'Module:Progression rainbow/styles.css'}
} .. current_frame:extensionTag{
name = 'templatestyles', args = { src = 'ProgressionScreen rainbowreader-only/styles.css'}
} .. '\n' .. tostring(root)
end
 
function p._mainmain(args, frame)
local getArgs = return p._main(require('Module:Arguments').getArgs(frame))
end