Module:ja-infl-demo

From Wiktionary, the free dictionary
Jump to navigation Jump to search

local export = {}

local m_links = require("Module:links")
local m_ja = require("Module:ja")
local m_ja_link = require("Module:ja-link")
local m_ja_pron = require("Module:ja-pron")
local match = mw.ustring.match
local split = mw.text.split

local function map(list, func)
	local result = {}
	for _, item in ipairs(list) do
		table.insert(result, func(item))
	end
	return result
end

function export.show(frame)
	local params = {
		[1] = {},
		["c"] = { list = true },
		["rom"] = {},
		["ojad"] = { boolean = true },
		["header"] = {}
	}
	local args, unrecognized_args = require("Module:parameters").process(frame:getParent().args, params, true)
	for key, value in pairs(unrecognized_args) do
		error("“" .. key .. "” is not a recognized parameter.")
	end
	
	local function ja(text) return '<span class="Jpan" lang="ja">' .. text .. '</span>' end
	local pagename = mw.title.getCurrentTitle().text
	local header = args['header'] or ('Inflection rules for the ' .. ja(pagename) .. ' form')
	
	local function format_accent(pron, acc)
		local ja_pron = m_ja_pron.accent(pron, acc)
		--[[
		local kana, romaji = ja_pron:match('(<span lang="ja" class="Jpan">.-) <span class="Latn"><samp>%[(.-)%]</samp></span>')
		if kana:find('ー') then
			-- revert to the original kana zukai (unicode mandatory here)
			local orig_kana = mw.ustring.gmatch(pron, '[ぁ-ゖァ-ヺー]')
			kana = mw.ustring.gsub(kana, '[ぁ-ゖァ-ヺー]', orig_kana)
		end
		return kana .. ' (<i>' .. romaji .. '</i>)'
		]]
		local kana = mw.ustring.gsub(pron, "[^ぁ-ゖァ-ヺー]", '')
		local romaji = ja_pron:match('<span lang="ja" class="Jpan">.- <span class="Latn"><samp>%[(.-)%]</samp></span>')
		return '<span class="Jpan" lang="ja">[[' .. kana .. '#Japanese|' .. kana .. ']]</span> (<i>' .. romaji .. '</i>)'
	end
	
	local function format_word(word)
		local accent, label
		if word:find(':') then
			word, label = match(word, '(.-):(.*)')
		end
		if word:find('[0-9]') then
			word, accent = match(word, '(.-)([0-9]+)')
			accent = tonumber(accent)
		end
		
		local result
		if accent then
			result = format_accent(word, accent)
		elseif not m_ja.script(word):find('Hani') and not word:find('^*') and args['rom'] == 'y' then
			result = m_ja_link.link({ lemma = word })
		else
			result = m_links.full_link({ term = word:gsub('^*', ''), lang = require("Module:languages").getByCode('ja') })
		end
		
		if label then
			result = result .. ' (<i>' .. mw.ustring.gsub(label, '([ぁ-ゖゞゝァ-ヺーヽヾ一-鿿㐀-䶿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩]+)', ja) .. '</i>)'
		end
		return result
	end
	
	local function format_cell(cell)
		if cell == '' or cell == '-' then
			return ''
		else
			return table.concat(map(split(cell, '/'), format_word), '<br>')
		end
	end
	
	local function format_row(row)
		local number_of_rows = #args['c']
		local predefined = {
			['godan'] = 'godan verbs (type 1)',
			['ichidan'] = 'ichidan verbs (type 2)',
			['kami ichidan'] = 'kami ichidan verbs (type 2a)',
			['shimo ichidan'] = 'shimo ichidan verbs (type 2b)',
			['irregular'] = 'irregular verbs (type 3)',
			['adjectives'] = 'adjectives',
		}
		if predefined[row] then
			return '! colspan=' .. number_of_rows .. ' | ' .. predefined[row]
		elseif not row:find(';') then
			return '! colspan=' .. number_of_rows .. ' | ' .. row
		else
			local cells = map(split(row, ';'), format_cell)
			for i = 1, number_of_rows - #cells do table.insert(cells, '') end
			return '| ' .. table.concat(cells, ' || ')
		end
	end
	
	local result = {}
	table.insert(result, '<div class="NavFrame"><div class="NavHead">' .. header .. '</div><div class="NavContent">\n{| class=wikitable')
	table.insert(result, "! " .. table.concat(args['c'], " !! ") )
	table.insert(result, '|-')
	table.insert(result, table.concat(map(split(args[1], '\n'), function(row) return format_row(row) end), '\n|-\n'))
	table.insert(result, '|}')
	if args['ojad'] then
		table.insert(result, '<p style="font-size:85%;">(Part of the accent information comes from the [http://www.gavo.t.u-tokyo.ac.jp/ojad/ Online Japanese Accent Dictionary].)</p>')
	end
	table.insert(result, '</div></div>')
	return table.concat(result, '\n')
end

return export