コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

モジュール:サンドボックス/Semi-Brace/Goban

モジュールの解説[作成]
require('strict')
local p = {}

local function trim(str)
	return mw.text.trim(str)
end

local function getImage(token, frame)
	local qty_spec = frame.args.qty_spec or "px"
	local size = tostring(frame.args.size)
	return frame:preprocess("[[File:Go " .. trim(token) .. ".svg|" .. size .. "x" .. size .. qty_spec .."|link=]]")
end

local function getOne(token, frame)
	return "|" .. getImage(token, frame) .. "\n"
end

local function reqNotEq(declare, actual, mes)
	if (declare == actual) then
		error(mes)
	end
end

local function reqNotNil(actual, mes)
	if (actual == nil) then
		error(mes)
	end
end

function p.main(frame)
	-- mw.logObject(frame)
	local args = frame.args
	--[
	mw.log("Provided arguments: ----")
	mw.logObject(args)
	mw.log("------------------------")
	--]--
	local width = args.width
	local height = args.height
	
	-- アサーション
	reqNotNil(width, "widthを指定してください")
	reqNotNil(height, "heightを指定してください")
	
	reqNotEq(0, width % 2, "widthは奇数にしてください")
	reqNotEq(0, height % 2, "heightは奇数にしてください")
	
	-- `#` 演算子がちゃんと返さない
	reqNotNil(args[width * height], "Expected " .. tostring(width * height) .. " arguments")
	
	mw.log("Assertion passed")
	local table_head = '{| border="0" cellspacing="0" cellpadding="0" style="border:0;border-collapse:collapse;padding:0;font-size:1px"\n'
	local table_row_head = '|- style="vertical-align:top;border:none;"\n'
	local table_foot = '|}\n'
	local upper_edge = args.u or "u"
	local left_edge = args.l or "l"
	local right_edge = args.r or "r"
	local lower_edge = args.d or "d"
	mw.log("upper -> `" .. upper_edge .. "`, left -> `" .. left_edge .. "`, right -> `" .. right_edge .. "`, lower -> `" .. lower_edge .. "`")
	local fallback_token = {}
	-- init fallbacks
	-- upper
	if upper_edge ~= "" then
		for i=1, width, 1 do
			fallback_token[i] = (fallback_token[i] or "") .. upper_edge
		end
	end
	-- left
	if left_edge ~= "" then
		for i=0, height-1, 1 do
			local pos = i * width + 1
			fallback_token[pos] = (fallback_token[pos] or "") .. left_edge
		end
	end
	-- lower
	if lower_edge ~= "" then
		for i=1, width, 1 do
			local pos = i + width * (height - 1)
			fallback_token[pos] = (fallback_token[pos] or "") .. lower_edge
		end
	end
	-- right
	if right_edge ~= "" then
		for i=1, height, 1 do
			local pos = i * width
			fallback_token[pos] = (fallback_token[pos] or "") .. right_edge
		end
	end
	
	mw.log("Fallbacks: ---------")
	mw.logObject(fallback_token)
	mw.log("--------------------")
	--]]--
	local lens = width*height
	local build = table_head .. table_row_head
	for i=1, lens, 1 do
		-- [[File:Go.svg]]は十字
		local v = " "
		-- mw.log("Trim [" .. tostring(i) .. "]: `" .. args[i] .. "` -> `" .. (args[i]:byte(2)) .. "`")
		local ca = string.gsub(trim(args[i]), frame:preprocess(" "), "")
		if ca ~= "" then
			v = ca
		elseif fallback_token[i] ~= nil then
			mw.log("" .. tostring(i) .. ": fallback applied: `" .. fallback_token[i] .. "`")
			v = fallback_token[i]
		end
		v = trim(v)
		mw.log("" .. tostring(i) .. " -> `" .. v .. "`")
		build = build .. getOne(v, frame)
		if (i % width == 0) and i ~= lens then
			build = build .. table_row_head
		end --]]
	end
	build = build .. table_foot
	mw.log(build)
	return frame:preprocess(build)
end

return p