模組:Summary by Quality and importance
外观
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local p = {}
local yesno = require('Module:Yesno')
local function fnum(num)
if num == nil then return '-' end
if num == 0 then return '-' end
return mw.language.new('zh'):formatNum(tonumber(num))
end
function p.main(frame)
local args = getArgs(frame)
return p._main(args)
end
function p._main(args)
-- preparation
local title = args.title or '狀態統計'
local colHeader = args.rowby or '品質'
local rowHeader = args.colby or '重要度'
local rCount, cCount = 0, 0
for i = 97, 122 do
local r = string.char(i)
if args[r] == nil then break end
rCount = rCount + 1
end
for c = 1, 26 do
if args[c] == nil then break end
cCount = cCount + 1
end
local root, builder = mw.html.create('table'), nil
root
:addClass('wikitable')
:addClass('mw-collapsible')
:css{
['margin'] = '0 0 0.5em 1em',
['float'] = 'right',
['clear'] = 'right',
['text-align'] = 'right',
['font-size'] = 'smaller'
}
-- caption
builder = mw.html.create('caption')
builder
:addClass('nowrap')
:css('font-size', 'larger')
:wikitext(title)
root:node(builder)
-- header rows
builder = mw.html.create('tr')
builder
:tag('th')
:attr{rowspan='2', scope='col'}
:wikitext(colHeader)
:done()
:tag('th')
:attr{colspan=cCount, scope='col'}
:wikitext(rowHeader)
:done()
:tag('th')
:attr{rowspan='2', scope='col'}
:wikitext('合計')
root:node(builder)
builder = mw.html.create('tr')
for c = 1, cCount do
builder:tag('th')
:attr('scope', 'col')
:wikitext(args[c])
end
root:node(builder)
-- build rows
for i = 97, 96 + rCount do
local r = string.char(i) -- ascii code to character
local count = 0
builder = mw.html.create('tr')
builder:tag('th')
:attr('scope', 'row')
:wikitext(args[r])
for c = 1, cCount do
builder:tag('td')
:wikitext(fnum(args[r..c]))
count = count + tonumber(args[r..c] or 0)
end
builder:tag('td')
:css('font-weight', 'bold')
:wikitext(fnum(count))
if count ~= 0 then root:node(builder) end -- 不顯示0項目
end
-- build sum row
builder = mw.html.create('tr'):css('font-size', 'bold')
builder:tag('th')
:attr('scope', 'row')
:wikitext('總計')
local totalCount = 0
for c = 1, cCount do
local count = 0
for i = 97, 96 + rCount do
local r = string.char(i) -- ascii code to character
local num = tonumber(args[r..c] or 0)
count = count + num
totalCount = totalCount + num
end
builder:tag('td')
:css('font-weight', 'bold')
:wikitext(fnum(count))
end
builder:tag('td')
:css('font-weight', 'bold')
:wikitext(fnum(totalCount))
root:node(builder)
-- RETURN
return tostring(root)
end
return p