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

local export = {}

local m_ja = require('Module:ja')
local m_tr = require('Module:ja-translit')
local lang = require'Module:languages'.getByCode'ja'
local d_kyu = mw.loadData'Module:ja/data/kyu'

local function gmatch_array(s, pattern) local result = {} for e in mw.ustring.gmatch(s, pattern) do table.insert(result, e) end return result end
local function map(arr, f) local result = {} for _, e in ipairs(arr) do local fe = f(e) if fe ~= nil then table.insert(result, fe) end end return result end
local function filter(arr, f) local result = {} for _, e in ipairs(arr) do if f(e) then table.insert(result, e) end end return result end
local function contains(arr, item) for _, e in ipairs(arr) do if e == item then return true end end return false end
local function set(arr) local result = {} for _, e in ipairs(arr) do if not contains(result, e) then table.insert(result, e) end end return result end
local function flatten(arrs) local result = {} for _, arr in ipairs(arrs) do for _, e in ipairs(arr) do table.insert(result, e) end end return result end
-- f should be str->str in the following functions
local function memoize(f) local results = {} return function(s) if not results[s] then results[s] = f(s) end return results[s] end end
local getContent_memo = memoize(function(title) return mw.title.new(title):getContent() or '' end)
local function group(arr, f) local r = {} for _, e in ipairs(arr) do local fe = f(e) if r[#r] and r[#r].key == fe then table.insert(r[#r], e) else table.insert(r, { e, key = fe }) end end return r end

local function ja(text) return '<span lang="ja" class="Jpan">' .. text .. '</span>' end
local function link(lemma, display) return ja('[[' .. lemma .. '#ဂျပန်|' .. (display or lemma) .. ']]') end
local function link_bracket(lemma, display) return ja('【[[' .. lemma .. '#ဂျပန်|' .. (display or lemma) .. ']]】') end

--[[ returns an array of definitions, each having the format
	{
		def = <definition>,
		kanji_spellings = <array of alternative kanji spellings listed in {{ja-kanjitab|alt=...}}, can be overrided with {{ja-def|...}}>,
		kana_spellings = <array of alternative kana spellings listed in the headword template>,
		historical_kana_spellings = <array of historical kana spellings listed in the headword template>,
		header = <name of PoS header>,
		headword_line = <wikicode of headword line>,
	}
]]
local function get_definitions_from_wikicode(wikicode)
	local current_kanji_spellings = {}
	local current_kanji_spellings_with_labels = {}
	local current_kana_spellings = {}
	local current_historical_kana_spellings = {}
	local current_head_level = '=='
	local current_header = 'ဂျပန်'
	local current_headword_line
	local status -- nil, 'under_headword', 'under_kanji', 'in_ja_readings'

	wikicode = wikicode:gsub('\n*<br */?>\n*({{ja%-altread)', '%1')

	-- in the local function `function format_definition` below,
	-- insertion of `|hira=` affects `{{tlb}}`
	-- (`{{tlb|ja|followed by a verb phrase|hira=によって}}`)
	-- and then that bad wikitext is preprocessed
	-- (`The parameter "hira" is not used by this template`).
	-- we are not doing anything with `{{tlb}}` anyway,
	-- so remove it
	wikicode = wikicode:gsub(' *{{tlb.-}}', '')
	
	local result = {}
	for line in (wikicode:match'==ဂျပန်==\n(.*)' or ''):gmatch'[^\n]+' do
		if status == 'under_headword' then
			if line:match'^#+[^#:*]' then
				table.insert(result, {
					def = line
						:gsub('<ref>.-<.ref>', '')
						:gsub('<ref .->.-<.ref>', '')
						:gsub('<ref.-/>', '')
						:gsub('{{attention|ja.-}}', ''),
					kanji_spellings =
						line:match'{{ja%-def|' and mw.text.split(line:match'{{ja%-def|([^}]+)', '|') or
						line:match'<!%-%- kana only %-%->' and {} or
						current_kanji_spellings,
					kanji_spellings_with_labels = current_kanji_spellings_with_labels,
					kana_spellings = current_kana_spellings,
					historical_kana_spellings = current_historical_kana_spellings,
					header = current_header,
					headword_line = current_headword_line,
				})
			end
		elseif status == 'under_kanji' then
			if line:match'^#+[^#:*]' then
				table.insert(result, {
					def = '#' .. line
						:gsub('<ref>.-<.ref>', '')
						:gsub('<ref .->.-<.ref>', '')
						:gsub('<ref.-/>', '')
						:gsub('{{attention|ja.-}}', ''),
					kanji_spellings = {},
					kanji_spellings_with_labels = {},
					kana_spellings = {},
					historical_kana_spellings = {},
					header = current_header,
					headword_line = current_headword_line,
				})
			elseif line:match'^{{ja%-readings%f[|}%z]' then
				result[#result].kanji_readings = {}
				status = 'in_ja_readings'
			end
		elseif status == 'in_ja_readings' then
			for rs in line:gmatch'|[a-z]+=([^|}]+)' do
				for r in rs:gmatch'(..-)%f[,%z],?' do
					r = r:gsub('%-', ''):match('..-%f[<%z]'):match'^%s*(.-)%s*$'
					if not contains(result[#result].kanji_readings, r) then
						table.insert(result[#result].kanji_readings, r:gsub('%-', ''):match'^%s*(.-)%s*$')
					end
				end
			end
			if line:match'}}' then status = 'under_kanji' end
		end
		
		-- the following branches are ordered by frequency; read backwards
		if line:match'^{{ja%-noun[|}]' or line:match'^{{ja%-adj[|}]' or
			line:match'^{{ja%-pos[|}]' or line:match'^{{ja%-phrase[|}]' or
			line:match'^{{ja%-verb[|}]' or line:match'^{{ja%-verb form[|}]' or
			line:match'^{{ja%-verb%-suru[|}]' or line:match'{{ja%-altread[|}]' then
			
			local escaped_line = line:gsub('%[%[([^%[%]|]-)|([^%[%]|]-)%]%]', '[[%1`%2]]'):gsub('|hkata=', '|hhira=')
			escaped_line = escaped_line:gsub('|hira=', '|') -- ja-altread
			escaped_line = escaped_line:gsub('|kata=', '|') -- ja-altread
			current_kana_spellings = map(gmatch_array(escaped_line, '|([・、ぁ-ゖァ-ヺー%^%-%. %%]+)'), m_ja.remove_ruby_markup)
			current_historical_kana_spellings = map(gmatch_array(escaped_line, '|hhira=([ぁ-ゖァ-ヺー%^%-%. %%]+)'), m_ja.remove_ruby_markup)
			current_headword_line = line
			status = 'under_headword'
		elseif line:match'^{{ja%-kanji[|}]' then
			current_kana_spellings = {}
			current_historical_kana_spellings = {}
			current_headword_line = line
			table.insert(result, {
				def = '#' .. (({
					'Grade 1 kanji',
					'Grade 2 kanji',
					'Grade 3 kanji',
					'Grade 4 kanji',
					'Grade 5 kanji',
					'Grade 6 kanji',
					'Jōyō kanji',
					'Jinmeiyō kanji',
				})[tonumber(line:match'|grade=([^|}]*)')] or 'Hyōgaiji kanji'),
				kanji_spellings = {},
				kanji_spellings_with_labels = {},
				kana_spellings = {},
				historical_kana_spellings = {},
				header = current_header,
				headword_line = line,
			})
			status = 'under_kanji'
		elseif line:match'^===+[^=]+===+$' then
			local head_level_new, header_new = line:match'^(===+)([^=]+)===+$'
			if not status or head_level_new:len() <= current_head_level:len() then
				current_head_level, current_header = head_level_new, header_new
				status = nil
			end
		elseif line:match'^{{ja%-kanjitab[|}]' then
			local alt_argument = line:match'|alt=([^|}]*)'
			current_kanji_spellings = alt_argument and mw.text.split(alt_argument:gsub(':[^,]*', ''), ',') or {}
			current_kanji_spellings_with_labels = alt_argument and mw.text.split(alt_argument, ',') or {}
		elseif line:match'^==[^=]+==$' then
			break
		end
	end
	return result
end

-- ditto, except that each definition also contains the title of the page it is from
local function get_definitions_from_entry(title)
	local wikicode = getContent_memo(title)
	local defs = get_definitions_from_wikicode(wikicode)
	map(defs, function(def)
		def.title = title
		table.insert(({ Hira = true, Kana = true, ['Hira+Kana'] = true })[m_ja.script(title)] and def.kana_spellings or def.kanji_spellings, title)
		end)
	return defs
end

local function format_table_content(defs, frame, title, no_cat)
	local kanji_grade_labels = {
		'<span class="explain" title="Grade 1 kanji" style="vertical-align: top;">1</span>',
		'<span class="explain" title="Grade 2 kanji" style="vertical-align: top;">2</span>',
		'<span class="explain" title="Grade 3 kanji" style="vertical-align: top;">3</span>',
		'<span class="explain" title="Grade 4 kanji" style="vertical-align: top;">4</span>',
		'<span class="explain" title="Grade 5 kanji" style="vertical-align: top;">5</span>',
		'<span class="explain" title="Grade 6 kanji" style="vertical-align: top;">6</span>',
		'<span class="explain" title="Jōyō kanji" style="vertical-align: top;">S</span>',
		'<span class="explain" title="Jinmeiyō kanji" style="vertical-align: top;">J</span>',
		'<span class="explain" title="Hyōgaiji kanji" style="vertical-align: top;">H</span>' }
	
	local function ruby(kanji, kana) -- this function ought to be in [[Module:ja]]
		local kanji_segments = mw.ustring.gsub(kanji, "([A-Za-z0-9々一-鿿㐀-䶿﨎﨏﨑﨓﨔﨟﨡﨣﨤﨧-﨩𠀀-𪛟𪜀-𮯯𰀀-𱍏0-9A-Za-z]+)", "`%1`")
		
		-- returns possible matches between kanji and kana
		-- for example, match_k('`物`の`哀`れ', 'もののあわれ') returns { '[物](も)の[哀](のあわ)れ', '[物](もの)の[哀](あわ)れ' }
		local function match_k(kanji_segments, kana)
			if kanji_segments:find('`') then
				local kana_portion, kanji_portion, rest = mw.ustring.match(kanji_segments, '(.-)`(.-)`(.*)')
				_, _, kana = mw.ustring.find(kana, '^' .. kana_portion .. '(.*)')
				if not kana then return {} end
				local candidates = {}
				for i = 1, mw.ustring.len(kana) do
					for _, candidate in ipairs(match_k(rest, mw.ustring.sub(kana, i + 1))) do
						table.insert(candidates, kana_portion .. '[' .. kanji_portion .. '](' .. mw.ustring.sub(kana, 1, i) .. ')' .. candidate)
					end
				end
				return candidates
			else
				return (kanji_segments == kana) and { kana } or {}
			end
		end
		
		local matches = match_k(kanji_segments, kana)
		local result = #matches == 1 and matches[1] or ('[' .. kanji .. '](' .. kana .. ')')
		return result:gsub("%[([^%[%]]+)%]%(([^%(%)]+)%)", "<ruby><rb>%1</rb><rt>%2</rt></ruby>")
	end
	
	local function format_headword(defs)
		local def_title = defs[1].title
		local kana = defs[1].kana_spellings[1]
		local headword = link_bracket(def_title, kana and mw.title.getCurrentTitle().text ~= kana and ruby(def_title, kana) or def_title)
		local kanji_grade = mw.ustring.len(def_title) == 1 and m_ja.kanji_grade(def_title)
		return '<span style="font-size:x-large">' .. headword .. '</span>' .. (kanji_grade and kanji_grade_labels[kanji_grade] or '')
	end
	
	local preprocess_memo = memoize(function (s) return frame:preprocess(s) end)
	local function format_definitions(defs)
		local headword_line_categories = {}
		local alt_forms, alt_forms_rep = {}, { [title] = true, [defs[1].title] = true }
		local kanji_readings
		local function format_definition(def)
			local def_text = def.def:match'{{rfdef[|}]' and "''This term needs a translation to English.''" or preprocess_memo(def.def:gsub('^#+ *', '')):gsub('%[%[Category:.-%]%]', '')
			local def_prefix = def.def:match'^#+':gsub('#', ':')
			local def_pos_label = ' <span style="padding-right:.6em;color:#5A5C5A;font-size:80%">[' .. mw.ustring.lower(def.header) .. ']</span> '
			
			if not no_cat and def.kana_spellings[1] then
				local cat_hira, cat_kata = {}, {}
				local format_cat = require'Module:utilities'.format_categories
				local cat_sort = require'Module:Jpan-sortkey'.sortkey_from_string(def.kana_spellings[1])
				require'Module:Jpan-headword'.cat{
					lang = lang, pagename = title, categories = cat_hira, katakana_category = cat_kata,  pos = def.header:gsub('^.', string.lower)
				}
				table.insert(headword_line_categories, format_cat(cat_hira, lang, cat_sort))
				table.insert(headword_line_categories, format_cat(cat_kata, lang, m_ja.hira_to_kata(cat_sort)))
			end
			
			if def.kana_spellings[1] then alt_forms_rep[def.kana_spellings[1]] = true end
			for _, s in ipairs(def.kanji_spellings) do if not alt_forms_rep[s] then alt_forms_rep[s] = true table.insert(alt_forms, s) end end
			for _, s in ipairs(def.kana_spellings) do if not alt_forms_rep[s] then alt_forms_rep[s] = true table.insert(alt_forms, s) end end
			
			kanji_readings = def.kanji_readings or kanji_readings
			
			return def_prefix .. def_pos_label .. def_text
		end
		local formatted_defs = table.concat(map(defs, format_definition), '\n')
		if #alt_forms == 1 and alt_forms[1] == title then alt_forms = {} end
		return table.concat(headword_line_categories) .. '\n' .. formatted_defs
			.. (#alt_forms > 0 and '\n: <div style="background:#f8f9fa"><span style="color:#5A5C5A;font-size:80%">'
				.. (#alt_forms == 1 and 'မူကွဲ စာလုံးပေါင်း' or 'မူကွဲ စာလုံးပေါင်းများ')
				.. '</span><br><span style="margin-left:.8em">'
				.. table.concat(map(alt_forms, link), ', ')
				.. '</span></div>' or '')
			.. (kanji_readings and '\n: <div style="background:#f8f9fa"><span style="color:#5A5C5A;font-size:80%">ခန်ဂျိ အဖတ်:</span><br><span style="margin-left:.8em">'
				.. table.concat(map(kanji_readings, link), ', ')
				.. '</span></div>' or '')
	end
	
	local is_first_row = true
	local function format_row(defs)
		local result = '|-\n| style="white-space:nowrap;width:15%;vertical-align:top;' .. (is_first_row and '' or 'border-top:1px solid lightgray;') .. '" | ' .. format_headword(defs)
			.. '\n| style="' .. (is_first_row and '' or 'border-top:1px solid lightgray;') .. '" |\n' .. format_definitions(defs) .. '\n'
		is_first_row = false
		return result
	end
	
	local def_groups = group(defs, function(def) return def.title .. ',' .. (def.kana_spellings[1] or '') end)
	local rows = map(def_groups, format_row)
	
	return '{| style="width: 100%"\n' .. table.concat(rows) .. '|}'
end

local redirect_type = {
	-- auto-detected types
	{
		name = 'ရိုမဂျိ',
		article = 'the',
		detect = function(title, defs, cats)
			if m_ja.script(title) ~= 'Romaji' then return {} end
			local rom = mw.ustring.lower(title):gsub('[- ]', '')
			return filter(defs, function(def)
				for _, k in ipairs(def.kana_spellings) do
					if rom == m_tr.kana_to_romaji(k, {language_name = 'ja'}) then
						table.insert(cats, '[[Category:ဂျပန် ရောမအက္ခရာဖလှယ်ခြင်းများ|' .. mw.ustring.upper(title) .. ']]')
						return true
					end
				end
				return false
			end)
		end,
		display = function(title)
			return '<span lang="ja" class="Latn">' .. text .. '</span>'
		end,
		no_cat = true,
	},
	{
		name = 'ဟိရဂန စာလုံးပေါင်း',
		article = 'the',
		detect = function(title, defs, cats)
			if m_ja.script(title) ~= 'Hira' then return {} end
			local rom = mw.ustring.lower(m_tr.kana_to_romaji(title, {language_name = 'ja'})):gsub('[- ]', '')
			return filter(defs, function(def)
				if not contains(def.kana_spellings, title) and contains(def.historical_kana_spellings, title) then
					return false
				end
				for _, k in ipairs(def.kana_spellings) do
					if rom == m_tr.kana_to_romaji(k, {language_name = 'ja'}) then
						return true
					end
				end
				return false
			end)
		end,
	},
	{
		name = 'ခတခန စာလုံးပေါင်း',
		article = 'the',
		detect = function(title, defs, cats)
			if m_ja.script(title) ~= 'Kana' then return {} end
			local rom = mw.ustring.lower(m_tr.kana_to_romaji(title, {language_name = 'ja'})):gsub('[- ]', '')
			return filter(defs, function(def)
				if not contains(def.kana_spellings, title) and contains(def.historical_kana_spellings, title) then
					return false
				end
				for _, k in ipairs(def.kana_spellings) do
					if rom == m_tr.kana_to_romaji(k, {language_name = 'ja'}) then
						return true
					end
				end
				return false
			end)
		end,
	},
	{
		name = 'historical kana spelling',
		article = 'a',
		detect = function(title, defs, cats)
			return filter(defs, function(def)
				if not contains(def.kana_spellings, title) and contains(def.historical_kana_spellings, title) then
					local sc = m_ja.script(title)
					if sc == 'Hira' then
						table.insert(cats, '[[Category:Japanese historical hiragana]]')
					elseif sc == 'Kana' then
						table.insert(cats, '[[Category:Japanese historical katakana]]')
					elseif sc == 'Hira+Kana' then
						table.insert(cats, '[[Category:Japanese terms spelled with mixed historical kana]]')
					end
					return true
				end
			end)
		end,
	},
	{
		name = 'kyūjitai',
		article = 'the',
		detect = function(title, defs, cats)
			local shin = title:gsub('.[\128-\191]*', function(c)
				if c == '辨' or c == '辯' or c == '瓣' then return '弁' end
				return d_kyu[1]:match('(%S*)' .. c .. '%s') or c
			end)
			if shin == title then return {} end
			return mw.ustring.len(title) == 1 and filter(defs, function(def) return def.header == "Kanji" end) or filter(defs, function(def)
				if shin == def.title then
					if def.kana_spellings[1] then
						table.insert(cats, '[[Category:Japanese kyūjitai spellings|' .. require'Module:Jpan-sortkey'.sortkey_from_string(def.kana_spellings[1]) .. ']]')
					end
					return true
				end
			end)
		end,
		display = function(title)
			return ja(title:gsub('.[\128-\191]*', function(c)
				return d_kyu[1]:match(c .. '(&#x%x+;)%s') or c
			end))
		end,
		no_cat = true,
	},
	{
		name = 'kyūjitai of an alternative spelling',
		article = 'the',
		detect = function(title, defs, cats, key)
			local shin = title:gsub('.[\128-\191]*', function(c)
				if c == '辨' or c == '辯' or c == '瓣' then return '弁' end
				return d_kyu[1]:match('(%S*)' .. c .. '%s') or c
			end)
			if shin == title then return {} end
			return filter(defs, function(def)
				if shin ~= def.title and contains(def.kanji_spellings, shin) and (not key or contains(def.kana_spellings, key)) then
					if def.kana_spellings[1] then
						table.insert(cats, '[[Category:Japanese kyūjitai spellings|' .. require'Module:Jpan-sortkey'.sortkey_from_string(def.kana_spellings[1]) .. ']]')
					end
					return true
				end
			end)
		end,
		display = function(title)
			return ja(title:gsub('.[\128-\191]*', function(c)
				return d_kyu[1]:match(c .. '(&#x%x+;)%s') or c
			end))
		end,
		display_labels = function(title, defs, cats)
			local shin = title:gsub('.[\128-\191]*', function(c)
				if c == '辨' or c == '辯' or c == '瓣' then return '弁' end
				return d_kyu[1]:match('(%S*)' .. c .. '%s') or c
			end)
			return '(' .. link(shin) .. ')'
		end,
		no_cat = true,
	},
	{
		name = 'မူကွဲ စာလုံးပေါင်း',
		article = 'an',
		detect = function(title, defs, cats, key)
			return filter(defs, function(def)
				return contains(def.kanji_spellings, title) and (not key or contains(def.kana_spellings, key)) or contains(def.kana_spellings, title)
			end)
		end,
		display_labels = function(title, defs, cats)
			for _, def in ipairs(defs) do for _, kl in ipairs(def.kanji_spellings_with_labels) do
				local ks, lb = kl:match'^(.-):(.+)$'
				if ks == title then
					return require("Module:labels").show_labels { labels = mw.text.split(lb, ' '), lang = lang, nocat = true }
				end
			end end
			return ''
		end,
		is_fallback = true,
	},
	-- manual input only types
	['vk'] = {
		name = 'variant kanji form',
		article = 'a',
		detect = function(title, defs, cats) return mw.ustring.len(title) == 1 and filter(defs, function(def) return def.header == "Kanji" end) or defs end,
	},
	['iter'] = {
		name = 'form with iteration marks',
		article = 'a',
		detect = function(title, defs, cats) return defs end,
	},
	['niter'] = {
		name = 'form without iteration marks',
		article = 'a',
		detect = function(title, defs, cats) return defs end,
	},
	['eshin'] = {
		name = 'extended shinjitai',
		article = 'an',
		detect = function(title, defs, cats) return mw.ustring.len(title) == 1 and filter(defs, function(def) return def.header == "Kanji" end) or defs end,
	},
	names = {
		['rom'] = 1,
		['hira'] = 2,
		['kata'] = 3,
		['hkana'] = 4,
		['kyu'] = 5,
		['kyualt'] = 6,
		['alt'] = 7,
		
		['vk'] = 'vk',
		['iter'] = 'iter',
		['niter'] = 'niter',
		['eshin'] = 'eshin',
	},
}

function export.show(frame)
	local title = mw.loadData("Module:headword/data").pagename -- Accounts for unsupported titles and sets the page's DEFAULTSORTKEY.
	
	local args = require("Module:parameters").process(frame:getParent().args, {
		[1] = { list = true },
		['type'] = { list = true, allow_holes = true, separate_no_index = true },
		['key'] = { list = true, allow_holes = true },
	})
			
	local result = {
		'{| class="wikitable ja-see" style="min-width:70%"\n|-\n| <b>',
		' ', ja(title), ' အတွက် အသံထွက်နှင့် အဓိပ္ပာယ်များအတွက် – အောက်ပါ',
		#args[1] > 1 and ' ရေးသွင်းချက်များကို ကြည့်ပါ' or ' ရေးသွင်းချက်ကို ကြည့်ပါ',
		'။', 
		'</b>',
	}
	local bad_redirects = {}
	
	local name_count = 0
	local name_previous, rt_previous
	
	local function make_footnote()
		table.insert(result, '\n|-\n| (ဤဝေါဟာရ, ')
		table.insert(result, (rt_previous.display or ja)(title))
		table.insert(result, ', သည် ')
		table.insert(result, ' အထက်ပါ ဝေါဟာရ၏')
		if name_count > 1 then table.insert(result, 'များ') end
		table.insert(result, name_previous)
		table.insert(result, ' ဖြစ်သည်။)')
	end
	
	local function add_to_result(name, rt, defs)		
		if rt.display_labels then
			name = name .. ' ' .. rt.display_labels(title, defs, result)
		end
		
		if name_previous and name_previous ~= name then
			make_footnote()
			name_count = 1
		else
			name_count = name_count + 1
		end
						
		name_previous = name
		rt_previous = rt
	
		table.insert(result, '\n|-\n| style="background-color: white" |\n')
		table.insert(result, format_table_content(defs, frame, title, rt.no_cat))
	end
	
	for i_lemma, lemma in ipairs(args[1]) do
		local defs = get_definitions_from_entry(lemma)
		--mw.logObject(defs) --use this to inspect "defs"
		
		local label_lemma = args.type[i_lemma] or args.type.default
		if label_lemma then
			local ll1, ll2 = label_lemma:match'^(.-)(%S*)$'
			local rt = redirect_type[redirect_type.names[ll2]]
			if rt then
				defs = rt.detect(title, defs, result)
				if #defs > 0 then
					add_to_result((label_lemma:match'^an? ' or label_lemma:match'^the ') and ll1 .. rt.name or rt.article .. ' ' .. ll1 .. rt.name, rt, defs)
				else
					table.insert(bad_redirects, lemma)
				end
			else
				add_to_result(label_lemma, {}, defs)
			end
		else
			local success = false
			for _, rt in ipairs(redirect_type) do
				if not (success and rt.is_fallback) then
					local defs_try = rt.detect(title, defs, result, args.key[i_lemma])
					if #defs_try > 0 then
						success = true
						add_to_result(rt.article .. ' ' .. rt.name, rt, defs_try)
					end
				end
			end
			if not success then
				table.insert(bad_redirects, lemma)
			end
		end
	end	
	
	if name_previous then
		make_footnote()
		if mw.title.new('Category:Japanese kanji read as ' .. title).exists then
			table.insert(result, '<br><span style="font-size:85%;">For a list of all kanji read as ')
			table.insert(result, ja(title))
			table.insert(result, ', see ')
			table.insert(result, '[[:Category:Japanese kanji read as ')
			table.insert(result, title)
			table.insert(result, ']].)</span>')
		end
		table.insert(result, '\n|}')
	else -- failure to find any definitions
		result[6] = ': ' .. table.concat(map(args[1], function(title) return '<span style="font-size:120%">' .. link(title) .. '</span>' end), ', ') .. '\n|}'
	end
	
	if #bad_redirects > 0 then
		table.insert(result, '\n<small class="attentionseeking">(အောက်ပါ ')
		table.insert(result, #bad_redirects == 1 and 'ရေးသွင်းချက်သည်' or 'ရေးသွင်းချက်များသည်')
		table.insert(result, ' မဖန်တီးရသေးပါ: ')
		table.insert(result, table.concat(map(bad_redirects, link), ", "))
		table.insert(result, '.)</small>[[Category:Japanese redlinks/ja-see]]')
	end
	
	return table.concat(result)
end

function export.show_kango(frame) -- to be abolished
	return export.show(frame, 'kango')
end

function export.show_gv(frame) -- to be abolished
	return export.show(frame, 'glyphvar')
end

return export