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

local export = {}

local m_utilities = require("Module:utilities")
local m_ja = require("Module:ja")
local ShowLabels = require("Module:labels").show_labels
--[=[
	Other modules used: [[Module:parameters]], [[Module:table]], [[Module:debug]]
]=]

local title = mw.title.getCurrentTitle()
local PAGENAME = title.text
local NAMESPACE = title.nsText

local lang = require("Module:languages").getByCode("ja")

local kanji_pattern = "㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟"

local kanji_grade_links = {
	"[[:w:学年別漢字配当表#第1学年(80字)|第一学年]]",
	"[[:w:学年別漢字配当表#第2学年(160字)|第二学年]]",
	"[[:w:学年別漢字配当表#第3学年(200字)|第三学年]]",
	"[[:w:学年別漢字配当表#第4学年(202字)|第四学年]]",
	"[[:w:学年別漢字配当表#第5学年(193字)|第五学年]]",
	"[[:w:学年別漢字配当表#第6学年(191字)|第六学年]]",
	"[[:w:常用漢字|常用漢字]]",		-- 7
	"[[:w:人名用漢字|人名用]]",		-- 8
	"[[:w:表外漢字|表外漢字]]"		-- 9
}

local function track(code)
	if type(code) == "string" then
		require("Module:debug").track("ja-kanjitab/" .. code)
	end
end

local function quote(text)
	return "“" .. text .. "”"
end

