if notempty(entries[j+1][stop-1]) and (byes[j+1][entries[j+1][stop-1]['headerindex']] and isBlankEntry(j+1,stop-1) and isBlankEntry(j+1,stop+1) or hide[j+1][entries[j+1][stop-1]['headerindex']])then
if bargs('show-bye-paths')~='y' and bargs('show-bye-paths')~='yes' then
hidepath=true
end
end
if bargs('RD'..j..'-RD'..(j+1)..'-path')=='n' or bargs('RD'..j..'-RD'..(j+1)..'-path')=='no' or bargs('RD'..j..'-RD'..(j+1)..'-path')=='0' then
if notempty(entries[j][start-1]) and entries[j][start-1]['headerindex']==1 then
hidepath=true
end
end  
local cross = 0
for n=1,#mid do
if (start<stop and mid[n]<stop and mid[n]>start) or (start>stop and mid[n]>stop and mid[n]<start) then
hascross[j]=true
cross = mid[n]
end
end
if not hidepath then
if start==stop then
if start>r then break end
start = math.min(start,r)
pathEntries[color][j][start]['out'][1] = 1
pathEntries[color][j][start]['in'][1] = 1
if cross==0 then
pathEntries[color][j][start]['mid'][1] = 1
end
elseif start<stop then
if stop>r then break end
pathEntries[color][j][start]['out'][1] = 1
pathEntries[color][j][stop]['in'][1] = 1
pathEntries[color][j][stop]['in'][2] = 1
if color~='black' then
pathEntries[color][j][start]['out'][3] = 'below'
pathEntries[color][j][stop+1]['in'][3] = 'above'
end
if cross==0 then
pathEntries[color][j][start]['mid'][1] = 1
pathEntries[color][j][start]['mid'][3] = 'below'
for i=start+1,stop-1 do pathEntries[color][j][i]['in'][2] = 1 end
else
pathEntries[color][j][cross]['cross'][1] = 1
for i=start+1,cross-1 do pathEntries[color][j][i]['out'][2] = 1 end
for i=cross+2,stop-1 do pathEntries[color][j][i]['in'][2] = 1 end
end
elseif start>stop then
if start>r then break end
pathEntries[color][j][stop]['in'][1] = 1
if cross==0 then
for i=stop+1,start-1 do pathEntries[color][j][i]['in'][2] = 1 end
pathEntries[color][j][start]['out'][1] = 1
pathEntries[color][j][start]['mid'][1] = 1
pathEntries[color][j][start]['in'][2] = 1
if color~='black' then
pathEntries[color][j][start+1]['out'][3] = 'above'
pathEntries[color][j][start+1]['mid'][3] = 'above'
pathEntries[color][j][stop]['in'][3] = 'below'
end
else
pathEntries[color][j][cross]['cross'][2] = 1
for i=stop+1,cross-1 do pathEntries[color][j][i]['in'][2] = 1 end
for i=cross+2,start-1 do pathEntries[color][j][i]['out'][2] = 1 end
pathEntries[color][j][start]['out'][1] = 1
pathEntries[color][j][start]['out'][2] = 1
if color~='black' then
pathEntries[color][j][start+1]['out'][3] = 'above'
pathEntries[color][j][stop]['in'][3] = 'below'
end
end
end
end
end
end
for j=minc,c-1 do
for i=1,r do
local empty = true
for key,_ in pairs(pathEntries) do
for k,v in pairs(pathEntries[key][j][i]) do
if v[1]~=0 or v[2]~=0 or v[3]~=nil then
empty = false
end
end
if empty then pathEntries[key][j][i] = nil end
end
end
end
for j=minc,c-1 do
if notempty(pathEntries['black'][j]) and notempty(pathEntries['black'][j+1]) then
for i=1,r do
if (entries[j+1][i-1]==nil or (byes[j+1][entries[j+1][i-1]['headerindex']]) and isBlankEntry(j+1,i-1)) and notempty(pathEntries['black'][j][i]) and notempty(pathEntries['black'][j+1][i]) then
if pathEntries['black'][j][i]['in'][1]==1 and pathEntries['black'][j+1][i]['out'][1]==1 then
entries[j+1][i-1]={['ctype']='line'}
entries[j+1][i]={['ctype']='blank'}
end
end
end
end
end
end  
local function getGroups
local function check(j,i)
local result=false
if entries[j][i] == nil then
if entries[j][i+1] == nil then
result=true
elseif entries[j][i+1]['ctype']=='text' and isBlankEntry(j,i+1) then
result=true
end
elseif entries[j][i]['ctype']=='text' and isBlankEntry(j,i) then
result=true
end
return result
end
for j=minc,c-1 do
if teams_per_match[j]==2 then
local n=0
for i=1,r do
if (notempty(pathEntries['black'][j][i]) and pathEntries['black'][j][i]['in'][1] == 1) or (notempty(pathEntries['color'][j][i]) and pathEntries['color'][j][i]['in'][1] == 1) then
n=n+1
if check(j,i) then
local k=minc-1
repeat
if entries[j-k][i+1]~=nil and entries[j-k][i+1]['ctype']=='text' and isBlankEntry(j-k,i+1) then
entries[j-k][i+2]=nil
end
entries[j-k][i]={['ctype']='blank'}
entries[j-k][i+1]={['ctype']='blank'}
if k>0 and isempty(pathEntries['black'][j-k][i]) and isempty(pathEntries['color'][j-k][i]) then
pathEntries['black'][j-k][i] = 'blank'
pathEntries['black'][j-k][i+1] = 'blank'
end
k=k+1
until k>j-1 or not check(j-k,i) or notempty(pathEntries['black'][j-k][i]) or notempty(pathEntries['color'][j-k][i])
k=k-1
entries[j-k][i]={['ctype']='group',['index']=n,['colspan']=k+1}
entries[j-k][i+1]={['ctype']='blank'}
entries[j-k][i]['group'] = bargs('RD'..j..'-group'..n)
end
end
end
end
end
end  
local function getCells
local maxrow = 1
local colentry = {}
local bool = true
for j=minc,c do
if notempty(fargs['col'..j..'-headers']) then bool=false end
teams_per_match[j] = tonumber(fargs['RD'..j..'-teams-per-match']) or tonumber(fargs['col'..j..'-teams-per-match']) or tonumber(fargs['teams-per-match']) or 2
maxtpm = math.max(maxtpm,teams_per_match[j])
end
for j=minc,c do
entries[j] = {}
shift[j] = tonumber(bargs('RD'..j..'-shift')) or tonumber(bargs('shift')) or 0
colentry[j] = {
split((fargs['col'..j..'-headers'] or ''):gsub("%s+", ""),{","},true),
split((fargs['col'..j..'-matches'] or ''):gsub("%s+", ""),{","},true),
split((fargs['col'..j..'-lines'] or ''):gsub("%s+", ""),{","},true),
split((fargs['col'..j..'-text'] or ''):gsub("%s+", ""),{","},true),
}
if bool==true and fargs['noheaders']~='y' and fargs['noheaders']~='yes' then
table.insert(colentry[j][1],1)
end
end  
for j=minc,c do
local textindex=0
for k,v in ipairs(colentry[j]) do
table.sort(colentry[j][k])
local ctype
if k==1 then ctype='header'
elseif k==2 then ctype='team'
elseif k==3 then ctype='line'
elseif k==4 then ctype='text'
elseif k==5 then ctype='group'
end
for n=1,#colentry[j][k] do
if shift[j]~=0 and colentry[j][k][n]>1 then
colentry[j][k][n] = colentry[j][k][n]+shift[j]
end
local i=2*colentry[j][k][n]-1
maxrow = math.max(i+2*teams_per_match[j]-1,maxrow)
if ctype=='team' then
if entries[j][i-1]==nil and entries[j][i-2]==nil then
entries[j][i-2]={['ctype']='text',['index']=n}
entries[j][i-1]={['ctype']='blank'}
textindex=n
end
entries[j][i]={['ctype']=ctype,['index']=teams_per_match[j]*n-(teams_per_match[j]-1),['position']='top'}
entries[j][i+1]={['ctype']='blank'}
for m=2,teams_per_match[j] do
entries[j][i+2*(m-1)]={['ctype']=ctype,['index']=teams_per_match[j]*n-(teams_per_match[j]-m)}
entries[j][i+2*(m-1)+1]={['ctype']='blank'}
end
elseif ctype=='text' then
entries[j][i]={['ctype']=ctype,['index']=textindex+n}
entries[j][i+1]={['ctype']='blank'}
elseif ctype=='line' then
entries[j][i]={['ctype']=ctype}
entries[j][i+1]={['ctype']='blank'}
elseif ctype=='group' then
entries[j][i]={['ctype']=ctype,['index']=n}
entries[j][i+1]={['ctype']='blank'}
else
entries[j][i]={['ctype']=ctype,['index']=n,['position']='top'}
entries[j][i+1]={['ctype']='blank'}
end
end
end
end
if isempty(r) then
r = maxrow
end
end  
function p.main
fargs = .args
pargs = :getParent.args;  
r = tonumber(fargs.rows) or ''
c = tonumber(fargs.rounds) or 1
maxc = tonumber(pargs.maxrounds) or tonumber(pargs.maxround) or ''
minc = tonumber(pargs.minround) or 1
headerindex = {}
if notempty(maxc) then c=maxc end
if fargs.autocol=='yes' or fargs.autocol=='y' then autocol=true end
local colspacing = tonumber(fargs['col-spacing']) or 5  
maxtpm = 1
seeds = true
forceseeds = false
boldwinner = bargs('boldwinner') or ''
if bargs('seeds')=='y' or bargs('seeds')=='yes' then forceseeds=true end
if bargs('seeds')=='n' or bargs('seeds')=='no' then seeds=false end
if bargs('aggregate')=='y' or bargs('aggregate')=='yes' then aggregate=true end
if bargs('autolegs')=='y' or bargs('autolegs')=='yes' then autolegs=true end
if bargs('nowrap')=='y' or bargs('nowrap')=='yes' then nowrap=true end
if bargs('paramstyle')=='numbered' then
param
else
param
end  
getCells
getAltIndices
assignParams
matchGroups
if (boldwinner=='yes' or boldwinner=='y' or boldwinner=='high') then boldWinner end
getPaths
if minc==1 then
getGroups
end  
for j=minc,c do