Content deleted Content added
mNo edit summary |
per edit request on talk page - fix the node_function{orphan} bug |
||
(38 intermediate revisions by 12 users not shown) | |||
Line 4:
'Semi-finals',
'Final',
'Third
},
-- The text and background colors are paired and when you set one, you should set the other (accessibility)
textColor = {head = '#202122', '#202122', '#202122', '#202122', '#202122'},
bgColor = {head = '#f2f2f2', 'gold', 'silver', '#C96', '#f9f9f9'},
reuseStr = {},
saveStr = function(self, name, ...)
Line 15 ⟶ 18:
}
--Provides a convenient naming shortcut up to {{#invoke:RoundN|N512}} = {{invoke:RoundN|main|columns = 9}}
for columns = 1, 9 do
local N = math.pow(2, columns)
p['N' .. N] = function(frame)
return p.main(frame.args, columns)
end
p['n' .. N] = p['N' .. N]--to make case insensitive
end
--saves memory and avoids errors when using a nil as a table by providing a temporary table; if using nil as false; use 'table(k)' to look up table[k]
p.nilAsTab = {
__index = function(t, i)
return setmetatable({}, setmetatable(p.nilAsTab, {__index = {t = t, i = i}}))
end,
__newindex = function (pt, pi, v) --store new values in actual table rather than temporary
rawset(p.nilAsTab.t, p.nilAsTab.i, {})[p.nilAsTab.i][pi] = v
setmetatable(p.nilAsTab.t[p.nilAsTab.i], {__call = p.nilAsTab.__call})
end,
__call = function(t, i)
return t and rawget(t, i)
end
}
--never assign a value to these or they will stop being empty
local infiniteEmpty = setmetatable({}, {__index = setmetatable({}, p.nilAsTab), p.nilAsTab}) -- infiniteEmpty[1][2][3]...[infinity] = {}
local callableEmpty = setmetatable({}, p.nilAsTab)
local rowNum, head, m, col, tab, esc = {}, {}, {num = 1, phase = 0, bold = infiniteEmpty}, {}, mw.html.create'table', {
bs = require'Module:Escape',--backslash
comma = {['(%([^,]*),([^%)]*%))'] = '%1|@!#|%2'},--escape commas in ()
Line 91 ⟶ 120:
return x
end,
lay = setmetatable({}, p.nilAsTab)
},
canvas = {--Merges all cells in node. Content will be the next parameter.
Line 127 ⟶ 156:
end
local function newRow(bodyRow)
local first = p.flex_tree.merge and mw.clone(p.flex_tree.cell) or p.flex_tree.cell
tab.r = tab:tag'tr'
Line 149 ⟶ 169:
end
local function drawHead(text, row3rd)
local td = (row3rd and rowNum[row3rd]:tag'td':attr{rowspan = 2}
or head.row:tag'td')
Line 157 ⟶ 177:
['text-align'] = 'center',
border = '1px solid #aaa',
['background-color'] =
color = p.textColor.head
}
end
end
local function spacer(width)
tab.r:tag'td'
:attr{width = width}
Line 168 ⟶ 189:
end
local function dpBox(v, r)
p.dpBoxBase = p.dpBoxBase or mw.html.create'td':attr{rowspan = 2, colspan = p.colspan}
if not v then
p.dpBoxEmpty = p.previewnumbers and mw.clone(p.dpBoxBase) or p.dpBoxEmpty or mw.clone(p.dpBoxBase):wikitext(p.flex_tree.wt)
rowNum[r]:node(p.dpBoxEmpty)
else
Line 197 ⟶ 218:
end,
spin = function(self, v)
table.insert(self, v)
return self
end,
Line 216 ⟶ 237:
init = function(self, setting)
self.cycles = {original = setting}
for cycle in (setting and esc.bs:text(setting) or '{<.->} {[^%d]*}'):gfind('{(.-)}') do
self:load(cycle)
end
Line 231 ⟶ 252:
}
local function boldWin(s1, s2)
return setmetatable(
p.bold and s1 ~= s2 and (math[({'min', 'max'})[p.bold]](s1, s2) == s1 and {true} or {[2] = true}) or callableEmpty,
p.nilAsTab
)
end
local function maxSpan(span, start, rows)
return math.min(span, math.max(0, rows - start + 1))
end
--in case of templates like RDseed need padding value
p.teamBoxPadding = function()
return '.6ex'
end
p.teamBoxPadTab = {padding = '0 ' .. p.teamBoxPadding()}
p.teamBoxNormal = {border = '1px solid #aaa', ['background-color'] = p.bgColor[4], color = p.textColor[4]}
local function teamBox(v, r, f)
if p.flex_tree.merge and not v and f.phase == 2 then
for i = -2, 0 do
Line 249 ⟶ 279:
tab.r:attr{rowspan = 4}:css{['vertical-align'] = 'center'}
else
if not p.bold then
--backwards compatability (wikitemplates bold each arg individually)
local hasBold, b = tostring(v):gsub("([^']*)'''([^']*)", '%1<b>%2</b>')
if b == 1 then
v = hasBold
end
end
local cell
if f[1] then
cell = f.sumBox and f.sumBox[1] and
{padding = f.sumBox[1]}
or {['border-left'] = f.borderLeft}
cell['text-align'] = v and f[1]
else
cell = p.teamBoxPadTab
end
local text = v or f[1] and '' or ' '
if f.bold then
text = mw.ustring.gsub(text, '(%(%[%[[^%[%]]*%]%]%))', '<span style="font-weight:normal">%1</span>')
end
tab.r = rowNum[r]:tag'td'
:css
:css(cell)
:attr{rowspan = 2}
end
end
Line 286 ⟶ 329:
local mType, mNum = string.match(k, '^(%l+)match(%d*)$')
mType, mNum = ({skip = skipMatch, manualbold = unBold})[mType], tonumber(mNum)
if mType then
if mNum then
mType[mNum] = args:clean(k) == 'yes' or args[k] == 'true'
Line 309 ⟶ 352:
for _, v in ipairs({--more args to boolean
'widescore',
'color',
'color_repechage',
'3rdplace',
'omit_blanks',
Line 324 ⟶ 367:
end
end
p.namespace = mw.title.getCurrentTitle().namespace
p.previewnumbers = p.namespace ~= 0 and p.previewnumbers
p.scoreWasher:init(args['score-clean'])
p.scoreWasher.demo = args.demoWash and tonumber(args:clean('demoWash', {pattern = '%D'}), 10)
Line 331 ⟶ 376:
p.scoreBoxes = (tonumber(args:clean('score-boxes', {pattern = '%D'})) or 1) + sumBox
p.scoreSumBox = p.scoreBoxes > 0 and p.scoreSumBox or nil
local boxStyle = p.scoreBoxes > 1 and
(p.scoreSumBox and
setmetatable(
{{}, [p.scoreBoxes] = {'0 1ex'}},
{__call = function(t, i) if t[i] then return nil end return 0 end}
)
or setmetatable(
{},
{__call = function() return 0 end}
)
)
or setmetatable({}, {__call = function() return nil end})
p.colspan = p.scoreBoxes > 0 and (p.scoreBoxes + 1) or nil
local nodeArgs = {
Line 373 ⟶ 430:
end
tab
:cssText(table.concat{args.scroll_height and 'padding' or 'margin', ':', fontSize and (math.ceil(fontSize * 10) / 10) or '.9', 'em 2em 1em 1em;border:0;', fontSize and '' or 'font-size:90%;border-collapse:separate;', args.style})
:attr{cellpadding = 0, cellspacing = 0}
if not p.no_column_head then--headings row
Line 380 ⟶ 437:
:css{['white-space'] = args.scroll_height and 'nowrap'}
newRow()
else
tab.r = tab:tag'tr'
tab.r:tag'td'
end
local sp = {--set column widths
args['team-width'] or 170,
Line 391 ⟶ 447:
p.short_brackets and 4 or 20
}
local scoreWidth = args:clean('score-width', {pattern = '[^%d;]'}) and mw.text.split(args['score-width'], ';') or {}
scoreWidth[1] = tonumber(scoreWidth[1], 10)
if p.scoreSumBox and #scoreWidth ~= 1 then
local _scoreWidth = {}
for k = 1, p.scoreBoxes - 1 do
_scoreWidth[k] = tonumber(scoreWidth[k], 10) or math.ceil(sp[2] * 0.75)
end
setmetatable(scoreWidth, _scoreWidth)
end
local head_br = {
count = 0,
Line 427 ⟶ 484:
or p.RD[#p.RD + k - p.tCols - 1]
or ('Round of ' .. math.pow(2, p.tCols - k + 1))
drawHead(head.wt)
end
end
Line 446 ⟶ 498:
args.line_px:rep(2):gsub('(%a)(%d)', '%1 %2', 1)
}
p['3rdplace'] = p.tCols == p.cols and (p['3rdplace'] or p.cols > 3 and nil == p['3rdplace']
if p['3rdplace'] then
p.textThird = args.Consol or args['RD' .. (p.cols + 1)] or p.RD[4]
rowNum.third = math.max(math.pow(2, p.branch_upwards and -3 or p.cols - 2) * 9 + (no3rdText and 4 or 9), no3rdText and 12 or 17, rows)
end
for r = 1, rowNum.third or rows do
newRow(r)
end
p.cornerDiv = mw.html.create'div':css{height = tab.line[1][true], ['border-right'] = p.reuseStr.solid}
for c = 1, p.cols do
Line 467 ⟶ 520:
rowNum[1]:node(c < p.cols and
mw.clone(bumpBase):attr{rowspan = bump}
)
end
end
col.top = m.num
p.span = p.tCols > c and bump * 2 or p.branch_upwards or math.max((bump - 1) / 2, 2)
col.show3rd = p['3rdplace'] and c == p.tCols and rowNum.third
local colorFinal, bumpMid = p.color and
for r = 1, col.show3rd or rows, 2 do
m.r = r + bumps
Line 533 ⟶ 581:
rowNum[rows + 1]:tag'td':attr{
rowspan = m.r + 9 - rows - (text and 0 or 2),
colspan = (p.cols - 1) *
}
end
if p.tCols == 1 then
bumps =
elseif p.branch_upwards then
r = 7
Line 550 ⟶ 598:
end
end
dpBox(nodeFunc.pattern and nodeFunc.nonFunc or args[step], m.r
if p.previewnumbers then
rowNum[m.r].nodes[#rowNum[m.r].nodes]
:css{
float = 'left',
Line 563 ⟶ 610:
:wikitext(m.num)
:attr{title = 'Number only visible outside article space (e.g. template) when |numberpreview=yes'}
end
end
Line 573 ⟶ 619:
table.insert(m.nonEmpty, i)
end
end
if p.bold and m.showBox[2] and m.showBox[3] and not unBold[m.num] then
Line 582 ⟶ 625:
clean = {}
}
local notSummed = not p.scoreSumBox or #m.nonEmpty < 2
for s, i in ipairs(m.nonEmpty) do
m.bold.clean[s] = {p.scoreWasher:main(args[i[1]]), p.scoreWasher:main(args[i[2]])}
end
if p.scoreSumBox and m.nonEmpty[2] then
local i = {-step, -step - 1}
table.insert(m.nonEmpty, i)
args[i[1]], args[i[2]] = p.scoreWasher.sum(m.bold.clean)
m.bold.box[p.scoreBoxes] = boldWin(args[i[1]], args[i[2]])
end
m.bold.win = m.bold.box[#m.nonEmpty] or callableEmpty
else
m.bold =
end
end
else
if m.showBox[m.phase] then
if col.color_repechage then
col.color_repechage = 2
if m.bold.win(m.phase) and (colorFinal or col.color_repechage) then
color_index = 1 + (col.show3rd or 0) + (col.color_repechage or 0)
elseif m.bold.box[#m.nonEmpty] then
color_index = 2 + (col.show3rd or 0) + (col.color_repechage or 0)
else
color_index = 4
end
p.teamBoxCSS = (colorFinal or col.color_repechage) and
{border = p.teamBoxNormal.border, ['background-color'] = p.bgColor[color_index], color = p.textColor[color_index]}
or p.teamBoxNormal
else
p.teamBoxCSS = (colorFinal or col.color_repechage) and
{border = p.teamBoxNormal.border, ['background-color'] = p.bgColor[m.phase + (col.show3rd or 0) + (col.color_repechage or 0)], color = p.textColor[m.phase + (col.show3rd or 0) + (col.color_repechage or 0)]}
or p.teamBoxNormal
end
local f = {phase = m.phase, bold = m.bold.win(m.phase)}
teamBox(args[step + nodeArgs.team[m.phase]], m.r, f)
f[1] = 'center'
if p.colspan then
if m.nonEmpty[1] then
local loneSum
if #m.nonEmpty < p.scoreBoxes then
loneSum = #m.nonEmpty == 1 and boxStyle[p.scoreBoxes]
tab.r:attr{colspan = 1 + p.scoreBoxes - #m.nonEmpty}
end
for s, i in ipairs(m.nonEmpty) do
f.borderLeft = boxStyle(s)
f.sumBox = loneSum or boxStyle[s]
f.bold = m.bold.box[s](m.phase)
teamBox(args[i[m.phase]], m.r, f)
end
else
for s = 1, p.scoreBoxes do
f.borderLeft = boxStyle(s)
teamBox(nil, m.r, f)
end
end
Line 617 ⟶ 688:
end
if m.phase == 2 then
col.show3rd = col.show3rd ~= 2 and col.show3rd or nil
if p.scoreWasher.demo and p.scoreWasher.demo == m.num and p.namespace ~= 0 then
table.insert(m.bold.clean, 1, {args[step + nodeArgs.team[1]], args[step + nodeArgs.team[2]]})
return table.concat{
Line 641 ⟶ 713:
end
end
end
end
if p.cols > c then--draw lines to next round
p.unit = bump + 3
bump =
bumps = p.branch_upwards and 4 or (p.unit + 1)
rowNum[1]
Line 657 ⟶ 725:
rowNum[1]:tag'td'
:attr{rowspan = (p.branch_upwards or bump) + 4}
:css(nodeFunc.bridge.lay[c]
{['border-right'] = p.reuseStr.solid}
or {}
Line 663 ⟶ 731:
end
col.n = 0
col.t2 = nil
for r = bumps + 1, rows, p.unit * 2 do
tab.r = rowNum[r]:tag'td'
Line 671 ⟶ 740:
col.n = col.n + 2
col.t2 = skipMatch[col.tot + col.n / 2 + 1] and 3 or ((skipMatch[col.top + col.n] and 1 or 0) + (skipMatch[col.top + col.n + 1] and 2 or 0))
if col.t == 0 then --draws the ']' when a PAIR of matches needs lines
tab.r
:attr{rowspan = maxSpan(p.unit * 2, r, rows)}
Line 678 ⟶ 747:
['border-left'] = 0
})
else --draws the lines when only top OR bottom match need lines
tab.r
:attr{rowspan = maxSpan(p.unit, r, rows)}
:cssText(col.t == 2 and
p:saveStr('topRight', 'border-width:', tab.line[2], ' 0 0;border-style:solid')
or col.t == 1 and (nodeFunc.bridge.lay[c]
p:saveStr('right', ';border-right:', p.reuseStr.solid)
or 'vertical-align:bottom'
Line 689 ⟶ 758:
or nil
)
:node(col.t == 1 and interval > 0 and not nodeFunc.bridge.lay[c]
rowNum[r + (p.branch_upwards and (4 - bump) or p.unit)]:tag'td'
:attr{rowspan = maxSpan(p.unit, r + p.unit, rows)}
:cssText(col.t == 1 and
p:saveStr('bttmRght', 'border-width:0 ', tab.line[2], ' 0;border-style:solid')
or col.t == 2 and (nodeFunc.bridge.lay[c]
p:saveStr('right', ';border-right:', p.reuseStr.solid)
or 'vertical-align:top'
Line 700 ⟶ 769:
or nil
)
:node(col.t == 2 and interval ~= 2 and not nodeFunc.bridge.lay[c]
end
col.t = {
Line 715 ⟶ 784:
tab.r
:attr{rowspan = maxSpan(p.unit * 2, r, rows)}
:css(nodeFunc.bridge.lay[c]
{['border-right'] = p.reuseStr.solid}
or {}
Line 749 ⟶ 818:
end
--[[local standard = {
'beta' = {
bold_winner = 'high',
omit_blanks = 'yes',
auto_3rd = 'yes'
}
}--]]
function p.main(frame, columns)
local args = require'Module:Arguments'.getArgs(frame, {trim = false})
args.columns = args.columns or columns
return p._main(args)
end
function p.seed(frame)
local parent = frame:getParent() or frame
local function arg(k, alt)
return parent.args[k] or frame.args[k] or alt
end
local padding, width = arg(2, p.teamBoxPadding()), arg(3, arg('widescore') and 40 or 30)
padding = tonumber(padding) and tonumber(padding) .. 'px' or padding
width = tonumber(width) and tonumber(width) .. 'px' or width
return mw.html.create'div'
:css{
margin = ('-1px %s -1px -0.7ex'):format(padding, padding),
float = 'left',
['background-color'] = p.bgColor.head,
border = '1px solid #aaa',
color = p.textColor.head,
['text-align'] = 'center',
width = width
}
:wikitext(arg(1, ' '))
end
|