local p = {
RD = {
'காலிறுதிகள்',
'அரையிறுதிகள்',
'இறுதிப்போட்டி',
'மூன்றாம் இடம்'
},
bgColor = {head = '#f2f2f2', 'gold', 'silver', '#C96', '#f9f9f9'},
reuseStr = {},
saveStr = function(self, name, ...)
if not self.reuseStr[name] then
self.reuseStr[name] = table.concat{...}
end
return self.reuseStr[name]
end
}  
--Provides a convenient naming shortcut up to =
for columns = 1, 9 do
local N = math.pow(2, columns)
p['N' .. N] = function
return p.main(.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
}
local nodeFunc = {
scanPattern = function(self, args, step)
self.pattern = nil
if args[step] then
self.pattern, self.nonFunc = string.match(esc.bs:text(args[step]), '^node_function{(.-)}(.*)')
end
if self.pattern then
for k, v in pairs(esc.comma) do
self.pattern = self.pattern:gsub(k, v)
end
self.nonFunc = self.nonFunc and esc.bs:undo(self.nonFunc)
self.pattern = mw.text.split(self.pattern, '%s*,%s*')
for k, v in ipairs(self.pattern) do
local func, arg = string.match(v, '^(%w+)%(?([^%)]*)')
if func and self[func] and self[func].main then
self.pattern[k] = func
if arg then
for x, y in pairs(esc.comma) do
arg = esc.bs:undo(arg):gsub(y:gsub('%%%d', *), x:match('%)([^%(])%(') or x:gsub('\\', *))
end
self[func].arg = self[func].arg or {}
self[func].arg[m.num] = arg
end
end
end
end
return self.pattern
end,
helper = {
topBranch = function--node is top of fork if top is 0
return (m.num - col.top) % 2
end,
addText = function(text)
if text and text ~= '' then
tab.r:wikitext(text)
end
end
},
line = {--this node is omitted and replaced with a line
main = function(x)
local h = p.getNodeFunc
if m.available then
local text, topId, isTop, notTop = h.line.arg[m.num] or '', h.topBranch
isTop = topId == 0
notTop = {[isTop and 1 or 0] = p.reuseStr.solid}
for k = 0, 1 do
tab.r = rowNum[m.r + k * 4]:tag'td'
:css(notTop[k] and
{[isTop and '-top' or '-bottom'] = notTop[k]}
or {}
:attr{
rowspan = ({[0] = 4, 2})[k],
colspan = p.colspan
}
h.addText(text or h.nonFunc)
text = nil
end
m.available = false
else
return nil
end
return x
end
},
bridge = {--Draw a line to the neighboring node in the same column that is not connected to the current node
main = function(x)
local h = p.getNodeFunc
h.bridge.lay[col.c][m.num - col.top + 1 + (h.topBranch == 1 and 1 or -1)] = true
h.addText(nonFunc)
return x
end,
lay = setmetatable({}, p.nilAsTab)
},
canvas = {--Merges all cells in node. Content will be the next parameter.
main = function(x)
local h = p.getNodeFunc
if m.available then
tab.r = rowNum[m.r]:tag'td'
:attr{
rowspan = 6,
colspan = p.colspan
}
h.addText(h.nonFunc)
m.available = false
return x
else
return nil
end
end
},
orphan = {--sets a flag for skipMatch to be set by p._main
main = function(x)
p.getNodeFunc.orphan.num = m.num
return x
end
},
skipAllowed = {--table of supported node functions when node is skipped (i.e. by skipmatch)
bridge = true,
canvas = true
}
}  
setmetatable(nodeFunc.helper, {__index = nodeFunc})
function p.getNodeFunc
return nodeFunc.helper
end  
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'
:node(first)
if bodyRow then
table.insert(rowNum, bodyRow, tab.r)
if p.flex_tree.merge then
rowNum[bodyRow].first = first
rowNum[bodyRow].first.unchanged = true
end
end
end  
function drawHead(text, row3rd)
local td = (row3rd and rowNum[row3rd]:tag'td':attr{rowspan = 2}
or head.row:tag'td')
:attr{colspan = p.colspan}
if text ~= 'omit_label' then
td:wikitext(text):css{
['text-align'] = '',
= ' solid #aaa',
background = p.bgColor.head
}
end
end  
function spacer(width)
tab.r:tag'td'
:attr{width = width}
:wikitext(p.no_column_head and '' or '&nbsp;')
end  
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.dpBoxEmpty or mw.clone(p.dpBoxBase):wikitext(p.flex_tree.wt)
rowNum[r]:node(p.dpBoxEmpty)
else
rowNum[r]:node(mw.clone(p.dpBoxBase):wikitext(v))
end
end  
p.scoreWasher = {
numberFormat = '%-?%d+%.?%d*',
main = function (self, s)
if s then
for _, cycle in ipairs(self.cycles) do
s = s:gsub(unpack(cycle))
end
if p.scoreSumBox and self.plus then
local t = 0
for _, part in ipairs(mw.text.split(s, self.plus)) do
t = t + (tonumber(part:match('%-?%d+%.?%d*')) or 0)
end
return t
end
return tonumber(s:match(self.numberFormat)) or math.huge
end
return 0
end,
spin = function(self, v)
table.insert(self, v)
return self
end,
load = function (self, cycle)
local wash, rinse = 0, {spin = self.spin}
for v in cycle:gfind('%(([^%(%)]-)%)') do
if v == '_plus_' then
self.plus = v
rinse:spin(v)
cycle = cycle:gsub('%(_plus_%)', '', 1)
else
wash = wash + 1
rinse:spin('%'):spin(wash)
end
end
table.insert(self.cycles, {esc.bs:undo(cycle, '%%'), table.concat(rinse)})
end,
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
end,
sum = function (clean)
local sum = {0, 0}
for _, box in ipairs(clean) do
for team, score in ipairs(box) do
sum[team] = sum[team] + score
end
end
return unpack(math.max(unpack(sum)) == math.huge and {'&mdash;', '&mdash;'} or sum)
end
}  
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  
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 = { = ' solid #aaa', background = p.bgColor[4]}
function teamBox(v, r, f)
if p.flex_tree.merge and not v and f.phase == 2 then
for i = -2, 0 do
if rowNum[r + i].first.unchanged then
rowNum[r + i].first.unchanged = nil
rowNum[r + i].first:node(p.unflex_div)
end
end
tab.r:attr{rowspan = 4}:css{['vertical-align'] = ''}
else
if not p.bold then
--backwards compatability (wikitemplates bold each arg individually)
local hasBold, b = tostring(v):gsub("([^']*)'''([^']*)", '%1%%s%2')
if b == 1 then
v = hasBold:format('<b>')
end
end
local cell
if f[1] then
cell = f.sumBox and f.sumBox[1] and
{padding = f.sumBox[1]}
or {['-'] = f.borderLeft}
cell['text-align'] = v and f[1]
else
cell = p.teamBoxPadTab
end
tab.r = rowNum[r]:tag'td'
:css(p.teamBoxCSS)
:css(cell)
:attr{rowspan = 2}
:node(mw.html.create(f.bold and 'b'):wikitext(v or f[1] and '' or '&nbsp;'))
end
end  
function p._main(args)
function args:clean(key, params)--prevent html comments from breaking named args and reduces repeat concatenation
params = params or {}
local clean = args[key] or params.ifNil
if clean then
params.append = params.append or ''
clean = mw.text.decode(clean):gsub('<!%-.-%->', *):gsub(params.pattern or '[^%w-;%.]', *) .. params.append
clean = clean ~= params.append and clean or params.ifNil
end