Module:kanjitab: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
Haplology (talk | contribs)
put kanji links in <span lang="ja" class="Jpan">
Haplology (talk | contribs)
display readings categories for designated uncommon kanji
Line 53: Line 53:
table.insert(cells, "<td>")
table.insert(cells, "<td>")
if reading ~= "" and mw.ustring.match(reading, '[ぁ-ゖ]') then
if reading ~= "" and mw.ustring.match(reading, '[ぁ-ゖ]') then
-- subcategorize by reading if this is joyo kanji, don't do that for less common kanji
-- 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
if m_ja.kanji_grade(single_kanji) < 8 and yomi ~= "irregular" then
table.insert(categories, "Japanese terms spelled with " .. single_kanji .. " read as " .. reading)
table.insert(categories, "Japanese terms spelled with " .. single_kanji .. " read as " .. reading)
else
else

Revision as of 03:56, 19 February 2014

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

Testcases

Lua error at line 5: attempt to call field 'getLanguageByCode' (a nil value) Lua error at line 5: attempt to call field 'getLanguageByCode' (a nil value) Lua error at line 5: attempt to call field 'getLanguageByCode' (a nil value)
Lua error at line 5: attempt to call field 'getLanguageByCode' (a nil value) Lua error at line 5: attempt to call field 'getLanguageByCode' (a nil value) Lua error at line 5: attempt to call field 'getLanguageByCode' (a nil value)
Lua error at line 5: attempt to call field 'getLanguageByCode' (a nil value) Lua error at line 5: attempt to call field 'getLanguageByCode' (a nil value)

{{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").getLanguageByCode("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, '([一-鿌])々', '%1%1')
	-- remove non-kanji characters
	kanji = mw.ustring.gsub(kanji, '[^一-鿌]', '')

	local table_head = '<table align="right" border="0" cellpadding="3" 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 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>')
		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