(Translated by https://www.hiragana.jp/)
模組:Zh-han - 维基词典,自由的多语言词典 とべ转到内容ないよう

ぐみ:Zh-han

維基てん自由じゆうてきげんてん
local m_str_utils = require("Module:string utilities")

local codepoint = m_str_utils.codepoint
local find = m_str_utils.find
local get_section = require("Module:utilities").get_section
local gsub = m_str_utils.gsub
local sub = m_str_utils.sub

local m_links = require("Module:links")
local m_script_utils = require("Module:script utilities")

local export = {}

local PAGENAME = mw.loadData("Module:headword/data").pagename

local cangjie = {
	A = "にち", B = "つき", C = "きむ", D = "", E = "みず", F = "", G = "", H = "たけ", I = "ほこ", J = "じゅう", K = "だい", L = "なか", M = "いち", N = "ゆみ", O = "ひと", P = "しん", Q = "", R = "くち", S = "しかばね", T = "廿にじゅう", U = "やま", V = "おんな", W = "", X = "なん", Y = "ぼく", Z = "しげる"
}

local radicals = {
	"いち","丨","丶","丿","おつ","亅",
	"","亠","ひと","儿","いれ","はち","冂","冖","冫","几","凵","かたな","ちから","勹","匕","匚","匸","じゅう","ぼく","卩","厂","厶","また",
	"くち","囗","","","夂","夊","ゆう","だい","おんな","","宀","すん","しょう","尢","しかばね","屮","やま","巛","こう","おのれ","はば","","幺","广","廴","廾","弋","ゆみ","彐","彡","彳",
	"しん","ほこ","","","ささえ","攴","ぶん","","きん","かた","无","にち","曰","つき","","かけ","とめ","歹","殳","毋","","もう","","气","みず","","つめ","ちち","爻","爿","へん","きば","うし","いぬ",
	"げん","たま","ふり","かわら","あま","なま","よう","","疋","疒","癶","しろ","かわ","さら","","ほこ","","いし","しめせ","禸","禾","あな","だて",
	"たけ","べい","いと","かん","网","ひつじ","はね","ろう","而","耒","みみ","聿","にく","しん","","いたり","うす","した","ます","ふね","うしとら","いろ","そうこう","虍","むし","","こう","ころも","襾",
	"","かく","げん","たに","まめ","いのこ","豸","かい","あか","はし","あし","","くるま","からし","たつ","辵","邑","とり","釆","さと",
	"きむ","ちょう","もん","阜","隶","ふるとり","あめ","あお","",
	"めん","かわ","韋","にら","おと","ぺーじ","ふう","","しょく","くび","こう",
	"うま","ほね","だか","髟","鬥","鬯","鬲","おに",
	"さかな","とり","鹵","鹿しか","むぎ","あさ",
	"","きび","くろ","黹",
	"黽","かなえ","","ねずみ",
	"はな","ひとし",
	"",
	"りゅう","かめ",
	"龠"
}

local simplified_radical = {
	['讠']='げん',['门']='もん',['饣']='しょく',['飞']='',['马']='うま',['见']='',['贝']='かい',['纟']='いと',['车']='くるま',['长']='ちょう',['韦']='韋',['风']='ふう',['钅']='きむ',['鸟']='とり',['りゅう']='りゅう',['龙']='りゅう',['页']='ぺーじ',['ひとし']='ひとし',['齐']='ひとし',['むぎ']='むぎ',['かめ']='かめ',['龟']='かめ',['鱼']='さかな',['黾']='黽',['鼡']='ねずみ',['']='',['齿']='',['卤']='鹵',['𬺞']='かめ',['丬']='爿',['くろ']='くろ',['']='',
}

local codes = {
	c = '<small>(簡體ちゅうぶん)</small>', m = '<small>(中國ちゅうごく大陸たいりく漢字かんじ)</small>',
	j = '<small>(日本にっぽん漢字かんじ)</small>', k = '<small>(韓國かんこく漢字かんじ)</small>'
}

-- Letters in "as" or "sn" parameter names follow these criteria:
-- There must be 1 or 2 of them.
-- They must be c, m, j, or k.
-- They must be in the order [cm]jk.
local function validate_letters(letters)
	local letter_count = #letters
	return letter_count == 3 and (letters == 'mjk' or letters == 'cjk')
		or letter_count == 2 and (letters == 'jk' or letters == 'cj' or letters == 'mj')
		or letter_count == 1 and codes[letters]
end

local function mul_link(term)
	return m_links.full_link({lang = require('Module:languages').getByCode('zh'), term = term.."//", tr="-"})
end

local function radical(name,number,variant)
	number = number or 1
	local padleft = ("%03d"):format(number)
	local text = '<div class="noprint" style="border:solid #aaa 1px;margin:0;font-size:74%;background:#f9f3f6;width:120px;padding:3px;spacing:0;text-align:center;float:right"><div style="float:left">[[Image:Commons-logo.svg|24px|none|link=Commons:Category:Radical ' .. padleft .. ']]</div>[[Commons:Category:Radical ' .. padleft .. "-0|參看さんかんせき於<br>'''部首ぶしゅ " .. number .. " <big>" .. ((name and name ~= '') and PAGENAME or '※') .. "</big>''']]てきへん</div>"
	if name == PAGENAME or (variant and variant ~= '') then
		text = text .. '[[Category:漢字かんじ部首ぶしゅ| ]]'
	else
		text = text .. '[[Category:派生はせい漢字かんじ| ]]'
	end
	return text
end

function export.simp(frame)
	local args = frame:getParent().args
	
	local trad = args[1] or ''
	if trad == '' then
		trad = mw.loadData("Module:zh/data/st")[PAGENAME] or PAGENAME
	end
	
	local nocap = args['nocap'] or ''
	local alt = args['a'] or ''
	local from = {args['f'] or '',args['f2'] or ''}
	local to = {args['t'] or '',args['t2'] or ''}
	if from[1] == '' and to[1] ~= '' then from[1] = trad end
	local text = {}
	local result = ('「' .. mul_link(trad) .. '」')
	if alt ~= '' then
		result = result .. 'かず「' .. mul_link(alt) .. '」'
	end
	result = result .. 'てき簡化'
	for i=1,2 do
		if from[i] ~= '' then
			if to[i] == '' then
				table.insert(text,mul_link(from[i]) .. " → いち無法むほう單獨たんどく顯示けんじてき[[分類ぶんるい:Kenny's testing category 3]]")
			elseif to[i] == ' ' or to[i] == '&nbsp;' then
				table.insert(text,'じょ「' .. mul_link(from[i]) .. '」')
			else
				table.insert(text,mul_link(from[i]) .. ' → ' .. mul_link(to[i]))
			end
		end
	end
	if #text > 0 then
		result = result .. '(' .. table.concat(text,'  ') .. ')'
		result = result:gsub('」 ','」')
	end
	return result
end

-- NOTE: added 2023 Nov as a sanity check for {{Han ref}}
function export.cp()
	return string.format('%.4X', codepoint(PAGENAME))
end

do
	local function check_translingual_only()
		for _, sec in pairs(mw.loadData("Module:headword/data").page.page_L2s) do
			if sec ~= "またがげん" or sec~= "またが语言" then -- LOCALIZATION
				return
			end
		end
		require('Module:debug').track('zh-han/translingual-only character')
	end
	
	function export.character(frame)
		local args = {}
		for arg, val in pairs(frame:getParent().args) do -- Set empty args to nil.
			if val ~= '' then
				args[arg] = val
			end
		end
		if radicals[tonumber(args['rn'])] ~= (simplified_radical[args['rad']] or args['rad']) then
			error('Radical number (rn) and radical (rad) do not match')
		end
		local text = { insert = table.insert }
		local categories = { insert = table.insert }
		local as = tostring(tonumber(args['as'] or '-1'))
		if as == '0' then
			text:insert(radical(args['rad'], args['rn'], (args['var'] or '')))
		end
		
		-- Creating headword with [[Module:headword]] ensures that page title is script-tagged with Hani (generic Han).
		-- This assumes that {{Han char}} is only used in Translingual sections.
		local mul = require("Module:languages").getByCode("mul")
		local Hani = require("Module:scripts").getByCode("Hani")
		local sort_key = args['rad'] .. args['as'] .. PAGENAME
		text:insert(
			require("Module:headword").full_headword{
				lang = mul,
				sc = Hani,
				heads = { args['head'] },
				categories = {},
				pos_category = "符號ふごう",
				sort_key = sort_key,
			}
		)
		text:insert("([[Appendix:部首ぶしゅ索引さくいん/" .. (args['rad'] or '') .. '|-{' .. (args['rad'] or ' ') .. '}-]]+' .. as .. "")
		if args['asj'] then
			text:insert("<small>(ちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asj']) .. "<small>(日本にっぽん漢字かんじ)</small>")
		end
		if args['asc'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asc']) .. "<small>(簡體ちゅうぶん)</small>")
		end
		if args['ask'] then
			text:insert("<small>(ちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['ask']) .. "<small>(韓國かんこく漢字かんじ)</small>")
		end
		if args['asjk'] then
			text:insert("<small>(ちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asjk']) .. "<small>(日本にっぽん漢字かんじかず韓國かんこく漢字かんじ)</small>")
		end
		if args['asj+'] then
			text:insert("<small>(なか文和ふみかず韓國かんこく漢字かんじ)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asj+']) .. "<small>(日本にっぽん漢字かんじ)</small>")
		end
		if args['ask+'] then
			text:insert("<small>(なか文和ふみかず日本にっぽん漢字かんじ)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['ask+']) .. "<small>(韓國かんこく漢字かんじ)</small>")
		end
		if args['asc+'] then
			text:insert("<small>(しげるたいちゅう文和ふみかず日本にっぽん漢字かんじ)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asc+']) .. "<small>(簡體ちゅうぶん)</small>")
		end
		if args['asc++'] then
			text:insert("<small>(しげるたいちゅうぶん日本にっぽん漢字かんじかず韓國かんこく漢字かんじ)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asc++']) .. "<small>(簡體ちゅうぶん)</small>")
		end
		if args['ascj'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['ascj']) .. "<small>(簡體ちゅう文和ふみかず日本にっぽん漢字かんじ)</small>")
		end
		if args['ascj+'] then
			text:insert("<small>(しげるたいちゅう文和ふみかず韓國かんこく漢字かんじ)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['ascj+']) .. "<small>(簡體ちゅう文和ふみかず日本にっぽん漢字かんじ)</small>")
		end
		if args['asm'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asm']) .. "<small>(中國ちゅうごく大陸たいりく漢字かんじかず日本にっぽん漢字かんじ)</small>")
		end
		if args['asmj'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asmj']) .. "<small>(中國ちゅうごく大陸たいりく漢字かんじかず日本にっぽん漢字かんじ)</small>")
		end
		if args['asmj+'] then
			text:insert("<small>(しげるたいちゅう文和ふみかず韓國かんこく漢字かんじ)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asmj+']) .. "<small>(中國ちゅうごく大陸たいりく漢字かんじかず日本にっぽん漢字かんじ)</small>")
		end
		if args['asm+'] then
			text:insert("<small>(しげるたいちゅう文和ふみかず日本にっぽん漢字かんじ)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asm+']) .. "<small>(中國ちゅうごく大陸たいりく漢字かんじ)</small>")
		end
		if args['asm++'] then
			text:insert("<small>(しげるたいちゅうぶん日本にっぽん漢字かんじかず韓國かんこく漢字かんじ)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asm++']) .. "<small>(中國ちゅうごく大陸たいりく漢字かんじ)</small>")
		end
		if args['ascjk'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['ascjk']) .. "<small>(簡體ちゅうぶん日本にっぽん漢字かんじかず韓國かんこく漢字かんじ)</small>")
		end
		if args['asmjk'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asmjk']) .. "<small>(中國ちゅうごく大陸たいりく漢字かんじ日本にっぽん漢字かんじかず韓國かんこく漢字かんじ)</small>")
		end
		if args['asmjk+'] then
			text:insert("<small>(臺灣たいわんしげるたいちゅうぶん)</small>ある" .. ("-{" .. args['rad'] .. "}-" or '') .. '+' .. tonumber(args['asmjk+']) .. "<small>(中國ちゅうごく大陸たいりく香港ほんこん漢字かんじ日本にっぽん漢字かんじかず韓國かんこく漢字かんじ)</small>")
		end
		text:insert(",とも" .. (args['sn'] or '') .. "")
		-- To do: fix handling of multiple additional stroke numbers;
		-- search query:
		-- hastemplate:"Module:zh-han" insource:/\|sn[cmjk][cmjk]?\+*=[^|]+\|sn[cmjk][cmjk]?\+*=[^|]+/
		if args['sn'] ~= '1' then
			-- text:insert('s')
		end
		if args['snj'] then
			text:insert("<small>(ちゅうぶん)</small>")
		end
		if args['snc'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>")
		end
		if args['snk'] then
			text:insert("<small>(ちゅうぶん)</small>")
		end
		if args['snjk'] then
			text:insert("<small>(ちゅうぶん)</small>")
		end
		if args['snj+'] then
			text:insert("<small>(なか文和ふみかず韓國かんこく漢字かんじ)</small>")
		end
		if args['snk+'] then
			text:insert("<small>(なか文和ふみかず日本にっぽん漢字かんじ)</small>")
		end
		if args['snc+'] then
			text:insert("<small>(しげるたいちゅう文和ふみかず日本にっぽん漢字かんじ)</small>")
		end
		if args['snc++'] then
			text:insert("<small>(しげるたいちゅうぶん日本にっぽん漢字かんじかず韓國かんこく漢字かんじ)</small>")
		end
		if args['sncj'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>")
		end
		if args['sncj+'] then
			text:insert("<small>(しげるたいちゅう文和ふみかず韓國かんこく漢字かんじ)</small>")
		end
		if args['snm'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>")
		end
		if args['snmj'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>")
		end
		if args['snmj+'] then
			text:insert("<small>(しげるたいちゅう文和ふみかず韓國かんこく漢字かんじ)</small>")
		end
		if args['snm+'] then
			text:insert("<small>(しげるたいちゅう文和ふみかず日本にっぽん漢字かんじ)</small>")
		end
		if args['snm++'] then
			text:insert("<small>(しげるたいちゅうぶん日本にっぽん漢字かんじかず韓國かんこく漢字かんじ)</small>")
		end
		if args['sncjk'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>")
		end
		if args['snmjk'] then
			text:insert("<small>(しげるたいちゅうぶん)</small>")
		end
		if args['snmjk+'] then
			text:insert("<small>(臺灣たいわんしげるたいちゅうぶん)</small>")
		end
		
		-- Find parameters specifying additional stroke numbers and add notes for
		-- them.
		for param_name, value in pairs(args) do
			if type(param_name) == "string" then
				local letters = param_name:match("^sn(%l+)%+*$")
				if letters then
					if validate_letters(letters) then
						if value and value ~= '' then
							local label = {}
							for letter in letters:gmatch(".") do
								table.insert(label, codes[letter]) -- Already checked that letters are valid.
							end
							label = table.concat(label, "")
							
							text:insert("ある" .. value .. "")
							if value ~= '1' then
								-- text:insert('s')
							end
							text:insert("" .. label .. "")
						end
					else
						require("Module:debug").track('zh-han/incorrect stroke number parameter')
						-- return error("Unrecognized stroke number parameter name " .. param_name .. ".")
					end
				end
			end
		end
		
		if args['canj'] then
			local canj = args['canj']
			local canj_split = mw.text.split(canj,',')
			for i = 1, #canj_split do
				if canj_split[i]:match('^%u+$') then
					canj_split[i] = canj_split[i]:gsub('[%(%)]','')
					-- LOCALIZATION
					canj_split[i] = canj_split[i]:gsub('[A-Z]',cangjie) .. '([[Appendix:くら索引さくいん/' .. canj_split[i]:sub(1,1) .. '|' .. canj_split[i] .. ']])'
				else
					return error('The value of "canj" parameter is invalid.')
				end
			end
			text:insert(",[[Appendix:くら索引さくいん|くら頡碼]]:-{" .. table.concat(canj_split,'ある') .. "}-")
			if #canj_split > 1 then
				categories:insert("[[Category:ゆうくら頡碼てき漢字かんじ]]")
			end
		end
		if args['four'] then
			local four = args['four']
			local four_split = mw.text.split(four,',')
			for i=1,#four_split do
				if not four_split[i]:match('[<>]') then
					four_split[i] = four_split[i]:gsub('(%d)(%d%d%d)%.?(%d?)','[[Appendix:四角號碼しかくごうま索引さくいん/%1|%1%2<sub>%3</sub>]]')
				else
					return error('The value of "four" parameter is invalid.')
				end
			end
			text:insert(",[[Appendix:四角號碼しかくごうま索引さくいん|四角號碼しかくごうま]]:" .. table.concat(four_split,'ある'))
			if #four_split > 1 then
				categories:insert("[[Category:ゆう四角號碼しかくごうまてき漢字かんじ]]")
			end
		end
		if args['ids'] then
			local ids = args['ids']
			local ids_split = mw.text.split(ids,',')
			for i=1, #ids_split do
				if not ids_split[i]:match('[%[%]]') then
					if find(ids_split[i], '^[⿲⿳](.)%1%1$') then
						categories:insert('[[Category:さん疊字じょうじ]]')
					end
					if find(ids_split[i], '^[⿰⿱](.)%1$') then
						categories:insert('[[Category:疊字じょうじ]]')
					end
					local before = {}
					ids_split[i] = gsub(ids_split[i], "([⺀-⻳ -〿アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヰヱヲン㇀-㇣〇いち-鿿㐀-䶿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩𠀀-𪛟𪜀-𮯠𰀀-𲎯])",
						function (character)
							if not before[character] then
								before[character] = true
								return "[[" .. character .. "]]"
							else
								return character
							end
						end)
					ids_split[i] = ids_split[i]:gsub("^(.-)%f[%(%z](.*)",
						function(m1, m2)
							local m1 = m_links.embedded_language_links{term = m1, lang = mul, sc = Hani}
							m1 = m_script_utils.tag_text(m1, mul, Hani)
							local letters = {
								G = '<span class="explain" title="中國ちゅうごく大陸たいりく">りく</span>',
								H = '<span class="explain" title="香港ほんこん">みなと</span>',
								T = '<span class="explain" title="臺灣たいわん">たい</span>',
								J = '<span class="explain" title="日本にっぽん">にち</span>',
								K = '<span class="explain" title="韓國かんこく">かん</span>',
								V = '<span class="explain" title="こしみなみ">えつ</span>'
							}
							return m1 .. "<small>" .. m2:gsub(".", letters) .. "</small>"
						end)
				end
			end
			text:insert(",[[w:漢字かんじけん|けん組合くみあい]]:-{" .. table.concat(ids_split,'ある'):gsub('%(','('):gsub('%)',')') .. "}-")
			if #ids_split > 1 then
				categories:insert("[[Category:ゆう多種たしゅ組合くみあい描述てき漢字かんじ]]")
			end
		end
		text:insert(')')
		if args['rad'] then
			if mw.title.getCurrentTitle().nsText == '' then
				categories:insert("[[Category:漢字かんじもと|" .. sort_key .. "]]")
			end
		else
			if mw.title.getCurrentTitle().nsText == '' then
				categories:insert("[[Category:ぼつゆうradさんすうてき漢字かんじ]]")
			end
		end
		local params_to_check = { "as", "rn", "sn", "canj" }
		local sortkey = args['rad'] and "|" .. args['rad'] or ""
		-- LOCALIZATION
		if not args['ids'] or args['ids'] == '' then
			categories:insert("[[Category:ぼつゆう表意ひょうい文字もじ描述てき漢字かんじ" .. sortkey .. "]]")
		end
		if not args['four'] or args['four'] == '' then
			categories:insert("[[Category:ぼつゆう四角號碼しかくごうまてき漢字かんじ" .. sortkey .. "]]")
		end
		for _, param in pairs(params_to_check) do
			if not args[param] or args[param] == '' then
				categories:insert("[[Category:ぼつゆう" .. param .. "まいりすうてき漢字かんじ" .. sortkey .. "]]")
			end
		end
		
		if type(args['sn']) == "string" and sub(args['sn'],1,1) == '0' then
			return error('Please remove leading zeros from the "sn" parameter.')
		end
		
		check_translingual_only()
		
		if (get_section(mw.title.getCurrentTitle():getContent(), "またがげん") or get_section(mw.title.getCurrentTitle():getContent(), "またが语言") or ""):match("%f[^%z\n\r]#[ \t]*%S") then
			categories:insert("[[Category:有定ありさだ義行よしゆきてきまたがげん漢字かんじ" .. sortkey .. "]]")
		end
		
		return table.concat(text) .. table.concat(categories)
	end
end

return export