local p = {}
local getArgs = require('Module:Arguments').getArgs
function p.main(frame)
local input={{},{},{},{},{},{}}
local format={{{},{},{},{},{},{},{},{},{}},{{},{},{},{},{},{},{},{},{}},{{},{},{},{},{},{},{},{},{}},{{},{},{},{},{},{},{},{},{}},{{},{},{},{},{},{},{},{},{}},{{},{},{},{},{},{},{},{},{}},{{},{},{},{},{},{},{},{},{}}}
local line = 0
local column = 1
local output = ''
local op = frame:expandTemplate{title='color', args={'red', '片头曲(OP)'}}
local ed = frame:expandTemplate{title='color', args={'blue', '片尾曲(ED)'}}
local args = getArgs(frame)
for init=1,54,1 do
if frame.args[init] == 'OP' or frame.args[init] == 'op' or frame.args[init] == 'ED' or frame.args[init] == 'ed' then
line = line+1
column = 1
input[line][10] = 0
else
column = column+1
end
input[line][column] = args[init]
end
for m=1,6,1 do
for n=1,9,1 do
if input[m][n] then
local link = mw.ustring.match(input[m][n],'%[%[(.*)%]%]')
local ref = mw.ustring.find(input[m][n],'<ref')
if ref then
format[m][n][4] = 1
input[m][n] = mw.ustring.sub(input[m][n],1,ref-1)
if link then
format[m][n][3] = 1
input[m][n] = link
end
else
if link then
format[m][n][3] = 1
input[m][n] = link
end
end
end
end
end
for i=1,6,1 do
for j=1,9,1 do
format[i][j][1] = 1
format[i][j][2] = 1
if input[i][j] then
for a= 1,8,1 do
if input[i][j+a] == input[i][j] then
format[i][j][1]=format[i][j][1]+1
input[i][j+a] = nil
else
break
end
end
end
end
end
for p=1,6,1 do
for q=1,9,1 do
if input[p][q] then
for b= 1,6,1 do
if input[p+b][q] == input[p][q] and format[p+b][q][1] == format[p][q][1] then
format[p][q][2]=format[p][q][2]+1
input[p+b][q] = nil
else
break
end
end
end
end
end
for f=1,6,1 do
for g=1,9,1 do
if input[f][g] then
if format[f][g][3] == 1 or format[f][g][4] == 1 then
input[f][g] = frame.args[9*(f-1)+g]
end
end
end
end
input[2][5] = frame.args[14]
for x=1,6,1 do
for y=1,9,1 do
if input[x][y] == 'OP' or input[x][y] == 'op'then
if format[x][y][2]~=1 then
output = output..'|-\n!rowspan='..format[x][y][2]..'|'..op..'\n'
else
output = output.."|-\n!"..op..'\n'
end
end
if input[x][y] == 'ED' or input[x][y] == 'ed' then
if format[x][y][2]~=1 then
output = output..'!rowspan='..format[x][y][2]..'|'..ed..'\n'
else
output = output.."!"..ed..'\n'
end
end
if input[x][y] and input[x][y] ~= 'OP' and input [x][y] ~='ED' and input[x][y] ~= 'op' and input[x][y] ~= 'ed' and input[x][y+1] ~=0 then
if format[x][y][1] == 1 then
if format[x][y][2] == 1 then
output = output..'||'..input[x][y]
else
output = output..'||rowspan='..format[x][y][2]..' style=\"text-align:center\"|'..input[x][y]
end
else
if format[x][y][2] == 1 then
output = output..'||colspan='..format[x][y][1]..' style=\"text-align:center\"|'..input[x][y]
else
output = output..'||colspan='..format[x][y][1]..' rowspan='..format[x][y][2]..' style=\"text-align:center\"|'..input[x][y]
end
end
end
if input[x][y+1] == 0 then
if input[x][y] then
if format[x][y][2] == 1 then
output = output..'||'..input[x][y]..'\n|-\n'
else
output = output..'||rowspan='..format[x][y][2]..' style=\"text-align:center\"|'..input[x][y]..'\n|-\n'
end
else
output = output..'\n|-\n'
end
end
end
end
return output
end
return p