Documentation for this module may be created at မော်ဂျူး:ja-kana-def/doc

local export = {}
local replace = mw.ustring.gsub
local m_ja = require('Module:ja')

local kana_data = {
	-- ['名']={'順','行','段'},
	['あ']={ '1','あ','あ'}, ['い']={ '2','あ','い'}, ['う']={ '3','あ','う'}, ['え']={ '4','あ','え'}, ['お']={ '5','あ','お'},
	['か']={ '6','か','あ'}, ['き']={ '7','か','い'}, ['く']={ '8','か','う'}, ['け']={ '9','か','え'}, ['こ']={'10','か','お'},
	['さ']={'11','さ','あ'}, ['し']={'12','さ','い'}, ['す']={'13','さ','う'}, ['せ']={'14','さ','え'}, ['そ']={'15','さ','お'},
	['た']={'16','た','あ'}, ['ち']={'17','た','い'}, ['つ']={'18','た','う'}, ['て']={'19','た','え'}, ['と']={'20','た','お'},
	['な']={'21','な','あ'}, ['に']={'22','な','い'}, ['ぬ']={'23','な','う'}, ['ね']={'24','な','え'}, ['の']={'25','な','お'},
	['は']={'26','は','あ'}, ['ひ']={'27','は','い'}, ['ふ']={'28','は','う'}, ['へ']={'29','は','え'}, ['ほ']={'30','は','お'},
	['ま']={'31','ま','あ'}, ['み']={'32','ま','い'}, ['む']={'33','ま','う'}, ['め']={'34','ま','え'}, ['も']={'35','ま','お'},
	['や']={'36','や','あ'},                          ['ゆ']={'37','や','う'},                          ['よ']={'38','や','お'},
	['ら']={'39','ら','あ'}, ['り']={'40','ら','い'}, ['る']={'41','ら','う'}, ['れ']={'42','ら','え'}, ['ろ']={'43','ら','お'},
	['わ']={'44','わ','あ'}, ['ゐ']={'45','わ','い'},                          ['ゑ']={'46','わ','え'}, ['を']={'47','わ','お'},
	['ん']={'48', nil, nil},
}

local function link(term, tr, gloss)
	if not tr then tr = m_ja.kana_to_romaji(term) end
	return require('Module:links').full_link({lang = require('Module:languages').getByCode('ja'), term = term, tr = tr, gloss = gloss}, 'term')
end

local function to_ordinal(text)
	return mw.getCurrentFrame():expandTemplate{ title = 'ordinal to word', args = { text } }
end

function export.show(frame)
	local args = frame:getParent().args
	local syllable = mw.title.getCurrentTitle().text
	local k2h_syllable = m_ja.kata_to_hira(syllable)
	local manual_translit = args['tr'] or nil

	local text = 'The $script syllable $syllable. Its equivalent in $other_script is $other_syllable.'
	local text_two = 'It is the $order syllable in the [[gojūon]] order'
	local text_three = '; its position is $position.'

	if m_ja.script(syllable) == 'Hira' then
		text = replace(text, '$script', '[[Appendix:Hiragana script|hiragana]]')
		text = replace(text, '$other_script', '[[Appendix:Katakana script|katakana]]')
		text = replace(text, '$syllable', link(syllable, manual_translit))
		text = replace(text, '$other_syllable', link(m_ja.hira_to_kata(syllable), manual_translit))
	elseif m_ja.script(syllable) == 'Kana' then
		text = replace(text, '$script', '[[Appendix:Katakana script|katakana]]')
		text = replace(text, '$other_script', '[[Appendix:Hiragana script|hiragana]]')
		text = replace(text, '$syllable', link(syllable, manual_translit))
		text = replace(text, '$other_syllable', link(k2h_syllable, manual_translit))
	else
		error(syllable .. ' is not a kana character or sequence.')
	end

	if syllable == 'ん' then
		text_two = replace(text_two, '$order', to_ordinal(kana_data[syllable][1]))
		return text .. ' ' .. text_two .. '.'
	elseif syllable == 'ン' then
		text_two = replace(text_two, '$order',  to_ordinal(kana_data[k2h_syllable][1]))
		return text .. ' ' .. text_two .. '.'
	elseif (syllable == 'ヷ') or (syllable == 'ヸ') or (syllable == 'ヹ') or (syllable == 'ヺ') or (syllable == 'ヸャ') or (syllable == 'ヸュ') or (syllable == 'ヸョ') then
		-- XXX
		text = replace(text, ' တူညီ-+', '')
		return text
	elseif kana_data[syllable] or kana_data[k2h_syllable] then
		if m_ja.script(syllable) == 'Kana' then
			kana_data[syllable] = kana_data[k2h_syllable] -- kana_data['ア'] = kana_data['あ']
			kana_data[syllable][2] = m_ja.hira_to_kata(kana_data[k2h_syllable][2]) -- kana_data['ア'][2] = k2h(kana_data['あ'][2])
			kana_data[syllable][3] = m_ja.hira_to_kata(kana_data[k2h_syllable][3]) -- kana_data['ア'][3] = k2h(kana_data['あ'][3])
		end
		text_two = replace(text_two, '$order', to_ordinal(kana_data[syllable][1]))
		text_three = replace(text_three, '$position',
			link(
					kana_data[syllable][2] .. '[[行]]' .. kana_data[syllable][3] .. '[[段]]',
					m_ja.kana_to_romaji(kana_data[syllable][2]) .. '-gyō ' .. m_ja.kana_to_romaji(kana_data[syllable][3]) .. '-dan',
					"row ''" .. m_ja.kana_to_romaji(kana_data[syllable][2]) .. "'', section ''" .. m_ja.kana_to_romaji(kana_data[syllable][3]) .. "''"
			)
		)
		return text .. ' ' .. text_two .. text_three
	else
		return text
	end
end

return export