模块:Xiangqi diagram

function format(s)
	s = string.gsub(s or '', '\n', '')
	return s
end
function split(inputstr, sep)
	if sep == nil then
		return {inputstr}
	end
	local t = {}
	local x = 1
	local y = inputstr:find(sep)
	while y ~= nil do
		table.insert(t, inputstr:sub(x, y - 1))
		x = y + 1
		y = inputstr:find(sep, x)
	end
	table.insert(t, inputstr:sub(x))
	return t
end

function drawArrow(arrow)
	local t = split(arrow:gsub(' ', ''), ',')
	for i = 1, #t do
		t[i] = t[i] * 1
	end
	local ss = ''
	while t[4] < 0 do
		t[4] = t[4] + 360
	end
	while t[4] >= 360 do
		t[4] = t[4] - 360
	end

	-- 箭頭的底盤 拿來旋轉方向使用
	ss = ss .. '<div class="xiangqi-aB" style="'
	ss = ss .. 'left:' .. (t[1] - 1) * 100 .. 'px;'
	ss = ss .. 'top:' .. t[2] * 100 .. 'px;'
	ss = ss .. 'width:' .. t[3] * 100 .. 'px;'
	ss = ss .. 'transform:rotate(' .. t[4] .. 'deg);">'

	-- 箭頭的直線部分
	ss = ss .. '<div class="xiangqi-aL" style="width:' .. (t[3] - 0.4) * 100 .. 'px;"></div>'
	-- 箭頭的頭部分
	ss = ss .. '<div class="xiangqi-aH" style="left:' .. t[3] * 100 .. 'px;"></div>'

	ss = ss .. '</div>'
	return ss
end

local qidata = {
	rs = {'暗'},

	gd = {'将', '將'},
	ad = {'士'},
	ed = {'象'},
	rd = {'车', '車'},
	hd = {'马', '馬'},
	cd = {'砲', '包'},
	sd = {'卒'},

	gl = {'帅', '帥'},
	al = {'仕'},
	el = {'相'},
	rl = {'伡', '俥'},
	hl = {'㐷', '傌'},
	cl = {'炮'},
	sl = {'兵'}
}

local p = {}
function p.board(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local align = format(args[1])
	if align == '' then
		align = 'tright'
	end
	local header = format(args[2])
	local footer = format(args[93])

	local size
	local width
	local height

	if args.size ~= nil then
		size = args.size
		width = size * 9
		height = size * 12
	elseif args.width ~= nil then
		width = args.width
		size = width / 9
		height = size * 12
	elseif args.height ~= nil then
		height = args.height
		size = height / 12
		width = size * 9
	else
		size = 25
		width = 225
		height = 300
	end

	local amplification = size / 100

	-- 範圍顯示參數
	local startcol = args.startcol or 1
	local cols = args.cols or (10 - startcol)
	local startrow = args.startrow or 0
	local rows = args.rows or (12 - startrow)

	local bwidth = cols * size
	local bheight = rows * size
	local bleft = (startcol - 1) * size
	local btop = startrow * size

	local ss = ''
	ss = ss .. '<div class="' .. align .. '" style="clear:right;">'
	ss = ss .. '<div style="width:' .. bwidth .. 'px;border:1px solid #a2a9b1;padding:5px;background-color:#f8f9fa;">'
	if header ~= '' then
		ss = ss .. '<div style="text-align:center;margin-bottom:3px;width:' .. bwidth .. 'px;">' .. header .. '</div>'
	end
	ss = ss .. '<div style="position:relative;overflow:hidden;width:' .. bwidth .. 'px;height:' .. bheight .. 'px;">'
	ss = ss .. '<div style="position:absolute;left:-' .. bleft .. 'px;top:-' .. btop .. 'px;">'
	ss = ss .. '<div style="position:absolute;">[[File:Xiangqi_board.svg|' .. width .. 'px]]</div>'
	for i = 0, 9 do
		for j = 0, 8 do
			local s = format(args[i * 9 + j + 3])
			s = string.gsub(s, ' ', '')
			s = string.gsub(s, ' ', '')
			s = string.gsub(s, '_', '')

			local sx = (j * size)
			local sy = ((i + 1) * size)

			-- 判斷棋名
			local qi = ''
			for key, val in pairs(qidata) do
				if s:find(key) ~= nil then
					qi = key
				end
				for k, v in ipairs(val) do
					if s:find(v) ~= nil then
						qi = key
					end
				end
			end
			if qi ~= '' then
				ss = ss .. '<div style="position:absolute;left:' .. sx .. 'px;top:' .. sy .. 'px;">'
				ss = ss .. '[[File:Xiangqi_' .. qi .. '1.svg|' .. size .. 'px]]</div>'
			end

			-- 標記符號的位移
			local sp = ''
			if s:find('x') ~= nil then
				sp = sp .. '<div class="xiangqi-pX"></div>'
			end
			if s:find('%.') ~= nil then
				sp = sp .. '<div class="xiangqi-pD"></div>'
			end
			if s:find('%[') ~= nil then
				sp = sp .. '<div class="xiangqi-pR"></div>'
			end
			if sp ~= '' then
				ss = ss .. '<div style="position:absolute;left:' .. sx .. 'px;top:' .. sy .. 'px;transform:scale(' .. amplification .. ');">' .. sp .. '</div>'
			end
		end
	end

	----箭頭
	local sp = ''
	local arrowname = 'arrow'
	local count = 1
	while args[arrowname] ~= nil do
		sp = sp .. drawArrow(args[arrowname])
		count = count + 1
		arrowname = 'arrow' .. count
	end
	if sp ~= '' then
		ss = ss .. '<div style="position:absolute;transform:scale(' .. amplification .. ');">' .. sp .. '</div>'
	end

	ss = ss .. '</div></div>'
	if footer ~= '' then
		ss = ss .. '<div style="text-align:left;margin-top:3px;width:' .. bwidth .. 'px;">' .. footer .. '</div>'
	end
	ss = ss .. '</div></div>'

	return ss
end

return p