Module:kanjitab: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
Undo revision 36161461 by Suzukaze-c (talk)
+testing if table layout change is responsible for ja-kanjitab no longer floating to the left of floating div for wp links
Line 28: Line 28:
kanji = mw.ustring.gsub(kanji, '[^㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]', '')
kanji = mw.ustring.gsub(kanji, '[^㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]', '')


local table_head = '<table class="floatright wikitable" style="text-align:center; font-size:small;"><tr><th colspan="' .. mw.ustring.len(kanji) .. '" style="font-weight:normal;">[[kanji|Kanji]] in this term</th></tr><tr lang="ja" class="Jpan" style="font-size:2em; background:white; line-height:1em;" >'
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
-- hold the single kanji as we iterate over all the kanji
Line 48: Line 48:
-- make the "spelled with ..." categories, the readings cells on the lower level and build the sort key
-- 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
-- otherwise rely on the pagename to make the original kanjitab and categories
table.insert(cells, '<tr style="background:white;">')
table.insert(cells, "<tr>")
for i = 1, mw.ustring.len(kanji) do
for i = 1, mw.ustring.len(kanji) do
single_kanji = mw.ustring.sub(kanji,i,i)
single_kanji = mw.ustring.sub(kanji,i,i)
reading = args[i] or ""
reading = args[i] or ""
table.insert(cells, "<td>")
table.insert(cells, '<td style="background:white;">')
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, with exceptions
-- subcategorize by reading if this is joyo kanji, don't do that for less common kanji, with exceptions
Line 140: Line 140:
return
return
table_head ..
table_head ..
mw.ustring.gsub(kanji, '(.)', '<td style="padding:0.5em;">[[%1#Japanese|%1]]</td>') .. '</tr>' ..
mw.ustring.gsub(kanji, '(.)', '<td style="padding:0.5em;"><span lang="ja" class="Jpan">[[%1#Japanese|%1]]</span></td>') .. '</tr>' ..
table.concat(cells) ..
table.concat(cells) ..
'</table>' ..
'</table>' ..
Line 147: Line 147:
return
return
table_head ..
table_head ..
mw.ustring.gsub(kanji, '(.)', '<td style="padding:0.5em;">[[%1#Japanese|%1]]</td>') .. '</tr>' ..
mw.ustring.gsub(kanji, '(.)', '<td style="padding:0.5em;"><span lang="ja" class="Jpan">[[%1#Japanese|%1]]</span></td>') .. '</tr>' ..
table.concat(cells) ..
table.concat(cells) ..
'</table>' ..
'</table>' ..

Revision as of 18:05, 18 January 2016

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ōgai kanji|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 style="background:white;">')
		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 style="padding:0.5em;"><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 style="padding:0.5em;"><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