-- this is the function that is called from templates
function export.show(frame)
	local params = {
		[1] = { list = true, allow_holes = true },
		k = { list = true, allow_holes = true },
		o = { list = true, allow_holes = true },
		r = {},
		sort = {},
		yomi = {},
		ateji = {},
		alt = {},
		pagename = {},
		clear = {}
	}
	local args, unrecognized_args = require("Module:parameters").process(frame:getParent().args, params, true)

	if args.pagename and NAMESPACE == "" then
		track("pagename param in mainspace")
	end
	local pagename = args.pagename or PAGENAME

	for key, value in pairs(unrecognized_args) do
		local additional
		if mw.ustring.sub(key, 1, 1) == "y" then
			additional =  " Perhaps you meant " .. quote("yomi") .. "?"
		end
		error(quote(key) .. " is not a recognized parameter." .. (additional or ""))
	end

	local categories = {}
	local cells = {}
	-- replace e.g. 時々 with 時時
	local kanji = mw.ustring.gsub(pagename, '([' .. kanji_pattern .. '])々', '%1%1')
	-- remove non-kanji characters
	kanji = mw.ustring.gsub(kanji, '[^' .. kanji_pattern .. ']', '')

	local kanji_length = mw.ustring.len(kanji)
	local colspan = ""
	if kanji_length > 1 then
		colspan = 'colspan="' .. kanji_length .. '" '
	end

	local clear = args.clear
	if clear and clear ~= '' then
		if clear ~= 'left' and clear ~= 'right' and clear ~= 'none' then
			clear = 'both'
		end
		clear = ' clear: ' .. clear .. ';'
	else
		clear = ''
	end
	local table_head = [=[
{| class="wikitable kanji-table" style="text-align: center; font-size: small; float: right;]=] .. clear .. [=["
! ]=] .. colspan .. [=[style="font-weight: normal;" | この単語の[[漢字]]
|- lang="ja" class="Jpan" style="font-size: 2em; background: white; line-height: 1em;"

]=]

	local yomi

	-- on/kun is jūbakoyomi; NOTE: these are only applicable for two-kanji compounds
	-- kun/on is yutōyomi; NOTE: these are only applicable for two-kanji compounds
	if args.yomi then
		yomi = {}
		local extended_yomi_code = {
			o = 'on',			on = 'on',
			kanon = 'kanon',  -- kan is kan'yoon, and ko is kun+on for backward compatibility
			goon = 'goon',
			toon = 'toon',
			kan = 'kanyoon',	kanyo = 'kanyoon',		kanyoon = 'kanyoon',
			k = 'kun',			kun = 'kun',
			juku = 'jukujikun',	jukuji = 'jukujikun',	jukujikun = 'jukujikun',
			ok = "jūbakoyomi",	j = "jūbakoyomi",
			ko = "yutōyomi",	y = "yutōyomi",			yu = "yutōyomi",
			i = 'irregular',	irr = 'irregular',		irreg = 'irregular',	irregular = 'irregular',
			n = 'nanori',	nanori = 'nanori',
			[''] = '',			none = '',
		}
		for i in mw.text.gsplit(args.yomi, ',') do
			local _, _, a, b = mw.ustring.find(i, '^([a-z]*)([0-9]*)$')
			a = extended_yomi_code[a] or error("The yomi type “" .. args.yomi .. "” is not recognized.")
			b = tonumber(b) or 1
			table.insert(yomi, { a, b })
		end
		if #yomi == 1 and kanji_length > 1 then
			yomi[1][2] = kanji_length
		end
	else
		-- [[Special:WhatLinksHere/Template:tracking/ja-kanjitab/no yomi]]
		track("no yomi")
	end

	-- [[Special:WhatLinksHere/Template:tracking/ja-kanjitab/incorrect yutou or juubako]]
	if yomi and (yomi[1][1] == "jūbakoyomi" or yomi[1][1] == "yutōyomi") and kanji_length ~= 2 then
		track("ja-pron/incorrect yutou or juubako")
	end


	-- readings and okurigana are appended to this
	local sortkey = ""

	-- [[Special:WhatLinksHere/Template:tracking/ja-kanjitab/too many k]]
	-- [[Special:WhatLinksHere/Template:tracking/ja-kanjitab/too many o]]
	if args.k.maxindex and args.k.maxindex > args[1].maxindex then
		track("too many k")
	end

	if args.o.maxindex and args.o.maxindex > args[1].maxindex then
		track("too many o")
	end

	local yomi_type_by_kanji = {}
	if yomi then
		for i = 1, #yomi do
			for j = 1, yomi[i][2] do
				table.insert(yomi_type_by_kanji, yomi[i][1])
			end
		end
	else
		for i = 1, kanji_length do
			table.insert(yomi_type_by_kanji, '')
		end
	end

	local is_ateji = {}
	local cat_ateji = nil
	if args.ateji then
		local ateji = args.ateji
		if ateji == 'y' then
			for i = 1, kanji_length do
				is_ateji[i] = true
			end
			cat_ateji = "日本語 当て字"
		else
			for i in mw.text.gsplit(ateji, '[;,]') do
				string.gsub(i, '^([0-9]+)$', function(a)
					is_ateji[tonumber(a)] = true
					cat_ateji = "日本語 当て字"
				end)
				string.gsub(i, '^([0-9]+)[-~]([0-9]+)$', function (a, b)
					for j = tonumber(a), tonumber(b) do
						is_ateji[j] = true
					end
					cat_ateji = "日本語 当て字"
				end)
			end
		end
	end

	-- if hiragana readings were passed,
	-- make the "spelled with ..." categories, the readings cells on the lower level and build the sort key
	-- otherwise rely on the pagename to make the original kanjitab and categories
	local cells_above = {}
	local cells_below = {}
	local kanji_pos = 1
	local is_katakana = {}
	for i = 1, kanji_length do
		local reading = args[1][i]
		local reading_kana, reading_length = nil, nil
		local cell = {}

		if reading then _, _, reading_kana, reading_length = mw.ustring.find(reading, '^([^0-9]*)([0-9]*)$') end
		reading_kana = reading_kana ~= "" and reading_kana or nil
		reading_length = reading_kana and tonumber(reading_length) or 1

		if reading_length <= 1 then
			table.insert(cell, '| rowspan="2" | ')
		else
			table.insert(cell, '| colspan ="' .. reading_length .. '" | ')
		end

		-- display reading, actual reading and okurigana
		if reading_kana then
			if mw.ustring.find(reading_kana, '^[ァ-ヺー]+$') then
				is_katakana[i] = true
			elseif mw.ustring.find(reading_kana, '[ぁ-ゖ]') and not mw.ustring.find(reading_kana, '^[ぁ-ゖ]+$') then
					--[[Special:WhatLinksHere/Template:tracking/ja-kanjitab/not all hiragana]]
					track('not all hiragana')
			end

			local actual_reading = args.k[i]
			local okurigana = args.o[i]

			sortkey = sortkey .. (actual_reading or reading_kana) .. (okurigana or "")

			local okurigana_text = okurigana and "(" .. okurigana .. ")" or ""
			local actual_reading_text = actual_reading and " > " .. actual_reading .. okurigana_text or ""
			local text = reading_kana .. okurigana_text .. actual_reading_text

			table.insert(cell, '<span class="Jpan" lang="ja">' .. text .. '</span>')
			if reading_length <= 1 then table.insert(cell, '<br/>') end
		end

		-- display kanji grade, categorize
		for j = kanji_pos, kanji_pos + reading_length - 1 do
			local single_kanji = mw.ustring.sub(kanji, j, j)
			local kanji_grade = m_ja.kanji_grade(single_kanji)
			local ateji_text = is_ateji[j] and '<br/><small>([[当て字]])</small>' or ''

			---------------------------------------------------------------------------------
			-- 2020/03/16 日本語版ウィクショナリーで扱っていないカテゴリをコメントアウト。
			-- 必要に応じて復帰してください。
			---------------------------------------------------------------------------------
			-- if reading_kana then
			-- 	-- subcategorize by reading if this is joyo kanji, not doing that for less common kanji, with exceptions
			-- 	if (kanji_grade < 8 or (
			-- 		'厭昌之芽昌浩智晃淳敦聡晃旭亮糊桂隘阿唖撫鼠阿耘迂寅已伊餡姦闊礙碍凱亥价謳嘔齧日臣桶抉兎鵜卯綾飴焙肋鮫頚糞軋烏痒捷辰叩橙揃嶋澤菱彦囃覗呑之乃鼠做寅樋堤槌机杖頼辿哉叢狢峯巳卍鱒仄他惚弘宏燕倦經痙圭禽僑鋸醵墟屹綺几翫癌劫膠昂鹸牽喧餐鑽瑣些渾梱坤國壕誦哨蒐杓爾梓荼楕躁綜楚闡閃撰專泄藉棲錘錐祷盪淘點顛填擲擢闖厨蛋潭腿冪碧劈焚祓弗憑誹砒婢挽拔撥剥胚播乃狼牢蓮礫醂龍榴蕾酉祐佑耶也蔓曼沫邁呆硼牡甫步矮狸苔'
			-- 	):find(single_kanji)) and yomi_type_by_kanji[j] ~= 'irregular' and yomi_type_by_kanji[j] ~= 'jukujikun' and reading_length == 1 then
			-- 		if mw.ustring.find(reading_kana, '^[' .. kanji_pattern .. ']*$') then
			-- 			-- [[Special:WhatLinksHere/Template:tracking/ja-kanjitab/old-fashioned usage]]
			-- 			track("old-fashioned usage")
			-- 			table.insert(categories, "日本語 " .. single_kanji .. "を含む単語")
			-- 		else
			-- 			table.insert(categories, "日本語 " .. single_kanji .. "を" .. reading_kana .. "と読む単語")
			-- 		end
			-- 	else
			-- 		table.insert(categories, "日本語 " .. single_kanji .. "を含む単語")
			-- 	end
			-- else
			-- 	-- [[Special:WhatLinksHere/Template:tracking/ja-kanjitab/no reading]]
			-- 	if yomi_type_by_kanji[j] ~= 'irregular' and yomi_type_by_kanji[j] ~= 'jukujikun' then
			-- 		track("no reading")
			-- 	end
			-- 	table.insert(categories, "日本語 " .. single_kanji .. "を含む単語")
			-- end

			if reading_kana and is_katakana[i] and is_ateji[j] then
				cat_ateji = "日本語 外来語の当て字"
			end

			if reading_length <= 1 then
				table.insert(cell, "<small>" .. kanji_grade_links[kanji_grade] .. "</small>" .. ateji_text)
			else
				table.insert(cells_below, "| <small>" .. kanji_grade_links[kanji_grade] .. "</small>" .. ateji_text)
			end
		end
		table.insert(cells_above, table.concat(cell))
		kanji_pos = kanji_pos + reading_length
		if kanji_pos > kanji_length then break end
	end
	table.insert(cells, '|- style="background: white;"')
	if #cells_below > 0 then
		table.insert(cells, table.concat(cells_above, '\n'))
		table.insert(cells, '|- style="background: white;"')
		table.insert(cells, table.concat(cells_below, '\n'))
	else
		for i, v in ipairs(cells_above) do
			cells_above[i] = v:gsub('| rowspan="2" | ', '| ')
		end
		table.insert(cells, table.concat(cells_above, '\n'))
	end

	local yomi_info = {
		["on"] = {
			text = "音読み",
			entry = "音読み",
			category = "日本語 音読み",
		},
		["kanon"] = {
			text = "漢音",
			entry = "漢音",
			category = "日本語 音読み",
		},
		["goon"] = {
			text = "呉音",
			entry = "呉音",
			category = "日本語 音読み",
		},
		["toon"] = {
			text = "唐音",
			entry = "唐音",
			category = "日本語 音読み",
		},
		["kun"] = {
			text = "訓読み",
			entry = "訓読み",
			category = "日本語 訓読み",
		},
		["nanori"] = {
			text = "名乗り",
			entry = "名乗り",
			category = "日本語 名乗り",
		},
		["yutōyomi"] = {
			text = "湯桶読み",
			entry = "湯桶読み",
			category = "湯桶読み",
		},
		["jūbakoyomi"] = {
			text = "重箱読み",
			entry = "重箱読み",
			category = "重箱読み",
		},
		["jukujikun"] = {
			text = "熟字訓",
			entry = "熟字訓",
			category = "熟字訓",
		},
		["irregular"] = {
			text = "''不規則''",
			category = "日本語 不規則な読み",
		},
		["kanyoon"] = {
			text = "慣用音",
			entry = "慣用音",
			category = "日本語 慣用音",
		},
	}

	---------------------------------------------------------------------------------
	-- 2020/03/16 日本語版ウィクショナリーで扱っていないカテゴリをコメントアウト。
	-- 必要に応じて復帰してください。
	---------------------------------------------------------------------------------
	-- local rendaku = args.r
	-- if rendaku then
	-- 	table.insert(categories, "日本語 連濁")
	-- end

	if cat_ateji then table.insert(categories, cat_ateji) end

	if yomi then
		table.insert(cells, "|-")
		for _, i in ipairs(yomi) do
			local yomi_info = yomi_info[i[1]] or { text = i[1] }
			local text
			if yomi_info.entry then
				text = "[[" .. yomi_info.entry .. "|" .. yomi_info.text .. "]]"
			else
				text = yomi_info.text
			end
			table.insert(cells, '| colspan="' .. i[2] .. '" |' .. text)
		end
		local is_onyomi = { on = true, kanon = true, goon = true, toon = true, kanyoon = true }
		-- categories
		local all_onyomi = true
		for i = 1, #yomi do
			if not is_onyomi[yomi[i][1]] then all_onyomi = false; break end
		end
		if all_onyomi then
			---------------------------------------------------------------------------------
			-- 2020/03/16 日本語版ウィクショナリーで扱っていないカテゴリをコメントアウト。
			-- 必要に応じて復帰してください。
			---------------------------------------------------------------------------------
			-- table.insert(categories, yomi_info.on.category)
		elseif yomi[1][1] == 'jūbakoyomi' or yomi[1][1] == 'yutōyomi' then
			table.insert(categories, yomi_info[yomi[1][1]].category)
		else
			local all_yomi_of_same_type = true
			for i = 2, #yomi do
				if yomi[i][1] ~= yomi[1][1] then all_yomi_of_same_type = false; break end
			end
			if all_yomi_of_same_type then
				---------------------------------------------------------------------------------
				-- 2020/03/16 日本語版ウィクショナリーで扱っていないカテゴリをコメントアウト。
				-- 代わりに熟字訓のカテゴリだけを出力する処理を追加。
				-- 必要に応じて復帰してください。
				---------------------------------------------------------------------------------
				-- table.insert(categories, yomi_info[yomi[1][1]].category)
				if yomi[1][1] == 'jukujikun' then
					if is_katakana[1] then
						table.insert(categories, "日本語 外来語の熟字訓")
					else
						table.insert(categories, yomi_info[yomi[1][1]].category)
					end
				end
			elseif #yomi == 2 and yomi[1][2] == 1 and yomi[2][2] == 1 and mw.ustring.len(pagename) == 2 then
				if is_onyomi[yomi[1][1]] and yomi[2][1] == 'kun' then
					table.insert(categories, yomi_info["jūbakoyomi"].category)
				elseif yomi[1][1] == 'kun' and is_onyomi[yomi[2][1]] then
					table.insert(categories, yomi_info["yutōyomi"].category)
				end
			else
				local has_on = false
				local has_kun = false
				for i = 1, #yomi do
					if is_onyomi[yomi[i][1]] then
						has_on = true
					elseif yomi[i][1] == 'kun' or yomi[i][1] == 'jukujikun' then
						has_kun = true
					end	
				end
				if has_on and has_kun then
					table.insert(categories, '日本語 音訓混じり')
				end
			end
		end
	end

	local kanji_table = kanji_length == 0 and "" or (table_head ..
		mw.ustring.gsub(kanji, '(.)', '| style="padding: 0.5em;" | [[%1#日本語|%1]]\n') ..
		table.concat(cells, '\n') ..
		'\n|}')

	local forms_table = ""
	if args.alt and args.alt ~= "" and args.alt ~= "-" then
		local forms = {}
		for form in mw.text.gsplit(args.alt, ',') do
			local i_semicolon = string.find(form, ':')
			if i_semicolon then
				local altform = string.sub(form, 1, i_semicolon - 1)
				local altlabels = mw.text.split(string.sub(form, i_semicolon + 1), ' ')
				table.insert(forms, '<span class="Jpan" lang="ja" style="font-size:140%">[[' .. altform .. '#日本語|' .. altform .. ']]</span> <small>' .. ShowLabels(altlabels, lang, nil, nil, nil, nil, true) .. '</small>')
			else
				table.insert(forms, '<span class="Jpan" lang="ja" style="font-size:140%">[[' .. form .. '#日本語|' .. form .. ']]</span>')
			end
		end
		forms_table = '\n' .. [[{| class="wikitable floatright"
! style="font-weight:normal" | 異表記]] .. (#forms == 1 and "" or "s") .. '\n' .. [[
| style="text-align:center;font-size:108%" | ]] .. table.concat(forms, '<br>') .. '\n|}'
	end


	-- use user-provided sortkey if we got one, otherwise
	-- use the sortkey we've already made by combining the
	-- readings if provided, if we have neither then
	-- default to empty string and don't sort
	local userprovided_sortkey = args.sort
	if userprovided_sortkey then
		sortkey = userprovided_sortkey
	end
	if sortkey then
		sortkey = m_ja.jsort(sortkey)
	end

	return (forms_table == "" and kanji_table or (kanji_table .. forms_table)) .. m_utilities.format_categories(categories, lang, sortkey)
end

return export