Module:kanjitab: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
Wyang (talk | contribs)
+狸
expanding kanji range
Line 24: Line 24:
local cells = {}
local cells = {}
-- replace e.g. 時々 with 時時
-- replace e.g. 時々 with 時時
local kanji = mw.ustring.gsub(PAGENAME, '([一-鿌])々', '%1%1')
local kanji = mw.ustring.gsub(PAGENAME, '([㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟])々', '%1%1')
-- remove non-kanji characters
-- remove non-kanji characters
kanji = mw.ustring.gsub(kanji, '[^一-鿌]', '')
kanji = mw.ustring.gsub(kanji, '[^㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]', '')


local table_head = '<table align="right" border="0" cellpadding="4" cellspacing="0" style="border:1px solid #aaaaaa; margin-left:5px; margin-right:10px; font-size:8pt; text-align:center; margin: 0 0 1em 1em;" rules="all" frame="box"><tr><td colspan="' .. mw.ustring.len(kanji) .. '" bgcolor="#f9f9f9">[[kanji|Kanji]] in this term</td></tr><tr style="font-size: 2em">'
local table_head = '<table align="right" border="0" cellpadding="4" cellspacing="0" style="border:1px solid #aaaaaa; margin-left:5px; margin-right:10px; font-size:8pt; text-align:center; margin: 0 0 1em 1em;" rules="all" frame="box"><tr><td colspan="' .. mw.ustring.len(kanji) .. '" bgcolor="#f9f9f9">[[kanji|Kanji]] in this term</td></tr><tr style="font-size: 2em">'

Revision as of 22:09, 4 March 2015

This module generates the content of {{ja-kanjitab}}, {{ryu-kanjitab}} etc.

Testcases

Kanji in this term
kun'yomi
Kanji in this term
kun'yomi
Kanji in this term
kun'yomi


Kanji in this term
Kanji in this term
Kanji in this term


Kanji in this term
Kanji in this term


{{ja-kanjitab|pagename=大和言葉|||こと|は|k4=ば|yomi=juku2,k2}}
{{ja-kanjitab|pagename=大和言葉|やまと2|こと|は|k3=ば|yomi=juku2,k2}} // "3" is # of argument, not # of kanji

local m_utilities = require("Module:utilities")
local m_ja = require("Module:ja")
local export = {}

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

local kanji_grade_links = {
	"[[:w:Kyōiku_kanji|Grade: 1]]",
	"[[:w:Kyōiku_kanji|Grade: 2]]",
	"[[:w:Kyōiku_kanji|Grade: 3]]",
	"[[:w:Kyōiku_kanji|Grade: 4]]",
	"[[:w:Kyōiku_kanji|Grade: 5]]",
	"[[:w:Kyōiku_kanji|Grade: 6]]",
	"[[:w:Jōyō kanji|Grade: S]]",      -- 7
	"[[:w:Jinmeiyō kanji|Jinmeiyō]]",  -- 8
	"[[:w:Hyōgaiji|Hyōgaiji]]"         -- 9
}

