local p = {}
local entries = {}
local pathEntries = {}
local pathcolor
local shift = {}
local hascross = {}
local teams_per_match = {}
local rlegs = {}
local maxlegs = {}
local autolegs
local byes = {}
local hide = {}
local matchgroup = {}
local autocol
local seeds
local forceseeds
local nowrap
local boldwinner
local aggregate
local paramstyle
local masterindex  
local function isempty(s)
return s==nil or s==''
end  
local function notempty(s)
return s~=nil and s~=''
end  
local function bargs(s)
return pargs[s] or fargs[s]
end  
local function toChar(num)
return string.char(string.byte("a")+num-1)
end  
local function split(str,delim,tonum)
result = {};
local a = "[^"..table.concat(delim).."]+"
for w in str:gmatch(a) do
if tonum==true then
table.insert(result, tonumber(w));
else
table.insert(result, w);
end
end
return result;
end  
local function getWidth(ctype, default)
local result = bargs(ctype..'-width')
if isempty(result) then return default end
if tonumber(result)~=nil then return result..'px' end
return result
end  
local function matchGroups
for j=minc,c do
matchgroup[j]={}
for i=1,r do
if entries[j][i]~= nil and entries[j][i]['ctype']=='team' then
matchgroup[j][i]=math.ceil(entries[j][i]['index']/teams_per_match[j])
entries[j][i]['group'] = math.ceil(entries[j][i]['index']/teams_per_match[j])
end
end
end
end  
local function teamLegs(j,i)
local legs = rlegs[j]
if notempty(entries[j][i]['legs']) then
legs = tonumber(entries[j][i]['legs'])
end
if autolegs then
local l=1
repeat l=l+1
until isempty(entries[j][i]['score'][l])
legs = l-1
end
return legs
end  
local function boldWinner
local function boldScore(j,i,l)
if entries[j][i]~= nil and entries[j][i]['ctype']=='team' then
local myscore = entries[j][i]['score'][l]:gsub('%W','')
if myscore == "" or myscore:find("%D") then return 'normal'
else myscore=tonumber(myscore) end
local compscore = {}
for k,v in pairs(matchgroup[j]) do
if matchgroup[j][i]==v and k~=i then
local theirscore = entries[j][k]['score'][l] or ''
theirscore = theirscore:gsub('%W','')
if theirscore== "" or theirscore:find("%D") then return 'normal'
else table.insert(compscore,tonumber(theirscore)) end
end
end
for k,v in pairs(compscore) do
if myscore<=v then return 'normal' end
end
if l~='agg' then
entries[j][i]['wins'] = entries[j][i]['wins']+1
else
entries[j][i]['aggwins'] = 1
end
return 'bold'
end
end
local function boldTeam(j,i,agg)
local wins
local legs = teamLegs(j,i)
if agg~=true then
wins = 'wins'
if entries[j][i][wins]>legs/2 then
return 'bold'
end
if autolegs then
for l=1,legs do
if notempty(entries[j][i]['score'][l]) and string.find(entries[j][i]['score'][l],"nbsp") then
return 'normal'
end
end
else
for l=1,legs do
if isempty(entries[j][i]['score'][l]) or string.find(entries[j][i]['score'][l],"nbsp") then
return 'normal'
end
end
end
else
wins = 'aggwins'
end  
local compteam = {}
for k,v in pairs(matchgroup[j]) do
if matchgroup[j][i]==v and k~=i then
table.insert(compteam,tonumber(entries[j][k][wins]))
end
end
for k,v in pairs(compteam) do
if entries[j][i][wins]<=v then
return 'normal'
end
end
return 'bold'
end
for j=minc,c do
for i=1,r do
if entries[j][i]~= nil and entries[j][i]['ctype']=='team' then
entries[j][i]['wins'] = 0
entries[j][i]['aggwins'] = 0
end
end
for i=1,r do
if entries[j][i]~= nil and entries[j][i]['ctype']=='team' then
local legs = teamLegs(j,i)
for l=1,legs do
entries[j][i]['score']['weight'][l] = boldScore(j,i,l)
end
if aggregate and legs>1 then
entries[j][i]['score']['weight']['agg'] = boldScore(j,i,'agg')
end
end
end
for i=1,r do
if entries[j][i]~= nil and entries[j][i]['ctype']=='team' then
local agg
local legs = teamLegs(j,i)
if aggregate and legs>1 then agg=true end
entries[j][i]['weight'] = boldTeam(j,i,agg)
end
end
end
end  
local function isBlankEntry(col,row,ctype)
if isempty(entries[col][row]) then return true end
if isempty(entries[col][row]['team']) and isempty(entries[col][row]['text']) then return true end
return false
end  
local function showSeeds(j,i)
local showseed=false
if forceseeds or notempty(entries[j][i]['seed']) then
showseed=true
else
for k=1,teams_per_match[j]-1 do
if notempty(entries[j][i+2*k]) and entries[j][i]['group']==entries[j][i+2*k]['group'] and notempty(entries[j][i+2*k]['seed']) then
showseed=true
end
if notempty(entries[j][i-2*k]) and entries[j][i]['group']==entries[j][i-2*k]['group'] and notempty(entries[j][i-2*k]['seed']) then
showseed=true
end
end
end
return showseed
end  
local function blankCell(tbl,j,i,rowspan,colspan,text,align)
local cell = tbl:tag('td')
if colspan~=1 then
cell:attr('colspan',colspan)
end
if rowspan~=1 then
cell:attr('rowspan',rowspan)
end
if notempty(align) then
cell:css('text-align',align)
end
if notempty(text) then
if nowrap then
cell:css('white-space', 'nowrap')
end
cell:wikitext(text)
end
return cell
end  
local function headerCell(tbl,j,i,colspan)
local cell = tbl:tag('td')
:attr('rowspan',2)
:css('text-align','')
:css('',' solid #aaa')
:css('background-color',entries[j][i]['shade'])
:wikitext(entries[j][i]['header'])
if colspan~=1 then
cell:attr('colspan',colspan)
end
if nowrap then
cell:css('white-space', 'nowrap')
end
if entries[j][i]['position']=='top' and entries[j][i-2]~=nil and entries[j][i-2]['ctype']~='blank' and entries[j][i-2]['ctype']~='text' and entries[j][i-2]['ctype']~='group' and (not byes[j][entries[j][i]['headerindex']] or not isBlankEntry(j,i-2)) then
cell:css('-top',' solid #aaa')
end
return cell
end  
local function teamCell(tbl,k,j,i,l,colspan)
local bg = '#F2F2F2'
local cell = tbl:tag('td')
:attr('rowspan',2)
:css('',' solid #aaa')
if colspan~=1 and colspan~=nil then
cell:attr('colspan',colspan)
end
if k=='seed' or k=='score' then
cell:css('text-align','')
end
if k=='seed' or k=='score' or nowrap then
cell:css('white-space', 'nowrap')
end
if k=='seed' then
cell:css('-',' solid #aaa')
else
bg='#F9F9F9'
end
if k=='team' then
cell:css('padding-','0.3em')
end
if l=='agg' then
cell:css('-',' solid #aaa')
end
if (l==nil and entries[j][i]['weight']=='bold') or entries[j][i]['score']['weight'][l]=='bold' then
cell:css('font-weight','bold')
end
if entries[j][i]['position']=='top' and entries[j][i-2]~=nil and entries[j][i-2]['ctype']~='blank' and entries[j][i-2]['ctype']~='text' and entries[j][i-2]['ctype']~='group' and (not byes[j][entries[j][i]['headerindex']] or not isBlankEntry(j,i-2)) then
cell:css('-top',' solid #aaa')
end
cell:css('background-color',bg)
if l==nil then
cell:wikitext(entries[j][i][k])
else
cell:wikitext(tostring(entries[j][i][k][l]))
end
return cell
end  
local function insertEntry(tbl,j,i)
local entry_colspan=maxlegs[j]+2
if not seeds then entry_colspan=entry_colspan-1 end
if (aggregate and maxlegs[j]>1) or maxlegs[j]==0 then
entry_colspan=entry_colspan+1
end  
if entries[j][i]~=nil and entries[j][i]['ctype']=='blank' then
return
end  
if entries[j][i]==nil then
if entries[j][i-1]~=nil or i==1 then
local rowspan = 0
local row = i
repeat
rowspan=rowspan+1
row=row+1
until entries[j][row]~=nil or row>r
return blankCell(tbl,j,i,rowspan,entry_colspan)
else
return
end
end  
if entries[j][i]['ctype']=='header' then
if byes[j][entries[j][i]['headerindex']] then
local emptyround = true
local row = i+1
repeat
if not isBlankEntry(j,row) then
emptyround = false
end
row = row+1
until (entries[j][row]~=nil and entries[j][row]['ctype']=='header') or row>r
if emptyround == true then
return blankCell(tbl,j,i,2,entry_colspan)
end
end  
if hide[j][entries[j][i]['headerindex']] then
return blankCell(tbl,j,i,2,entry_colspan)
end  
if isempty(entries[j][i]['header']) then
if entries[j][i]['headerindex']==1 then
if j==c then entries[j][i]['header'] = 'Final'
elseif j==c-1 then entries[j][i]['header'] = 'Semifinals'
elseif j==c-2 then entries[j][i]['header'] = 'Quarterfinals'
else entries[j][i]['header'] = 'Round '..j
end
else
entries[j][i]['header'] = 'Lower round '..j
end
end  
return headerCell(tbl,j,i,entry_colspan)
end  
if entries[j][i]['ctype']=='team' then