コンテンツにスキップ

モジュール:cjkcode

出典: フリー多機能辞典『ウィクショナリー日本語版(Wiktionary)』

このモジュールについての説明文ページを モジュール:cjkcode/doc に作成できます

local p = {}

local systems = {
	cns = {
		label = "CNS 11643",
		link = "CNS",
		category = "CNS 11643",
		unit = "面",
	},
}

function p.cns(frame)
	local args = frame:getParent() and frame:getParent().args or frame.args

	local sys = args.sys or "cns"
	local def = systems[sys]
	if not def then return "" end

	local code = args[1] or args.cns or args.code
	if not code or code == "" then return "" end

	local groups = {}
	for part in mw.text.gsplit(code, "/", true) do
		local x, y = part:match("^%s*(%w+)%-(%x+)%s*$")
		if x and y then
			groups[x] = groups[x] or {}
			table.insert(groups[x], y:upper())
		end
	end

	local plane_count = 0
	local code_count = 0
	for _, vals in pairs(groups) do
		plane_count = plane_count + 1
		code_count = code_count + #vals
	end

	local sortkey = mw.text.split(code, "/", true)[1]

	if plane_count == 1 and code_count == 1 then
		for x, vals in pairs(groups) do
			return string.format(
				"*%s: %s%s <code>0x%s</code>[[カテゴリ:%s|%s]]",
				def.label,
				x,
				def.unit,
				vals[1],
				def.category,
				vals[1]
			)
		end
	end

	local planes = {}
	for k in pairs(groups) do table.insert(planes, k) end
	table.sort(planes, function(a,b) return tonumber(a) < tonumber(b) end)

	local lines = {}
	table.insert(lines, string.format("*[[%s|%s]]", def.link, def.label))
	local categories = {}

	for _, plane in ipairs(planes) do
		local vals = groups[plane]
		table.sort(vals)

		local code_strs = {}
		for _, v in ipairs(vals) do
			table.insert(code_strs, string.format("<code>0x%s</code>", v))
		end

		table.insert(lines, string.format("**%s%s %s", plane, def.unit, table.concat(code_strs, ", ")))
		table.insert(categories, string.format("[[カテゴリ:%s|%s]]", def.category, vals[1]))
	end

	return table.concat(lines, "\n") .. "\n" .. table.concat(categories, "\n")
end

function p.ksx(frame)
	local args = frame:getParent() and frame:getParent().args or frame.args
	local code = args[1] or args.ksx
	if not code or code == "" then return "" end

	local groups = {}
	for part in mw.text.gsplit(code, "/", true) do
		local plane, val = part:match("^%s*([%d%-]+)%-(%x+)%s*$")
		if plane and val then
			groups[plane] = groups[plane] or {}
			table.insert(groups[plane], val:upper())
		end
	end
	if next(groups) == nil then return "" end

	local planes = {}
	for k in pairs(groups) do table.insert(planes, k) end
	table.sort(planes)

	local lines = {}
	local categories = {}

	for _, plane in ipairs(planes) do
		local vals = groups[plane]
		table.sort(vals)

		local codes = {}
		for _, v in ipairs(vals) do
			table.insert(codes, "<code>0x" .. v .. "</code>")
		end

		table.insert(lines, string.format("*KS X %s: %s", plane, table.concat(codes, ", ")))

		table.insert(categories, string.format("[[カテゴリ:KS X %s|%s]]", plane, vals[1]))
	end

	return table.concat(lines, "\n") .. "\n" .. table.concat(categories, "\n")
end

function p.kps(frame)
	local args = frame:getParent() and frame:getParent().args or frame.args
	local code = args[1] or args.kps
	if not code or code == "" then return "" end

	local groups = {}
	for part in mw.text.gsplit(code, "/", true) do
		local plane, val = part:match("^%s*([%d%-]+)%-(%x+)%s*$")
		if plane and val then
			groups[plane] = groups[plane] or {}
			table.insert(groups[plane], val:upper())
		end
	end
	if next(groups) == nil then return "" end

	local planes = {}
	for k in pairs(groups) do table.insert(planes, k) end
	table.sort(planes)

	local lines = {}
	local categories = {}

	for _, plane in ipairs(planes) do
		local vals = groups[plane]
		table.sort(vals)

		local codes = {}
		for _, v in ipairs(vals) do
			table.insert(codes, "<code>0x" .. v .. "</code>")
		end

		table.insert(lines, string.format("*KPS %s: %s", plane, table.concat(codes, ", ")))
		table.insert(categories, string.format("[[カテゴリ:KPS %s|%s]]", plane, vals[1]))
	end

	return table.concat(lines, "\n") .. "\n" .. table.concat(categories, "\n")
end

return p