-- this is the function that is called from templates
function export.show(frame)
	PAGENAME = mw.title.getCurrentTitle().text
	local args = frame:getParent().args
	local categories = {}
	local cells = {}
	-- replace e.g. 時々 with 時時
	local kanji = mw.ustring.gsub(PAGENAME, '([㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟])々', '%1%1')
	-- remove non-kanji characters
	kanji = mw.ustring.gsub(kanji, '[^㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]', '')

	local table_head = '<table align="right" border="0" cellpadding="4" cellspacing="0" style="border:1px solid #aaaaaa; margin-left:5px; margin-right:10px; font-size:8pt; text-align:center; margin: 0 0 1em 1em;" rules="all" frame="box"><tr><td colspan="' .. mw.ustring.len(kanji) .. '" bgcolor="#f9f9f9">[[kanji|Kanji]] in this term</td></tr><tr style="font-size: 2em">'

	-- hold the single kanji as we iterate over all the kanji
	local single_kanji = ""
	-- hold a reading passed from the first unnamed parameter
	local reading = ""
	local actual_reading = ""
	local okurigana = ""
	local sortkey = ""
	local yomi = args["yomi"] or ""
	if yomi == "ok" or yomi == "j" then yomi = "jūbakoyomi" end -- on/kun is jūbakoyomi
	if yomi == "ko" or yomi == "y" then yomi = "yutōyomi" end -- kun/on is yutōyomi
	if yomi == "on" or yomi == "o" then yomi = "on" end
	if yomi == "kun" or yomi == "k" then yomi = "kun" end
	if yomi == "irr" or yomi == "irreg" or yomi == "irregular" then yomi = "irregular" end
	if yomi == "kan" or yomi == "kanyo" or yomi == "kanyoon" then yomi = "kanyoon" 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
	table.insert(cells, "<tr>")
	for i = 1, mw.ustring.len(kanji) do
		single_kanji = mw.ustring.sub(kanji,i,i)
		reading = args[i] or ""
		table.insert(cells, "<td>")
		if reading ~= "" and mw.ustring.match(reading, '[ぁ-ゖ]') then
			-- subcategorize by reading if this is joyo kanji, don't do that for less common kanji, with exceptions
			if (m_ja.kanji_grade(single_kanji) < 8 or mw.ustring.match(single_kanji, '[厭昌之芽昌浩智晃淳敦聡晃旭亮糊桂隘阿唖撫鼠阿耘迂寅已伊餡姦闊礙碍凱亥价謳嘔齧日臣桶抉兎鵜卯綾飴焙肋鮫頚糞軋烏痒捷辰叩橙揃嶋澤菱彦囃覗呑之乃鼠做寅樋堤槌机杖頼辿哉叢狢峯巳卍鱒仄他惚弘宏燕倦經痙圭禽僑鋸醵墟屹綺几翫癌劫膠昂鹸牽喧餐鑽瑣些渾梱坤國壕誦哨蒐杓爾梓荼楕躁綜楚闡閃撰專泄藉棲錘錐祷盪淘點顛填擲擢闖厨蛋潭腿冪碧劈焚祓弗憑誹砒婢挽拔撥剥胚播乃狼牢蓮礫醂龍榴蕾酉祐佑耶也蔓曼沫邁呆硼牡甫步矮狸]')) and yomi ~= "irregular" then
				table.insert(categories, "Japanese terms spelled with " .. single_kanji .. " read as " .. reading)
			else
				table.insert(categories, "Japanese terms spelled with " .. single_kanji)
			end
			
			actual_reading = args[("k" .. i)] or ""
			okurigana = args[("o" .. i)] or ""
			if actual_reading == "" and okurigana == "" then
				sortkey = (sortkey .. reading)
				table.insert(cells, reading)
			elseif actual_reading ~= "" and okurigana == "" then
				sortkey = (sortkey .. actual_reading)
				table.insert(cells, reading)
				table.insert(cells, " > ")
				table.insert(cells, actual_reading)
			elseif actual_reading == "" and okurigana ~= "" then
				sortkey = (sortkey .. reading .. okurigana)
				table.insert(cells, reading)
				table.insert(cells, "(")
				table.insert(cells, okurigana)
				table.insert(cells, ")")
			else
				sortkey = (sortkey .. actual_reading .. okurigana)
				table.insert(cells, reading)
				table.insert(cells, "(")
				table.insert(cells, okurigana)
				table.insert(cells, ") > ")
				table.insert(cells, actual_reading)
				table.insert(cells, "(")
				table.insert(cells, okurigana)
				table.insert(cells, ")")
			end
		else
			table.insert(categories, "Japanese terms spelled with " .. single_kanji)
		end
		
		if reading ~= "" and mw.ustring.match(reading, '[ぁ-ゖ]') then table.insert(cells, "<br/>") end
		table.insert(cells, "<small>")
		local kanji_grade = m_ja.kanji_grade(single_kanji)
		table.insert(cells, kanji_grade_links[kanji_grade] or "")
		table.insert(cells, "</small>")
		table.insert(cells, "</td>")
	end
	-- finish the html for the row
	table.insert(cells, "</tr>")

	local rendaku = args["r"] or ""
	if rendaku ~= "" then table.insert(categories, "Rendaku") end
	if yomi ~= "" then
		table.insert(cells, "<tr>")
		if yomi == "on" then 
			table.insert(categories, "Japanese terms read with on'yomi")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[音読み|on\'yomi]]</td>')
		elseif yomi == "kun" then 
			table.insert(categories, "Japanese terms read with kun'yomi")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[訓読み|kun\'yomi]]</td>')
		elseif yomi == "yutōyomi" then 
			table.insert(categories, "Japanese terms read with yutōyomi")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[湯桶読み|yutōyomi]]</td>')
		elseif yomi == "jūbakoyomi" then 
			table.insert(categories, "Japanese terms read with jūbakoyomi")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[重箱読み|jūbakoyomi]]</td>')
		elseif yomi == "irregular" then 
			table.insert(categories, "Japanese terms with irregular kanji readings")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '"><i>Irregular</i></td>')
		elseif yomi == "kanyoon" then
			table.insert(categories, "Japanese terms read with kan'yōon")
			table.insert(cells, '<td colspan="' .. mw.ustring.len(kanji) .. '">[[慣用音#Japanese|kan\'yōon]]</td>')
		end
	table.insert(cells, "</tr>")
	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"] or ""
	if userprovided_sortkey ~= "" then sortkey = userprovided_sortkey end
	if sortkey ~= "" then sortkey = m_ja.jsort(sortkey) end

	-- will only use sortkey if sortkey is different from PAGENAME
	if sortkey == "" then
		return
			table_head ..
			mw.ustring.gsub(kanji, '(.)', '<td><span lang="ja" class="Jpan">[[%1#Japanese|%1]]</span></td>') .. '</tr>' ..
			table.concat(cells) ..
			'</table>' ..
			m_utilities.format_categories(categories, lang)
	else
		return
			table_head ..
			mw.ustring.gsub(kanji, '(.)', '<td><span lang="ja" class="Jpan">[[%1#Japanese|%1]]</span></td>') .. '</tr>' ..
			table.concat(cells) ..
			'</table>' ..
			m_utilities.format_categories(categories, lang, sortkey)
	end
end

return export