local getArgs = require('Module:Arguments').getArgs;
local yesno = require('Module:Yesno');
local p = {}

local term = '落客-{zh-hans:站台;zh-hant:月台}-'
local dest = '終點站'

local function station(data, stn)
	if stn == '' then return '' end
	if stn == 'dest' or mw.ustring.match(stn, '[終终][点點]站') then return dest end
	if stn == 'term' or mw.ustring.match(stn, '落客[站月]台'  ) then return term end
	if mw.ustring.match(stn, '[内內外][圈環环]') then return stn end
	local dF = data['station format'][stn] or data['station format'][1]
	-- if format ~= nil then
	dF = mw.ustring.gsub(dF, '%%1', stn)
	local str = dF:find '|' and dF or string.format('[[%s|%s]]', dF, stn)
	return str
	-- else return stn
	-- end
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(frame, args)
end

function p._main(frame, args)
	if not (args.g1 or args['組1'] or args['组1']) then
		return ''
	end
	local astyle = args[ 'style' ] or ''
	local out = mw.html.create 'table'
		:addClass( "station-structure-platform" )
	
	local system = args[ 'system' ] or args[ 's' ] or args[ '系統' ] or args[ '系统' ]
	local data = mw.loadData( 'Module:Adjacent stations/' .. system );
	
	local complex = yesno( args[ 'complex' ] or args[ '進階' ] or args[ '进阶' ] )
	
	local prepend, append = false, false
	local haspno = false
	for i = 1, 30, 1 do
		local g = args['g' .. i] or args['組' .. i] or args['组' .. i] or ''
		local group = ""
		if     mw.ustring.match(g, '[p台]')   ~= nil then group = "platform" 
		elseif mw.ustring.match(g, '[t軌轨]') ~= nil then group = "track"
		elseif mw.ustring.match(g, '[x文]')   ~= nil then group = "text" end
			
		if args[ 'pre' .. i ] then prepend = true end
		if args[ 'ap'  .. i ] then append  = true end
		if (group == "track" and args[ 'p' .. i ]) or args[ '台' .. i ]
			or args[ 'pu' .. i ] or args[ '上台' .. i ] or args[ '上号' .. i ]
			or args[ 'pd' .. i ] or args[ '下台' .. i ] or args[ '下号' .. i ] then
				haspno = true
		end
	end
	
	for i = 1, 30, 1 do
		local g = args['g' .. i] or args['組' .. i] or args['组' .. i] or ''
		local group = ""
		if     mw.ustring.match(g, '[p台]')   ~= nil then group = "platform" 
		elseif mw.ustring.match(g, '[t軌轨]') ~= nil then group = "track"
		elseif mw.ustring.match(g, '[x文]')   ~= nil then group = "text" end
		
		if group ~= "" then
			local style = args['style' .. i] or ''
			local future = mw.ustring.match(g, '[f來来]') ~= nil
			
			local tr = out:tag 'tr'
			if future then style = style .. 'opacity:60%;' end
			local border = {
				plt  = 'solid 2px var(--border-color-notic,black)';
				pltx = 'solid 2px var(--border-color-notic,black)';
				trk  = 'solid 1px var(--border-color-muted,lightgray)'
			}
			
			local text = args[ 't' .. i ] or args[ '文' .. i ] or ''
			
			local prep = args[ 'pre' .. i ] or ''
			local pres = args[ 'pre' .. i .. 's' ] or ''
			if prepend then tr:tag 'td':cssText( pres ) :wikitext( prep ) end
			
			if group == "platform" then
				local bg = args[ 'bg' .. i ] or args[ '背' .. i ]
				bg = (bg or '#f9f9f9')
				local p = args[ 'p' .. i ] or args[ '類' .. i ] or args[ '类' .. i ] or ''
				local type
				
				if     string.find(p, 'side')        ~= nil
					or mw.ustring.match(p, '[側侧]') ~= nil then type = "side" 
				elseif string.find(p, 'split')       ~= nil
					or mw.ustring.match(p, '[離离]') ~= nil then type = "split"
				elseif string.find(p, 'narrow')      ~= nil
					or mw.ustring.match(p, '[窄]')   ~= nil then type = "narrow"
				elseif string.find(p, 'island')      ~= nil
					or mw.ustring.match(p, '[島岛]') ~= nil then type = "island"
				elseif string.find(p, 'bay')         ~= nil
					or mw.ustring.match(p, '[灣湾]') ~= nil then type = "bay"
				elseif string.find(p, 'wide')        ~= nil
					or mw.ustring.match(p, '[闊阔]') ~= nil then type = "wide"
				else                                        type = p        end
				
				local bt = args[ 'bt' .. i ] or args[ '上邊框' .. i ] or args[ '上边框' .. i ]
				local bb = args[ 'bb' .. i ] or args[ '下邊框' .. i ] or args[ '下边框' .. i ]
				local bl = args[ 'bl' .. i ] or args[ '左邊框' .. i ] or args[ '左边框' .. i ]
				local br = args[ 'br' .. i ] or args[ '右邊框' .. i ] or args[ '右边框' .. i ]
				
				local maintd
				local maintdwkt = {}
				
				if type == "island" or type == "bay" or type == "wide" then
					bt = (bt or border.plt)
					bb = (bb or border.plt)
					bl = (bl or border.pltx)
					br = (br or border.pltx)
					if complex then
						tr:tag 'td'
							:css( 'border-top'   , bt )
							:css( 'border-left'  , bl )
							:css( 'border-bottom', bb )
							:css( 'background'   , bg )
						
						local line_u = data.lines[ args[ 'l' .. i-1 ]
										or args[ '綫' .. i-1 ] or args[ '線' .. i-1 ] or args[ '线' .. i-1 ]
										or '' ] or ''
						local pu = args[ 'pd' .. i-1 ] or args[ '下台' .. i-1 ] or ''
						local line_color_u = '#' .. (line_u.color or '000000')
						local rgb_u = { tonumber(string.sub(line_color_u, 2, 3), 16), tonumber(string.sub(line_color_u, 4, 5), 16), tonumber(string.sub(line_color_u, 6, 7), 16) }
						local text_color_u = (rgb_u[1]*0.299 + rgb_u[2]*0.587 + rgb_u[3]*0.114) > 186 and "black" or "white"
						-- out = out .. '<!-- ' .. rgb[1] .. ',' .. rgb[2] .. ',' .. rgb[3] .. ' -->'
						local pno_u = frame:expandTemplate{ title = '圆形编号', args = {pu, bg = line_color_u, fg = text_color_u} }
						
						local line_d = data.lines[ args[ 'l' .. i+1 ]
										or args[ '綫' .. i+1 ] or args[ '線' .. i+1 ] or args[ '线' .. i+1 ]
										or '' ] or ''
						local pd = args[ 'pu' .. i+1 ] or args[ '上台' .. i+1 ] or ''
						local line_color_d = '#' .. (line_d.color or '000000')
						local rgb_d = { tonumber(string.sub(line_color_d, 2, 3), 16), tonumber(string.sub(line_color_d, 4, 5), 16), tonumber(string.sub(line_color_d, 6, 7), 16) }
						local text_color_d = (rgb_d[1]*0.299 + rgb_d[2]*0.587 + rgb_d[3]*0.114) > 186 and "black" or "white"
						-- out = out .. '<!-- ' .. rgb[1] .. ',' .. rgb[2] .. ',' .. rgb[3] .. ' -->'
						local pno_d = frame:expandTemplate{ title = '圆形编号', args = {pd, bg = line_color_d, fg = text_color_d} }
						
						tr:tag 'td'
							:css( 'border-top'   , bt )
							:css( 'border-bottom', bb )
							:css( 'background'   , bg )
							:wikitext( pno_u .. '<br/>' .. pno_d )
						maintd = tr:tag 'td'
					else
						maintd = tr:tag 'td':attr( 'colspan', 5 )
					end
					maintd
						:cssText( 'font-size:smaller;text-align:center;' )
						:css    ( 'background'   , bg )
						:css    ( 'border-top'   , bt )
						:css    ( 'border-bottom', bb )
						:css    ( 'border-left'  , complex and 'none' or bl     )
						:css    ( 'border-right' , complex and 'none' or br     )
						:css    ( 'height'       , complex and '3rem' or '2rem' )
					if type ~= "wide" and future then
						table.insert( maintdwkt , "未啟用" )
					end
					if     type == "island" then
						table.insert( maintdwkt ,   '[[島式月台|島式-{zh-cn:站台;zh-tw:月台;zh-hk:月台}-]]'   )
					elseif type == "bay"    then
						table.insert( maintdwkt , '[[港灣式月台|港灣式-{zh-cn:站台;zh-tw:月台;zh-hk:月台}-]]' ) end
				elseif type == "side" or type == "split" or type == "narrow" then
					-- dir = dn
					bl = (bl or border.pltx)
					br = (br or border.pltx)
					if string.find(p, 'dn') ~= nil or string.find(p, '下') then
						bt = (bt or 'none')
						bb = (bb or border.plt)
					-- dir = up
					else
						bt = (bt or border.plt)
						bb = (bb or 'none')
					end
					if complex then
						tr:tag 'td'
							:css( 'border-top'   , bt )
							:css( 'border-left'  , bl )
							:css( 'border-bottom', bb )
							:css( 'background'   , bg )
						
						if string.find(p, 'dn') ~= nil or string.find(p, '下') ~= nil then
							local line_d = data.lines[ args[ 'l' .. i+1 ]
											or args[ '綫' .. i+1 ] or args[ '線' .. i+1 ] or args[ '线' .. i+1 ]
											or '' ] or ''
							local pd = args[ 'pu' .. i+1 ] or args[ '上台' .. i+1 ] or ''
							local line_color_d = '#' .. (line_d.color or '000000')
							local rgb_d = { tonumber(string.sub(line_color_d, 2, 3), 16), tonumber(string.sub(line_color_d, 4, 5), 16), tonumber(string.sub(line_color_d, 6, 7), 16) }
							local text_color_d = (rgb_d[1]*0.299 + rgb_d[2]*0.587 + rgb_d[3]*0.114) > 186 and "black" or "white"
							-- out = out .. '<!-- ' .. rgb[1] .. ',' .. rgb[2] .. ',' .. rgb[3] .. ' -->'
							tr:tag 'td'
								:css( 'border-top'   , bt )
								:css( 'border-bottom', bb )
								:css( 'background'   , bg )
								:wikitext( frame:expandTemplate{ title = '圆形编号', args = {pd, bg = line_color_d, fg = text_color_d} } )
						else
							local line_u = data.lines[ args[ 'l' .. i-1 ]
											or args[ '綫' .. i-1 ] or args[ '線' .. i-1 ] or args[ '线' .. i-1 ]
											or '' ] or ''
							local pu = args[ 'pd' .. i-1 ] or args[ '下台' .. i-1 ] or ''
							local line_color_u = '#' .. (line_u.color or '000000')
							local rgb_u = { tonumber(string.sub(line_color_u, 2, 3), 16), tonumber(string.sub(line_color_u, 4, 5), 16), tonumber(string.sub(line_color_u, 6, 7), 16) }
							local text_color_u = (rgb_u[1]*0.299 + rgb_u[2]*0.587 + rgb_u[3]*0.114) > 186 and "black" or "white"
							-- out = out .. '<!-- ' .. rgb[1] .. ',' .. rgb[2] .. ',' .. rgb[3] .. ' -->'
							tr:tag 'td'
								:css( 'border-top'   , bt )
								:css( 'border-bottom', bb )
								:css( 'background'   , bg )
								:wikitext( frame:expandTemplate{ title = '圆形编号', args = {pu, bg = line_color_u, fg = text_color_u} } )
						end
						maintd = tr:tag 'td'
					else
						maintd = tr:tag 'td':attr( 'colspan', 5 )
					end
					maintd
						:cssText( 'height:1.25rem;font-size:smaller;text-align:center;' )
						:css    ( 'background'   , bg )
						:css    ( 'border-top'   , bt )
						:css    ( 'border-bottom', bb )
						:css    ( 'border-left'  , complex and 'none' or bl )
						:css    ( 'border-right' , complex and 'none' or br )
					if type ~= "narrow" and future then
						table.insert( maintdwkt , "未啟用" )
					end
					if     type == "side"  then 
						table.insert( maintdwkt ,  '[[側式月台|側式-{zh-cn:站台;zh-tw:月台;zh-hk:月台}-]]'   )
					elseif type == "split" then 
						table.insert( maintdwkt ,'[[分離式月台|分離式-{zh-cn:站台;zh-tw:月台;zh-hk:月台}-]]' ) end
				end
				
				local d = args[ 'd' .. i ] or args[ '門' .. i ] or args[ '门' .. i ] or ''
				if     d == "左" then table.insert( maintdwkt , '開左邊车门' )
				elseif d == "右" then table.insert( maintdwkt , '開右邊车门' )
				elseif mw.ustring.match( d, "上[左右]下[左右]" ) then
					local str = mw.ustring.gsub( d, "上([左右])下([左右])",
						'<div style="display:inline-block;vertical-align:middle;line-height:1.2em;">↑開%1邊车门<br/>↓開%2邊车门</div>' 
					)
					table.insert( maintdwkt , str )
				elseif d ~= ''   then table.insert( maintdwkt , d )            end
				
				if text ~= '' then 
					table.insert( maintdwkt , text )
				end
				
				maintd:wikitext( table.concat( maintdwkt, " · ")  )
				
				if complex then 
					tr:tag 'td'
						:attr( 'colspan', '2' )
						:css ( "border-top"   , bt )
						:css ( "border-bottom", bb )
						:css ( "border-right" , br )
						:css ( "background"   , bg )
				end
			elseif group == "track" then
				
				-- unless above is platform pointing down, add bar
				local pg = args['g' .. i-1] or args['組' .. i-1] or args['组' .. i-1] or ''
				local pp = args['p' .. i-1] or args['類' .. i-1] or args['类' .. i-1] or ''
				local ng = args['g' .. i+1] or args['組' .. i+1] or args['组' .. i+1] or ''
				local common = mw.ustring.match( g, "[c共]" ) ~= nil
				local nextcom = mw.ustring.match( ng , "[c共]" ) ~= nil
				if i == 1 or ((mw.ustring.match(pg, '[p台]') ~= nil) and ((
						(string.find(pp, 'dn') ~= nil or string.find(pp, '下') ~= nil)
						and (  string.find(pp, 'side')   ~= nil or mw.ustring.match(pp, '[側侧]') ~= nil
							or string.find(pp, 'split')  ~= nil or mw.ustring.match(pp, '[離离]') ~= nil
							or string.find(pp, 'narrow') ~= nil or mw.ustring.match(pp, '[窄]')   ~= nil)
					) or (
						   string.find(pp, 'island') ~= nil	or mw.ustring.match(pp, '[島岛]') ~= nil
						or string.find(pp, 'bay')    ~= nil	or mw.ustring.match(pp, '[灣湾]') ~= nil
						or string.find(pp, 'wide')   ~= nil	or mw.ustring.match(pp, '[闊阔]') ~= nil
					))
					) or common then -- do nothing
				else style = style .. 'border-top:' .. border.trk end
				
				-- get line info
				local l = args[ 'l' .. i ] or args[ '綫' .. i ] or args[ '線' .. i ] or args[ '线' .. i ] or ''
				local line = data.lines[l] or ''
				
				-- platform number
				local p = args[ 'p' .. i ] or args[ '台' .. i ] or ''
				local line_color = '#' .. (line.color or '000000')
				local rgb = { tonumber(string.sub(line_color, 2, 3), 16), tonumber(string.sub(line_color, 4, 5), 16), tonumber(string.sub(line_color, 6, 7), 16) }
				local text_color = (rgb[1]*0.299 + rgb[2]*0.587 + rgb[3]*0.114) > 186 and "black" or "white"
				-- out = out .. '<!-- ' .. rgb[1] .. ',' .. rgb[2] .. ',' .. rgb[3] .. ' -->'
				local pno = haspno and frame:expandTemplate{ title = '圆形编号', args = {p, bg = line_color, fg = text_color} } or ''
				local pno_blnk = haspno and frame:expandTemplate{ title = '圆形编号', args = {''} } or ''
				
				local pl = args[ 'pl' .. i ] or ''
				local pno_l = haspno and frame:expandTemplate{ title = '圆形编号', args = {pl, bg = line_color, fg = text_color} } or ''
				local pr = args[ 'pr' .. i ] or ''
				local pno_r = haspno and frame:expandTemplate{ title = '圆形编号', args = {pr, bg = line_color, fg = text_color} } or ''
				if pl == '' or pr == '' then pno_l = pno; pno_r = pno end
				local pu = args[ 'pu' .. i ] or args[ '上台' .. i ] or args[ '上号' .. i ] or ''
				local pd = args[ 'pd' .. i ] or args[ '下台' .. i ] or args[ '下号' .. i ] or ''
				
				-- arrow direction
				local al = (args[ 'al' .. i ] or args[ '左箭' .. i ] or ''):upper()
				if     al ~= '' and mw.ustring.match(al,'[l左]') then al = "L"
				elseif al ~= '' and mw.ustring.match(al,'[r右]') then al = "R"
				elseif mw.ustring.match(g, '[l左]') and al == '' then al = "L" end
				local arrow_l = frame:expandTemplate{ title = 'Arrow', args = {al or "L"} }
				local ar = (args[ 'ar' .. i ] or args[ '右箭' .. i ] or ''):upper()
				if     ar ~= '' and mw.ustring.match(ar,'[l左]') then ar = "L"
				elseif ar ~= '' and mw.ustring.match(ar,'[r右]') then ar = "R"
				elseif mw.ustring.match(g, '[r右]') and ar == '' then ar = "R" end
				local arrow_r = frame:expandTemplate{ title = 'Arrow', args = {ar or "R"} }
				
				local merge_down = string.find(g, 'md') -- unused
				local merge_up   = string.find(g, 'mu') -- unused
				
				-- if (args[ 'p' .. i ] or args[ 'pl' .. i ] or args[ 'pr' .. i ])
				-- 	and not (args[ 'pu' .. i ] or args[ 'pd' .. i ]) then
				-- 	local np = args[ 'p' .. i+1 ] or ''
				-- 	if pp == 'side-dn' or pp == 'split-dn' or pp == 'narrow-dn'
				-- 		or pp == 'island' or pp == 'bay' or pp == 'wide' then pu = (al ~= "" and "l" or "r")
				-- 	elseif string.find(np, 'side') or string.find(np, 'split') or string.find(np, 'narrow')
				-- 		or np == 'island' or np == 'bay' or np == 'wide' then pd = (al ~= "" and "l" or "r") end
				-- end
				
				-- track text
				local linetitle = line ~= '' and line.title or l
				if data.lines['_default'] and data.lines['_default'].title and not line.title then
					linetitle = mw.ustring.gsub(data.lines['_default'].title, '%%1', l)
				end
				
				local ttxt = {}
				local pre = (future and '未來' or '') .. (l ~= '' and linetitle or '')
				local d1  = args[ 'd' .. i .. 'd1' ] or args[ 'd' .. i            ] or args[ '往' .. i          ] or ''
				local d1x = args[ 'd' .. i .. 'd1x'] or args[ 'd' .. i .. 'x'     ] or args[ '往' .. i .. 'x'   ] or ''
				local d2  = args[ 'd' .. i .. 'd2' ] or args[ '及往' .. i         ]                               or ''
				local d2x = args[ 'd' .. i .. 'd2x'] or args[ '及往' .. i .. 'x'  ]                               or ''
				local df  = args[ 'd' .. i .. 'f'  ] or args[ '未來往' .. i       ] or args[ '未来往' .. i      ] or ''
				local df2 = args[ 'd' .. i .. 'f2' ] or args[ '未來及往' .. i     ] or args[ '未来及往' .. i    ] or ''
				local n1  = args[ 'n' .. i .. 'd1' ] or args[ 'n' .. i            ] or args[ '下站' .. i        ] or ''
				local n1x = args[ 'n' .. i .. 'd1x'] or args[ 'n' .. i .. 'x'     ] or args[ '下站' .. i .. 'x' ] or ''
				local n2  = args[ 'n' .. i .. 'd2' ] or args[ '另下站' .. i       ]                               or ''
				local n2x = args[ 'n' .. i .. 'd2x'] or args[ '另下站' .. i ..'x' ]                               or ''
				local nf  = args[ 'n' .. i .. 'f'  ] or args[ '未來下站' .. i     ] or args[ '未来下站' .. i    ] or ''
				local nf2 = args[ 'n' .. i .. 'f2' ] or args[ '未來另下站' .. i   ] or args[ '未来另下站' .. i  ] or ''
				local dest1 = station(data, d1); local dest2 = station(data, d2);
				local next1 = station(data, n1); local next2 = station(data, n2);
				local destf = station(data, df); local nextf = station(data, nf);
				local destf2 = station(data, df2); local nextf2 = station(data, nf2)
				
				local bound = '' -- unused
				-- unused
				-- if     bound ~= nil									  then bound = bound
				-- elseif type(line.up) == 'string' and line.up    == d1 then bound = '上行'
				-- elseif type(line.up) == 'table'  and line.up[1] == d1 then bound = '上行' 
				-- elseif type(line.dn) == 'string' and line.dn    == d1 then bound = '下行' 
				-- elseif type(line.dn) == 'table'  and line.dn[1] == d1 then bound = '下行' end
				
				-- if (((type(line.up) == 'string') and line.up or line.up[1]) == dest1)
				-- 	or ((line.future and (type(line.future.up) == 'string') and line.future.up or line.future.up[0]) == dest1) then
				-- 	bound = '上行'
				-- elseif (((type(line.dn) == 'string') and line.dn or line.dn[1]) == dest1)
				-- 	or ((line.future and (type(line.future.dn) == 'string') and line.future.dn or line.future.dn[0]) == dest1) then
				-- 	bound = '下行'
				-- end
				local to = '往'
				if mw.ustring.match( dest1, '[内內外][圈環环]' ) then to = '' end
				if dest1 ~= '' and next1 ~= '' and next2 ~= '' and ((system == 'MTR' or system == "港鐵") and n2 == '馬場') then
					ttxt = { to, dest1, d1x
						   , (dest2 ~= '' and ('及' .. dest2 .. d2x) or '')
						   , '<small>(', next1, n1x, '/', next2, n2x, ')</small>' }
				elseif dest1 ~= '' and next1 ~= '' and dest2 ~= '' and next2 ~= '' then
					ttxt = { to, dest1, d1x, '<small>(', next1, n1x,
							((destf == '' and nextf ~= '')
								and '<span style="opacity:60%"> · ' .. '未來:' .. nextf .. '</span>'
								or ''
							) , ')</small>'
						   , '或', dest2, d2x, '<small>(', next2, n2x,
							((destf2 == '' and nextf2 ~= '')
								and '<span style="opacity:60%"> · ' .. '未來:' .. nextf2 .. '</span>'
								or ''
							), ')</small>' }
				elseif dest1 ~= '' and next1 ~= '' and dest2 ~= '' then
					ttxt = { to, dest1, d1x
						   , '及', dest2, d2x
						   , '<small>(', next1, n1x
						   , ((destf == '' and nextf ~= '')
						   		and '<span style="opacity:60%"> · ' .. '未來:' .. nextf .. '</span>'
						   		or ''
						   	), ')</small>' }
				elseif dest1 == '' and dest2 == '' and text ~= '' then 
					ttxt = { '' }
				elseif dest1 == term then
					ttxt = { dest1 }
				else
					ttxt = { to, (dest1 or '{{{d'..i..'}}}'), d1x
						   , '<small>(', (next1 or '{{{n'..i..'}}}'), n1x
						   , ((destf == '' and nextf ~= '')
						   		and '<span style="opacity:60%"> · ' .. '未來:' .. nextf .. '</span>'
						   		or ''
						   	), ')</small>' }
				end
				
				if destf ~= '' then
					local ftxt = { ' · <span style="opacity:60%">'
								 , '未來' -- , (l ~= '' and (line ~= '' and line.title or l) or '')
								 , '往'  , (destf or '{{{d'..i..'f}}}')
								 , '<small>(', (nextf or '{{{n'..i..'f}}}') .. ')</small></span>' }
					for j = 1, #ftxt, 1 do
						ttxt[ #ttxt+1 ] = ftxt[ j ]
					end
				end
				
				table.insert( ttxt, 1, pre )
				ttxt[ #ttxt+1 ] = text
				
				-- set left cell
				tr:tag 'td':cssText( "text-align:left;width:21px" )
					:css( 'visibility', ( al == '' or al == '/' ) and 'hidden' or nil )
					:css( 'padding', common and "0 3px 3px" or 
									 nextcom and "3px 3px 0" or "3px" )
					:wikitext( arrow_l )
				local td2 = tr:tag 'td':cssText( "text-align:left;width:21px" )
					:css( 'padding', common and "0 3px 3px" or 
									 nextcom and "3px 3px 0" or "3px" )
				if al == '' or (complex and (pu ~= '' or pd ~= '')) then
					td2 :css( 'visibility', 'hidden' )
						:wikitext( pno_blnk )
				else
					td2 :wikitext( pno_l or pno_blnk )
				end
				-- set center cell
				tr:tag 'td':css( 'text-align', 'center')
					:css( 'padding', common and "0 3px 3px" or 
									 nextcom and "3px 3px 0" or "3px" )
					:wikitext( table.concat( ttxt ) )
				-- set right cell
				local td4 = tr:tag 'td':cssText( "text-align:right;width:21px" )
					:css( 'padding', common and "0 3px 3px" or 
									 nextcom and "3px 3px 0" or "3px" )
				if ar == '' or (complex and (pu ~= '' or pd ~= '')) then
					td4 :css( 'visibility', 'hidden')
						:wikitext( pno_blnk )
				else
					td4 :wikitext( pno_r or pno_blnk )
				end
				tr:tag 'td':cssText( "text-align:left;width:21px" )
					:css( 'padding', common and "0 3px 3px" or 
									 nextcom and "3px 3px 0" or "3px" )
					:css( 'visibility', ( ar == '' or ar == '/' ) and 'hidden' or nil )
					:wikitext( arrow_r )
				
			elseif group == "text" then
				local cstyle = args[ 'cstyle' .. i ] or ''
				tr:tag 'td':attr( 'colspan', '5' ):css( 'text-align', 'center' )
					:css( 'padding', string.match( text, "月台出口" ) ~= nil and "0" or "3px" )
					:cssText( cstyle )
					:wikitext( text )
			end
			
			local app = args[ 'ap' .. i ] or ''
			local aps = args[ 'ap' .. i .. 's' ] or ''
			if append then tr:tag 'td':cssText( aps ) :wikitext( app ) end
			
			tr:cssText( style )
		end
	end
	out:wikitext(frame:extensionTag {name = 'templatestyles', args = {src = 'Template:車站構造/style.css'}})
	return out
end
return p