Modul:ja

Wiktionary saytından

Bu modulun sənədləşdirmə səhifəsi Modul:ja/doc səhifəsində yaradıla bilər

local export = {}

-- note that arrays loaded by mw.loadData cannot be directly used by mw.ustring.gsub
local data = mw.loadData("Module:ja/data")

export.data = {
	joyo_kanji = data.joyo_kanji, 
	jinmeiyo_kanji = data.jinmeiyo_kanji, 
	grade1 = data.grade1, 
	grade2 = data.grade2, 
	grade3 = data.grade3, 
	grade4 = data.grade4, 
	grade5 = data.grade5, 
	grade6 = data.grade6
}

function export.romaji_dediacritics(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	return (mw.ustring.gsub(text, '.', function (char) return data.rd[char] or char end))
end

function export.hira_to_kata(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	return (mw.ustring.gsub(text, '.', function (char) return data.hk[char] or char end))
end

function export.kata_to_hira(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	return (mw.ustring.gsub(text, '.', function (char) return data.kh[char] or char end))
end

function export.kana_to_romaji(text, nodiacr, keepdot)
	if type(text) == "table" then -- assume a frame.
		text, nodiacr = text.args[1], text.args[2]
	end
	
	-- local kr_minus_period = data.kr
	-- kr_minus_period["。"] = "。"
	
	-- convert Japanese spaces to western spaces
	text = mw.ustring.gsub(text, ' ', ' ')
	
	text = mw.ustring.gsub(text, 'っ%%', 'っ')
	text = mw.ustring.gsub(text, 'ッ%%', 'ッ')
	
	-- if there is a は separated by halfwidth spaces, romanize it as " wa "
	text = mw.ustring.gsub(text, ' は ', ' wa ')
	-- also if it follows a space and is the last character, e.g. それでは
	text = mw.ustring.gsub(text, ' は$', ' wa')
	-- or " は、" 
	text = mw.ustring.gsub(text, ' は、', ' wa, ')
	-- or " は。" 
	text = mw.ustring.gsub(text, ' は。', ' wa. ')
	text = mw.ustring.gsub(text, ' は?', ' wa? ')
	text = mw.ustring.gsub(text, ' は)', ' wa)')
	-- or " '''は''' "
	text = mw.ustring.gsub(text, " '''は''' ", " '''wa''' ")
	-- romanize では as "dewa"
	text = mw.ustring.gsub(text, ' では ', ' dewa ')
	text = mw.ustring.gsub(text, ' では$', ' dewa')
	text = mw.ustring.gsub(text, ' では、', ' dewa, ')
	text = mw.ustring.gsub(text, ' では。', ' dewa. ')
	text = mw.ustring.gsub(text, ' では?', ' dewa? ')
	text = mw.ustring.gsub(text, ' では)', ' dewa)')
	text = mw.ustring.gsub(text, "'''では'''", "'''dewa'''")
	-- romanize で は (with space) as "de wa"
	text = mw.ustring.gsub(text, "'''で は'''", "'''de wa'''")
	-- same sort of thing for へ
	text = mw.ustring.gsub(text, ' へ ', ' e ')
	text = mw.ustring.gsub(text, " '''へ''' ", " '''e''' ")
	text = mw.ustring.gsub(text, ' へ$', ' e')
	text = mw.ustring.gsub(text, ' へ、', ' e, ')
	text = mw.ustring.gsub(text, ' へ。', ' e. ')
	text = mw.ustring.gsub(text, ' へ?', ' e?')
	text = mw.ustring.gsub(text, ' へ)', ' e)')
	-- dangling small tsu is romanized as nothing
	text = mw.ustring.gsub(text, 'ッ。', '。')
	text = mw.ustring.gsub(text, 'ッ!', '!')
	text = mw.ustring.gsub(text, 'ッ」', '」')
	text = mw.ustring.gsub(text, 'ッ、', '、')
	
	-- ゝ means "repeat the previous character" and is used with hiragana, like 々 is for kanji
	-- TODO: do same sort of thing for ゞ
	text = mw.ustring.gsub(text, '(.)ゝ', '%1%1')
	
	-- romanising ヶ
	text = mw.ustring.gsub(text, 'ヶげつ', 'kagetsu')
	text = mw.ustring.gsub(text, 'ヶ', 'ga')

	-- convert hiragana to katakana
	text = mw.ustring.gsub(text, '.', function (char) return data.hk[char] or char end)
	-- replace katakana with romaji (?? not sure what the pattern below does ??)
	-- this is hackish, but we're using the period to indicate morpheme boundaries to prevent macrons 
	-- from forming across them, so we'll remove the ASCII periods used for markup but not the Japanese periods
	
	-- convert the Japanese periods at the end
	--table.remove(kr_minus_period, "。")
	text = mw.ustring.gsub(text, '.[ィェォャュョァヮゥ゜]?ェ?',
	                       function (char)
	                       	if char == "。" then
	                       		return char
	                       	else
	                       		return data.kr[char] or char
	                       	end
	                       end)
	
	-- replace long vowel mark with the vowel that comes before
	text = mw.ustring.gsub(text, '([aeiou])ー', '%1%1')
	
	-- add vowels with diacritics 
	if not nodiacr then
		text = mw.ustring.gsub(text, 'oo', 'ō')
		text = mw.ustring.gsub(text, 'aa', 'ā')
		text = mw.ustring.gsub(text, 'ee', 'ē')
		text = mw.ustring.gsub(text, 'ou', 'ō')
		text = mw.ustring.gsub(text, 'uu', 'ū')
		text = mw.ustring.gsub(text, 'ii', 'ī')
	end

	if not keepdot then
		-- if input had spaces, keep them
		-- if the input string had periods, then remove them now
		text = mw.ustring.gsub(text, '%.', '')
		-- now that markup periods are gone, convert the Japanese periods to western periods
		text = mw.ustring.gsub(text, "。", ". ")
	end

	-- romanize sokuon or geminate consonants
	-- text = mw.ustring.gsub(text, '^ッ', '')
	-- double the previous consonant letter if there is a small tsu
	text = mw.ustring.gsub(text, 'ッ([kstpgdbjzrfh])', '%1%1')
	text = mw.ustring.gsub(text, 'ッ\'\'\'([kstpgdbjzrfh])', '%1\'\'\'%1')
	-- replace ッc with tc
	text = mw.ustring.gsub(text, 'ッc', 'tc')
	text = mw.ustring.gsub(text, 'ッ\'\'\'c', 't\'\'\'c')
	-- if small tsu comes at the end, just throw it away
	text = mw.ustring.gsub(text, 'ッ$', '')

	-- the @ is used to determine when to insert an opostrophe after ん or ン
	-- (all is kata at that point)
	-- insert apostrophe when ン is followed by a vowel or 
	-- y, which corresponds to the cases んや (n'ya) んゆ (n'yu) and んよ (n'yo)
	text = mw.ustring.gsub(text, "@([aeiouāēīōūy])", "'%1")
	-- remove @
	text = mw.ustring.gsub(text, "@", "")
	
	-- capitalize any letter following a ^ symbol
	text = mw.ustring.gsub(text, "%^%l", mw.ustring.upper)
	-- remove ^
	text = mw.ustring.gsub(text, "%^", "")
	-- remove %
	text = mw.ustring.gsub(text, '%%', '')
	
	return text
end

-- removes spaces and hyphens from input
-- intended to be used when checking manual romaji to allow the 
-- insertion of spaces or hyphens in manual romaji without appearing "wrong"
function export.rm_spaces_hyphens(f)
	local text = f.args[1]
	text = mw.ustring.gsub(text, ' ', '')
	text = mw.ustring.gsub(text, '-', '')
	text = mw.ustring.gsub(text, '%.', '')
	text = mw.ustring.gsub(text, ' ', '')
	text = mw.ustring.gsub(text, '\'', '')
	return text
end

function export.romaji_to_kata(f)
	local text = f.args[1]
	text = mw.ustring.gsub(text, '.', function (char) return data.rd[char] or char end)
	text = mw.ustring.gsub(text, 'kk', 'ッk')
	text = mw.ustring.gsub(text, 'ss', 'ッs')
	text = mw.ustring.gsub(text, 'tt', 'ッt')
	text = mw.ustring.gsub(text, 'pp', 'ッp')
	text = mw.ustring.gsub(text, 'bb', 'ッb')
	text = mw.ustring.gsub(text, 'dd', 'ッd')
	text = mw.ustring.gsub(text, 'gg', 'ッg')
	text = mw.ustring.gsub(text, 'jj', 'ッj')
	text = mw.ustring.gsub(text, 'tc', 'ッc')
	text = mw.ustring.gsub(text, 'tsyu', 'ツュ')
	text = mw.ustring.gsub(text, 'ts[uoiea]', {['tsu']='ツ',['tso']='ツォ',['tsi']='ツィ',['tse']='ツェ',['tsa']='ツァ'})
	text = mw.ustring.gsub(text, 'sh[uoiea]', {['shu']='シュ',['sho']='ショ',['shi']='シ',['she']='シェ',['sha']='シャ'})
	text = mw.ustring.gsub(text, 'ch[uoiea]', {['chu']='チュ',['cho']='チョ',['chi']='チ',['che']='チェ',['cha']='チャ'})
	text = mw.ustring.gsub(text, "n[uoiea']?", {['nu']='ヌ',['no']='ノ',['ni']='ニ',['ne']='ネ',['na']='ナ',['n']='ン',["n'"]='ン'})
	text = mw.ustring.gsub(text, '[wvtrpsmlkjhgfdbzy][yw]?[uoiea]', function (char) return data.rk[char] or char end)
	text = mw.ustring.gsub(text, 'u', 'ウ')
	text = mw.ustring.gsub(text, 'o', 'オ')
	text = mw.ustring.gsub(text, 'i', 'イ')
	text = mw.ustring.gsub(text, 'e', 'エ')
	text = mw.ustring.gsub(text, 'a', 'ア')
	return text
end

-- expects: any mix of kanji and kana
-- determines the script types used
-- e.g. given イギリス人, it returns Kana+Hani
function export.script(f)
	text, script = type(f) == 'table' and f.args[1] or f, {}
	
	if mw.ustring.match(text, '[ぁ-ゖ]') then
		table.insert(script, 'Hira')
	end
	-- TODO: there are two kanas.  This should insert Kata.
	if mw.ustring.match(text, '[ァ-ヺー]') then
		table.insert(script, 'Kana')
	end
	-- 一 is unicode 4e00, previously used 丁 is 4e01
	if mw.ustring.match(text, '[㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]') then
		table.insert(script, 'Hani')
	end
	-- matching %a should have worked but matched the end of every string
	if mw.ustring.match(text, '[a-zA-ZāēīōūĀĒĪŌŪa-zA-Z]') then
		table.insert(script, 'Romaji')
	end
	if mw.ustring.match(text, '[0-90-9]') then
		table.insert(script, 'Number')
	end
	if mw.ustring.match(text, '[〆々]') then
		table.insert(script, 'Abbreviation')
	end
	
	return table.concat(script, '+')
end

-- accepts the entry name, extracts the kanji, and
-- puts the kanji inside {{ja-kanji|}} and returns it
function export.extract_kanji(f)
	local text = f.args[1]
	local len = 1
	local result = ''
	text = mw.ustring.gsub(mw.ustring.gsub(text, ".", function (char) return data.ky[char] or char end), ".", function (char) return data.hy[char] or char end)
	text = mw.ustring.gsub(text,' ','')
	len = mw.ustring.len(text)
	if text ~= '' then
		result = '{{ja-kanjitab'
--		for i = 1, len, 1 do
--			char = mw.ustring.sub(text,i,i)
--			result = (result .. '|' .. char)
--		end
		result = (result .. '}}')
	end
	return result
end

-- returns the number of kanji in this term 
function export.count_kanji(f)
	local text = f.args[1]
	local len = 1
	-- replace 時々 with 時時
	text = mw.ustring.gsub(text, '([㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟])々', '%1%1')
	-- See w:Template:CJK_ideographs_in_Unicode (U+3400 - U+4DB5, U+4E00 - U+9FCC, U+F900 (escaped to avoid normalisation) - U+FAD9 (escaped to avoid normalisation), U+20000 - U+2FA1F)
	text = mw.ustring.gsub(text, '[^㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]', '')
	len = mw.ustring.len(text)
	return len
end

-- used within other functions but >> no longer necessary <<
-- returns a hidx-style hiragana sort key attached to |hidx=, 
-- e.g. |hidx=はつぐん' when given ばつぐん
function export.hidx(f)
	local text = f.args[1]
	local textsub = ''
	local convertedten = ''
	local result = ''
	local len = 1
	local kyreplace = ''
	kyreplace = mw.ustring.gsub(text,'.',function (char) return data.ky[char] or char end)
	if kyreplace == '' then
		result = ('|' .. 'hidx' .. '=')
	end
	text = mw.ustring.gsub(text,'.',function (char) return data.kh[char] or char end)
	if mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.dakuten[char] or char end) == '' then
		if kyreplace == '' then else result = ('|' .. 'hidx' .. '=') end
		len = mw.ustring.len(text)
		textsub = mw.ustring.sub(text,2,len)
		convertedten = mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.tenconv[char] or char end)
		result = (result .. convertedten .. textsub .. "'")
	else
		if mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.handakuten[char] or char end) == '' then
			if kyreplace == '' then else result = ('|' .. 'hidx' .. '=') end
			len = mw.ustring.len(text)
			textsub = mw.ustring.sub(text,2,len)
			convertedten = mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.tenconv[char] or char end)
			result = (result .. convertedten .. textsub .. "''")
		else
			if kyreplace == '' then
				result = (result .. text)
			end
		end
	end
	return result
end
   
-- when counting morae, most small hiragana belong to the previous mora, 
-- so for purposes of counting them, they can be removed and the characters 
-- can be counted to get the number of morae.  The exception is small tsu, 
-- so data.nonmora_to_empty maps all small hiragana except small tsu.
function export.count_morae(text)
	if type(text) == "table" then
		text = text.args[1]
	end
   -- convert kata to hira (hira is untouched)
   text = mw.ustring.gsub(text, '.', function (char) return data.kh[char] or char end)
   -- remove all of the small hiragana such as ょ except small tsu
   text = mw.ustring.gsub(text,'.',function (char) return data.nonmora_to_empty[char] or char end)
   -- remove zero-width spaces
   text = mw.ustring.gsub(text, '‎', '')
   -- return number of characters, which should be the number of morae
   return mw.ustring.len(text)
end    

-- accepts: any mix of kana
-- returns: a hiragana sort key designed for WMF software (hidx of old)
-- this is like hidx above but doesn't return |hidx=sortkey, 
-- just the sort key itself, but unlike hidx above, this
-- replaces the long vowel mark with its vowel
function export.jsort(text)
	if type(text) == "table" then
		text = text.args[1]
	end
	local textsub = ''
	local convertedten = ''
	local result = ''
	local len = 1

	-- remove western spaces, hyphens, and periods
	text = mw.ustring.gsub(text, '[ %-%.]', '')

	text = mw.ustring.gsub(text,'.',function (char) return data.kh[char] or char end)
	
	-- if the first character has dakuten, replace it with the corresponding 
	-- character without dakuten and add an apostrophe to the end, e.g. 
	-- がす > かす'
	if mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.dakuten[char] or char end) == '' then
		len = mw.ustring.len(text)
		textsub = mw.ustring.sub(text,2,len)
		convertedten = mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.tenconv[char] or char end)
		text = (convertedten .. textsub .. "'")
	else
		-- similar thing but with handuken and two apostrophes, e.g. ぱす -> はす''
		if mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.handakuten[char] or char end) == '' then
			len = mw.ustring.len(text)
			textsub = mw.ustring.sub(text,2,len)
			convertedten = mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.tenconv[char] or char end)
			text = (convertedten .. textsub .. "''")
		end
	end
	-- replace the long vowel mark with the vowel that it stands for
	for key,value in pairs(data.longvowels) do
		text = mw.ustring.gsub(text,key,value)
	end
	return text
end

-- returns 'yes' if the string contains kana (not exactly is kana)
-- returns 'no' otherwise, including if string is empty
function export.is_kana(f)
	local text = f.args[1]
	if mw.ustring.match(text, '[ぁ-ゖ]') then
		return 'yes'
	end
	if mw.ustring.match(text, '[ァ-ヺ]') then
		return 'yes'
	end
	return 'no'
end

-- returns a sort key with |sort= in front, e.g.
-- |sort=はつぐん' if given ばつぐん
function export.sort(f)
	local text = type(text) == 'table' and f.args[1] or f
	local textsub = ''
	local convertedten = ''
	local result = ''
	local len = 1
	local kyreplace = ''
	kyreplace = mw.ustring.gsub(text,'.',function (char) return data.ky[char] or char end)
	if kyreplace == '' then
		result = ('|' .. 'sort' .. '=')
	end
	text = mw.ustring.gsub(text,'.',function (char) return data.kh[char] or char end)
	if mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.dakuten[char] or char end) == '' then
		if kyreplace == '' then else result = ('|' .. 'sort' .. '=') end
		len = mw.ustring.len(text)
		textsub = mw.ustring.sub(text,2,len)
		convertedten = mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.tenconv[char] or char end)
		result = (result .. convertedten .. textsub .. "'")
	else
		if mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.handakuten[char] or char end) == '' then
			if kyreplace == '' then else result = ('|' .. 'sort' .. '=') end
			len = mw.ustring.len(text)
			textsub = mw.ustring.sub(text,2,len)
			convertedten = mw.ustring.gsub(mw.ustring.sub(text,1,1),'.',function (char) return data.tenconv[char] or char end)
			result = (result .. convertedten .. textsub .. "''")
		else
			if kyreplace == '' then
				result = (result .. text)
			end
		end
	end
	return result
end

-- returns the "stem" of a verb or -i adjective, that is the term minus the final character
function export.definal(f)
	return mw.ustring.sub(f.args[1],1,(mw.ustring.len(f.args[1])-1))
end

-- this generates links to categories of the form 
-- Category:Japanese terms spelled with (kanji)
-- which was previously done in Template:ja-kanjitab
-- but depended on the editor entering the right kanji 
function export.spelled_with_kanji()
	local PAGENAME = mw.title.getCurrentTitle().text
	--PAGENAME = f.args["pagename"]
	local cats = {}
	local c = ''

	-- remove non-kanji characters
	-- technically 々 is not a kanji, but we want a category for it, so leave it in
	PAGENAME = mw.ustring.gsub(PAGENAME, '[^㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]', '')
	
	local uniquekanji = ""
	for k in mw.ustring.gmatch(PAGENAME,".") do
		if not mw.ustring.find(uniquekanji,k) then uniquekanji = (uniquekanji .. k) end
	end

	for i = 1, mw.ustring.len(uniquekanji) do
		local c = mw.ustring.sub(uniquekanji,i,i)
		table.insert(cats, '[[Category:Japanese terms spelled with ')
		table.insert(cats, c)
		--table.insert(cats, '|')
		--table.insert(cats, sortkey)
		table.insert(cats, ']]')
		--table.insert(cats, "\n")
		--table.insert(cats, '</nowiki>')
	end
	
	return table.concat(cats, '')
end	

-- see also Template:JAruby
-- meant to be called from another module
function export.add_ruby_backend(term, kana, from_ja_link)
	local pattern = "" 
	-- holds the whole segments of markup enclosed in <ruby>...</ruby>
	local ruby_markup = {}
	-- range of kana: '[ぁ-ゖァ-ヺ]'
	-- nonkana: [^ぁ-ゖァ-ヺ]
	local kanji_pattern = "[々㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟0-9]"
	
	-- links without pipes will fail
	term = mw.ustring.gsub(term, '%[%[([^|%]]+)%]%]', '[[%1|%1]]')
	
	-- remove links from kana
	kana = mw.ustring.gsub(kana, '%[%[([^|%]]+)%]%]', '%1')
	kana = mw.ustring.gsub(kana, '%[%[[^%]]+|([^%]]+)%]%]', '%1')
	
	-- build up pattern
	-- escape the magic characters in the term
	pattern = mw.ustring.gsub(term, '%[%[[^%]]+|([^%]]+)%]%]', '%1')
	pattern = require("Module:utilities").pattern_escape(pattern)
	
	pattern = mw.ustring.gsub(pattern, "[%[%]]+", " *")
	kana = mw.ustring.gsub(kana, "[%[%]]+", '')
	pattern = mw.ustring.gsub(pattern, " *('+) *", "%1")
	kana = mw.ustring.gsub(kana, " *('+) *", "%1")
	pattern = mw.ustring.gsub(pattern, " +", " ")
	kana = mw.ustring.gsub(kana, " +", " ")
	
	-- remove periods and caret signs and hyphens
	pattern = mw.ustring.gsub(pattern, '%%[%.%^%-]', '')
	kana = mw.ustring.gsub(kana, '[%.%^%-]', '')
	
	-- in order to make a pattern that will find the ruby, 
	-- replace every unbroken string of kanji with a sub-pattern 
	pattern = mw.ustring.gsub(pattern, kanji_pattern .. '+', '(.+)')
	-- get a pattern like 
	-- (.+)ばか(.+)ばか(.+)ばかばかばああか(.+) when given 超ばか猿超ばか猿超ばかばかばああか猿
	-- it turns out we need to keep the spaces sometimes
	-- so that kana don't "leak" in ambiguous cases like 捨すてて撤退 where it's not clear if it's 
	-- す, てったい or すて, ったい.  only solution now is to put spaces in the "term" param 
	-- if they fall between kana
	
	-- build up term (e.g. [[歌う|歌った]])
	local replaced = {}
	local count = 0
	term = mw.ustring.gsub(term, '%]', '%]') -- escape the "]" character so that it cannot appear, example becomes [[歌う|歌った%]%]
	term = mw.ustring.gsub(term, kanji_pattern .. '+', function(text)
		count = count + 1
		table.insert(replaced, text)
		return '[' .. count .. ']'
	end) -- example becomes [[[1]う|[2]った%]%]
	
	-- remove spaces
	for i,val in ipairs(replaced) do replaced[i] = mw.ustring.gsub(val, ' ', '') end
	
	while mw.ustring.match(term, '%[%[[^|]*%[[0-9]+%][^|]*|') do
		term = mw.ustring.gsub(term, '(%[%[[^|]*)%[([0-9]+)%]([^|]*|)', function(a,b,c)
			return a .. replaced[tonumber(b)] .. c
		end)
	end -- example becomes [[歌う|[2]った%]%]
	
	-- apply that pattern to the kana to collect the rubies
	-- if this fails, try it without spaces
	if mw.ustring.match(kana, pattern) == nil then kana = mw.ustring.gsub(kana, ' ', '') end
	
	local ruby = { mw.ustring.match(kana, pattern) }
	-- local ruby = {}
	-- for c in mw.ustring.gmatch(kana, pattern) do table.insert(ruby, c) end
	
	-- find the kanji strings again and combine them with their ruby to make the <ruby> markup
	local kanji_segments = {}
	for c in mw.ustring.gmatch(term, '%[([0-9]+)%]') do table.insert(kanji_segments, replaced[tonumber(c)]) end
	
	for i = 1, #kanji_segments do
		table.insert(ruby_markup, "<ruby>" .. kanji_segments[i] .. "<rp>&nbsp;(</rp><rt>" .. ruby[i] .. "</rt><rp>) </rp></ruby>") 
	end
	
	count = 0
	term = mw.ustring.gsub(term, '%[[0-9]+%]', function()
		count = count + 1
		return ruby_markup[count]
	end)
	
	term = mw.ustring.gsub(term, '%%%]', ']')
	term = mw.ustring.gsub(term, '%%', '')
	term = mw.ustring.gsub(term, ' ', '')
	
	--done
	return '<span style="font-size: 1.2em">' .. term .. '</span>'
end

-- replaces the code in Template:ja-readings which accepted kanji readings
-- and displayed them in a consistent format 
function export.readings(frame)
	-- only do this if this entry is a kanji page and not some user's page
	local PAGENAME = mw.title.getCurrentTitle().text
	if mw.ustring.match(PAGENAME, "[㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]") then
		local args = frame:getParent().args
		
		local goon = args["goon"] or ""
		local kanon = args["kanon"] or ""
		local toon = args["toon"] or ""
		local soon = args["soon"] or ""
		local kanyoon = args["kanyoon"] or ""
		local on = args["on"] or ""
		local kun = args["kun"] or ""
		local nanori = args["nanori"] or ""
		local nazuke = args["nazuke"] or ""
		local nadzuke = args["nadzuke"] or ""
		-- my new field for the actual reading of the _kanji_, not the entire word (which may not even
		-- be written with that kanji) which is what "kun" presently has
		local corekun = args["corekun"] or ""

		-- this holds the finished product composed of wikilinks to be displayed 
		-- in the Readings section under the Kanji section
		local links = {}
		
		if goon ~= "" then table.insert(links, "* '''[[呉音|Goon]]''': " .. goon) end 
		if kanon ~= "" then table.insert(links, "* '''[[漢音|Kan’on]]''': " .. kanon) end 
		if toon ~= "" then table.insert(links, "* '''[[唐音|Tōon]]''': " .. toon) end 
		if soon ~= "" then table.insert(links, "* '''[[宋音|Sōon]]''': " .. soon) end 
		if kanyoon ~= "" then table.insert(links, "* '''[[慣用音|Kan’yōon]]''': " .. kanyoon) end 
	
		if on ~= "" then 
			if goon == "" and kanon == "" and toon == "" and soon == "" and kanyoon == "" then 
				table.insert(links, "* '''[[on'yomi|On]]''': " .. on) 
			else
				table.insert(links, "* '''[[on'yomi|On]]''' (unclassified): " .. on) 
			end	
		end
	
		if kun ~= "" then table.insert(links, "* '''[[kun'yomi|Kun]]''': " .. kun) end 
	
		-- three names for the same thing 
		if nanori ~= "" then 
			table.insert(links, "* '''[[nanori|Nanori]]''': " .. nanori)
		elseif nazuke ~= "" then 
			table.insert(links, "* '''[[nanori|Nanori]]''': " .. nazuke)
		elseif nadzuke ~= "" then
			table.insert(links, "* '''[[nanori|Nanori]]''': " .. nadzuke)
		end
		
		-- add kanji readings categories
		-- range of hiragana: [ぁ-ゖ]
		
		-- determine if this is joyo kanji (常用) or jinmeiyo kanji (人名用) or neither (表外)
		local joyo_kanji_pattern = ('[' .. data.joyo_kanji .. ']')
		local jinmeiyo_kanji_pattern = ('[' .. data.jinmeiyo_kanji .. ']')
		local sortkey = ""
		if mw.ustring.match(PAGENAME, joyo_kanji_pattern) then sortkey = "Common" 
		elseif mw.ustring.match(PAGENAME, jinmeiyo_kanji_pattern) then sortkey = "Names" 
		else
			sortkey = "Uncommon" 
		end
	
		local all_usable_readings = (goon .. kanon .. toon .. soon .. kanyoon .. on .. corekun)
		for r in mw.ustring.gmatch(all_usable_readings, "[{%[]([ぁ-ゖ].-)[}%]]") do 
			table.insert(links, "[[Category:Japanese kanji read as " .. r .. "|" .. sortkey .. "]]") 
		end
		
		-- readings should only be in hiragana
		if mw.ustring.match(all_usable_readings, '[ァ-ヺ]') then
			table.insert(links, "[[Category:Japanese kanji needing attention]]") 
		end
		
		return table.concat(links, "\n")
	end
end

-- do the work of Template:ja-kanji
function export.kanji(frame)
	local PAGENAME = mw.title.getCurrentTitle().text
	-- only do this if this entry is a kanji page and not some user's page
	if mw.ustring.match(PAGENAME, "[㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]") then 
		local args = frame:getParent().args
		local grade = args["grade"] or ""
		local rs = args["rs"] or ""
		local style = args["style"] or ""
		local shin = args["shin"] or ""
		local kyu = args["kyu"] or ""
	
		local wikitext = {}
		local categories = {}
		
		local catsort = (rs ~= "") and rs or PAGENAME
		
		-- display the kanji itself at the top at 275% size
		table.insert(wikitext, '<div><span class="Jpan" style="font-size:275%; line-height: 100%;">' .. PAGENAME .. '</span></div>')
		
		-- display information for the grade	
		
		-- if grade was not specified, determine it now
		if grade == "" then
			local joyo_kanji_pattern = ('[' .. data.joyo_kanji .. ']')
			local jinmeiyo_kanji_pattern = ('[' .. data.jinmeiyo_kanji .. ']')
			if mw.ustring.match(PAGENAME, joyo_kanji_pattern) then grade = "c" 
			elseif mw.ustring.match(PAGENAME, jinmeiyo_kanji_pattern) then grade = "n" 
			else
				grade = "uc" 
			end
		end
		
		table.insert(wikitext, "(''")
		if grade == "1" then table.insert(wikitext, "[[w:Kyōiku kanji|grade 1 “Kyōiku” kanji]]")
		elseif grade == "2" then table.insert(wikitext, "[[w:Kyōiku kanji|grade 2 “Kyōiku” kanji]]")
		elseif grade == "3" then table.insert(wikitext, "[[w:Kyōiku kanji|grade 3 “Kyōiku” kanji]]")
		elseif grade == "4" then table.insert(wikitext, "[[w:Kyōiku kanji|grade 4 “Kyōiku” kanji]]")
		elseif grade == "5" then table.insert(wikitext, "[[w:Kyōiku kanji|grade 5 “Kyōiku” kanji]]")
		elseif grade == "6" then table.insert(wikitext, "[[w:Kyōiku kanji|grade 6 “Kyōiku” kanji]]")
		elseif grade == "7" or grade == "c" then table.insert(wikitext, "[[w:Jōyō kanji|common “Jōyō” kanji]]")
		elseif grade == "8" or grade == "n" then table.insert(wikitext, "[[w:Jinmeiyō kanji|“Jinmeiyō” kanji used for names]]")
		elseif grade == "9" or grade == "uc" then table.insert(wikitext, "[[w:Hyōgai kanji|uncommon “Hyōgai” kanji]]")
		elseif grade == "0" or grade == "r" then table.insert(wikitext, "[[w:Radical_(Chinese_character)|Radical]]")
		else 
			table.insert(categories, "[[Category:Japanese terms needing attention/kanji grade]]")
		end
		
		-- if style was indicated, mention that and provide link to corresponding kanji 
		-- (link to shinjitai if this is kyujitai, link to kyujitai if this is shinjitai)
	
		if style == "s" then
			table.insert(wikitext, ",&nbsp;")
			if kyu == "" then 
				table.insert(wikitext, "[[shinjitai]] kanji") 
			else
				table.insert(wikitext, '[[shinjitai]] kanji, [[kyūjitai]] form <span lang="ja" class="Jpan">[[' .. kyu .. '#Japanese|' .. kyu .. ']]</span>') 
			end
		elseif style == "ky" then 
			table.insert(wikitext, ",&nbsp;")
			if shin == "" then
				table.insert(wikitext, "[[kyūjitai]] kanji") 
			else
				table.insert(wikitext, '[[kyūjitai]] kanji, [[shinjitai]] form <span lang="ja" class="Jpan">[[' .. shin .. '#Japanese|' .. shin .. "]]</span>") 
			end
		end
		table.insert(wikitext, "'')")
	
		-- add categories
		table.insert(categories, "[[Category:Japanese Han characters|" .. catsort .. "]]")
		if grade == "1" then table.insert(categories, "[[Category:Grade 1 kanji|" .. catsort .. "]]") 
		elseif grade == "2" then table.insert(categories, "[[Category:Grade 2 kanji|" .. catsort .. "]]")
		elseif grade == "3" then table.insert(categories, "[[Category:Grade 3 kanji|" .. catsort .. "]]")
		elseif grade == "4" then table.insert(categories, "[[Category:Grade 4 kanji|" .. catsort .. "]]")
		elseif grade == "5" then table.insert(categories, "[[Category:Grade 5 kanji|" .. catsort .. "]]")
		elseif grade == "6" then table.insert(categories, "[[Category:Grade 6 kanji|" .. catsort .. "]]")
		elseif grade == "7" or grade == "c" then table.insert(categories, "[[Category:Common kanji|" .. catsort .. "]]")
		elseif grade == "8" or grade == "n" then table.insert(categories, "[[Category:Kanji used for names|" .. catsort .. "]]")
		elseif grade == "9" or grade == "uc" then table.insert(categories, "[[Category:Uncommon kanji|" .. catsort .. "]]")
		elseif grade == "0" or grade == "r" then table.insert(categories, "[[Category:CJKV radicals| ]]")
		end
		
		-- error category
		if rs == "" then table.insert(categories, "[[Category:Japanese terms needing attention/radical and strokes]]") end
		
		return table.concat(wikitext, "") .. table.concat(categories, "\n")
	end
end

local grade1_pattern = ('[' .. data.grade1 .. ']')
local grade2_pattern = ('[' .. data.grade2 .. ']')
local grade3_pattern = ('[' .. data.grade3 .. ']')
local grade4_pattern = ('[' .. data.grade4 .. ']')
local grade5_pattern = ('[' .. data.grade5 .. ']')
local grade6_pattern = ('[' .. data.grade6 .. ']')
local secondary_pattern = ('[' .. data.secondary .. ']')
local jinmeiyo_kanji_pattern = ('[' .. data.jinmeiyo_kanji .. ']')
local hyogaiji_pattern = ('[^' .. data.joyo_kanji .. data.jinmeiyo_kanji .. ']')

function export.kanji_grade(kanji)
	if type(kanji) == "table" then
		kanji = kanji.args[1]
	end

	if mw.ustring.match(kanji, hyogaiji_pattern) then return 9 
	elseif mw.ustring.match(kanji, jinmeiyo_kanji_pattern) then return 8 
	elseif mw.ustring.match(kanji, secondary_pattern) then return 7 
	elseif mw.ustring.match(kanji, grade6_pattern) then return 6 
	elseif mw.ustring.match(kanji, grade5_pattern) then return 5 
	elseif mw.ustring.match(kanji, grade4_pattern) then return 4 
	elseif mw.ustring.match(kanji, grade3_pattern) then return 3 
	elseif mw.ustring.match(kanji, grade2_pattern) then return 2 
	elseif mw.ustring.match(kanji, grade1_pattern) then return 1
	end

	return false
end

function export.new(frame)
	local args = frame:getParent().args
	local result = "==Japanese=="
	if args["defs"] then
		result = result .. "\n{{DEFAULTSORT:" .. args["defs"] .. "}}"
	end
	if args["wp"] then
	if args["wp"] == "y" then
		result = result .. "\n{{wikipedia|lang=ja}}"
		else
		result = result .. "\n{{wikipedia|lang=ja|" .. args["wp"] .. "}}"
	end
	end
	if args["wp2"] then
		result = result .. "\n{{wikipedia|lang=ja|" .. args["wp2"] .. "}}"
	end
	if args["wp3"] then
		result = result .. "\n{{wikipedia|lang=ja|" .. args["wp3"] .. "}}"
	end
	if args["wp4"] then
		result = result .. "\n{{wikipedia|lang=ja|" .. args["wp4"] .. "}}"
	end
	if args["wp5"] then
		result = result .. "\n{{wikipedia|lang=ja|" .. args["wp5"] .. "}}"
	end
	if args["wp6"] then
		result = result .. "\n{{wikipedia|lang=ja|" .. args["wp6"] .. "}}"
	end
	pagename = mw.title.getCurrentTitle().text
	text = args[1] ~= "" and args[1] or pagename
	text = mw.ustring.gsub(text, "%-", "|")
	
	local function make_tab(original, yomi)
		output_text = ""
		original = mw.ustring.gsub(original, " ", "|")
		if mw.ustring.match(original, "<") then
			for word in mw.ustring.gmatch(original, "<([^>]+)>") do
				output_text = output_text .. "|" .. word
			end
			yomi = "k"
		else
			output_text = mw.ustring.gsub(original, ">([1-9])", "|k%1=")
			output_text = mw.ustring.match(output_text, "|") and "|" .. output_text or false
		end
		yomi = yomi or "o"
		return "\n{{ja-kanjitab" .. (output_text or "") .. "|yomi=" .. yomi .. (args["yomi"] == "irr" and "" or sortkey or "") .. "}}", yomi
	end
	if mw.ustring.match(pagename, "[㐀-䶵一-鿌\239\164\128-\239\171\153𠀀-𯨟]") then
		to_add, yomi = make_tab(text, args["yomi"])
		result = result .. to_add
	end
	
	if mw.ustring.match(text, "<") then
		text = mw.ustring.gsub(text, "[<>]", "")
	else
		text = mw.ustring.gsub(text, "^[^>|]+>[0-9]+([^>|]+)", "%1")
		text = mw.ustring.gsub(text, "|[^>|]+>[0-9]+([^>|]+)", "%1")
		text = mw.ustring.gsub(text, "([あかがさざただなはばぱまやらわ])|(あ)", "%1.%2")
		text = mw.ustring.gsub(text, "([いきぎしじちぢにひびぴみり])|(い)", "%1.%2")
		text = mw.ustring.gsub(text, "([うくぐすずつづぬふむゆる])|(う)", "%1.%2")
		text = mw.ustring.gsub(text, "([えけげせぜてでねへめれ])|([えい])", "%1.%2")
		text = mw.ustring.gsub(text, "([おこごそぞとどのほぼぽもよろ])|([おう])", "%1.%2")
		text = mw.ustring.gsub(text, "|", "")
	end
	
	local function other(class, title, args)
		local code = ""
		if args[class] then
			code = code .. "\n\n===" .. title .. "===\n* {{ja-l|" .. args[class] .. "}}"
			
			if args[class .. "2"] then
				code = code .. "\n* {{ja-l|" .. args[class .. "2"] .. "}}"
				
				if args[class .. "3"] then
					code = code .. "\n* {{ja-l|" .. args[class .. "3"] .. "}}"
					
					if args[class .. "4"] then
						code = code .. "\n* {{ja-l|" .. args[class .. "4"] .. "}}"
					end
				end
			end
		end
		return code
	end

	result = result .. other("alt", "Alternative forms", args)
	
	sortkey = export.script(text) == "Kana" and export.sort(text) or false
	if sortkey and sortkey == "|sort=" .. text then
		sortkey = false
	end
			
	if args["d"] or args["e"] or args["we1"] or args["b"] or args["co1"] or args["pr1"] or args["su1"] then
		result = result .. "\n\n===Etymology===\n"
		if args["we1"] then
			result = result .. "{{waei|" .. args["we1"] .. (args["we2"] and "|" .. args["we2"] or "") .. (args["defs"] and "" or sortkey or "") .. "}}"
		else
			if args["pr1"] then
				result = result .. "{{pre|ja|" .. args["pr1"] .. "|" .. args["pr2"] .. (args["defs"] and "" or sortkey or "") .. (args["tr1"] and "|tr1=" .. args["tr1"] or "") .. (args["tr2"] and "|tr2=" .. args["tr2"] or "") .. (args["t1"] and "|t1=" .. args["t1"] or "") .. (args["t2"] and "|t2=" .. args["t2"] or "") .. "}}"
			else
				if args["su1"] then
					result = result .. "{{suf|ja|" .. args["su1"] .. "|" .. args["su2"] .. (args["defs"] and "" or sortkey or "") .. (args["tr1"] and "|tr1=" .. args["tr1"] or "") .. (args["tr2"] and "|tr2=" .. args["tr2"] or "") .. (args["t1"] and "|t1=" .. args["t1"] or "") .. (args["t2"] and "|t2=" .. args["t2"] or "") .. "}}"
				else
					if args["co1"] then
						result = result .. "{{com|ja|" .. args["co1"] .. "|" .. args["co2"] .. (args["co3"] and "|" .. args["co3"] or "") .. (args["co4"] and "|" .. args["co4"] or "") .. (args["co5"] and "|" .. args["co5"] or "") .. (args["co6"] and "|" .. args["co6"] or "") .. (args["tr1"] and "|tr1=" .. args["tr1"] or "") .. (args["tr2"] and "|tr2=" .. args["tr2"] or "") .. (args["tr3"] and "|tr3=" .. args["tr3"] or "") .. (args["tr4"] and "|tr4=" .. args["tr4"] or "") .. (args["tr5"] and "|tr5=" .. args["tr5"] or "") .. (args["tr6"] and "|tr6=" .. args["tr6"] or "") .. (args["t1"] and "|t1=" .. args["t1"] or "") .. (args["t2"] and "|t2=" .. args["t2"] or "") .. (args["t3"] and "|t3=" .. args["t3"] or "") .. (args["t4"] and "|t4=" .. args["t4"] or "") .. (args["t5"] and "|t5=" .. args["t5"] or "") .. (args["t6"] and "|t6=" .. args["t6"] or "") .. (args["defs"] and "" or sortkey or "") .. "}}"
					else
						if args["b"] then
							result = result .. "{{bor|ja|" .. (args["bl"] or "en") .. (args["b"] and "|" .. args["b"] or "") .. (args["tr"] and "|tr=" .. args["tr"] or "") .. (args["t"] and "|t=" .. args["t"] or "") .. (args["defs"] and "" or sortkey or "") .. "}}"
						else
							result = result .. (args["e"] or
								("From {{der|ja|" .. (args["dl"] or "en") .. (args["d"] and "|" .. args["d"] or "") .. (args["tr"] and "|tr=" .. args["tr"] or "") .. (args["t"] and "|t=" .. args["t"] or "") .. (args["defs"] and "" or sortkey or "") .. "}}"))
						end
					end
				end
			end
		end
	end

if not args["nop"] then
	result = result .. "\n\n===Pronunciation===\n{{ja-pron" .. (args[1] ~= "" and "|" .. text or "")
		if args["y"] == "n" then
			result = result .. ""
				else if args["y"] and args["y"] ~= "n" then
						result = result .. "|y=" .. args["y"]
					 else
						result = result .. (yomi and "|y=" .. yomi or "")
					 end
		end
	result = result .. (args["acc"] and "|acc=" .. args["acc"] or "") .. (args["acc2"] and "|acc2=" .. args["acc2"] or "") .. (args["acc3"] and "|acc3=" .. args["acc3"] or "") .. (args["acc_ref"] and "|acc_ref=" .. args["acc_ref"] or "") .. (args["acc2_ref"] and "|acc2_ref=" .. args["acc2_ref"] or "") .. (args["acc3_ref"] and "|acc3_ref=" .. args["acc3_ref"] or "") .. (args["dev"] and "|dev=" .. args["dev"] or "") .. "}}" .. (args["hmp"] and "\n* {{hmp|lang=ja|" .. args["hmp"] .. (args["hmp2"] and "|" .. args["hmp2"] or "") .. (args["hmp3"] and "|" .. args["hmp3"] or "") .. (args["hmp4"] and "|" .. args["hmp4"] or "") .. (args["hmp5"] and "|" .. args["hmp5"] or "") .. (args["hmp6"] and "|" .. args["hmp6"] or "") .. (args["defs"] and "" or sortkey or "") .. "}}" or "")
end

	local pos = args[2] ~= "" and args[2] or "n"
	local pos_table = {
		[""] = { "Noun", "noun", true },
		["n"] = { "Noun", "noun", true },
		["s"] = { "Noun", "noun", true, "Verb", "verb-suru" },
		["noun"] = { "Noun", "noun", true },
		["suru"] = { "Noun", "noun", true, "Verb", "verb-suru" },
		["an"] = { "Adjective", "adj", true, "Noun", "noun" },
		["anoun"] = { "Adjective", "adj", true, "Noun", "noun" },
		["v"] = { "Verb", "verb", true },
		["verb"] = { "Verb", "verb", true },
		["vform"] = { "Verb", "verb form", true },
		["verb form"] = { "Verb", "verb form", true },
		["a"] = { "Adjective", "adj", true },
		["adj"] = { "Adjective", "adj", true },
		["adjective"] = { "Adjective", "adj", true },
		["adv"] = { "Adverb", "adverb", false },
		["adverb"] = { "Adverb", "adverb", false },
		["pn"] = { "Proper noun", "proper", false },
		["propn"] = { "Proper noun", "proper", false },
		["proper noun"] = { "Proper noun", "proper", false },
		["ph"] = { "Phrase", "phrase", true }, 
		["phrase"] = { "Phrase", "phrase", true },
		["intj"] = { "Interjection", "interjection", false },
		["conj"] = { "Conjunction", "conjunction", false },
		["part"] = { "Particle", "particle", false },
		["prep"] = { "Preposition", "preposition", false },
	}
	
	result = result .. "\n\n===" .. pos_table[pos][1] .. "===\n{{ja-" .. (not pos_table[pos][3] and "pos|" or "") .. pos_table[pos][2] .. 
		(args[1] ~= "" and "|" .. text or "") .. (args["ak"] and "|" .. args["ak"] or "") .. (args["ak2"] and "|" .. args["ak2"] or "") .. (args["ro"] and "|rom=" .. args["ro"] or "") .. (args["hh"] and "|hhira=" .. args["hh"] or "")
		
	if pos_table[pos][1] == "Adjective" then
		result = result .. "|infl=" .. (args["infl"] and args["infl"] or "na")
	end
	
	result = result .. (args["type"] and "|type=" .. args["type"] or "") .. (args["tr"] and "|tr=" .. args["tr"] or "") .. "}}"
	result = result .. "\n\n# " .. args[3]
	
	if pos_table[pos][1] == "Adjective" then
		result = result .. "\n\n====Inflection====\n"
		if args["infl"] == "i" or args["infl"] == "い" then
			result = result .. "{{ja-i" .. (args[1] ~= "" and "|" .. mw.ustring.sub(text, 1, -2) or "") .. "}}"
		else
			result = result .. "{{ja-na" .. (args[1] ~= "" and "|" .. text or "") .. "}}"
		end
	end

	if pos_table[pos][2] == "verb" then
		result = result .. "\n\n====Conjugation====\n{{ja-"
		penul = mw.ustring.sub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.sub(text, -2, -2), ".", function (char) return data.hk[char] or char end), ".", function (char) return data.kr[char] or char end), -1, -1)
		cons = mw.ustring.sub(mw.ustring.gsub(mw.ustring.gsub(mw.ustring.sub(text, -1, -1), ".", function (char) return data.hk[char] or char end), ".", function (char) return data.kr[char] or char end), 1, 1)
		if cons == "u" then
			cons = ""
		elseif cons == "t" then
			cons = "ts"
		end
		if final == "る" and (penul == "i" or penul == "e") and args["type"] == 2 then
			result = result .. "ichi"
		else
			result = result .. "go-" .. cons .. "u"
		end
		
		result = result .. (args[1] ~= "" and "|" .. mw.ustring.sub(text, 1, -2) or "") .. "}}"
	end
	
	if pos_table[pos][4] and args[4] ~= "" then
		result = result .. "\n\n===" .. pos_table[pos][4] .. "===\n{{ja-" .. pos_table[pos][5] .. (args[1] ~= "" and "|" .. text or "") .. 
			(args["type"] and "|type=" .. args["type"] or "") .. (args["tr"] and "|tr=" .. args["tr"] or "") .. "}}\n\n# " .. args[4]
		
		if pos_table[pos][4] == "Verb" then
			result = result .. "\n\n====Conjugation====\n{{ja-suru" .. (args[1] ~= "" and "|" .. text or "") .. "}}"
		end
	end
	
	result = result .. other("syn", "=Synonyms=", args)
	result = result .. other("ant", "=Antonyms=", args)
	result = result .. other("der", "=Derived terms=", args)
	result = result .. other("also", "See also", args)

	if args["acc_ref"] or args["acc2_ref"] or args["acc3_ref"] then
		result = result .. "\n\n===References===\n<references/>"
	end

	if args["cn"] then
		result = result .. "\n\n{{cln|ja|" .. args["cn"] .. (args["cn2"] and "|" .. args["cn2"] or "") .. (args["cn3"] and "|" .. args["cn3"] or "") .. (args["cn4"] and "|" .. args["cn4"] or "") .. (args["cn5"] and "|" .. args["cn5"] or "") .. (args["cn6"] and "|" .. args["cn6"] or "") .. (args["defs"] and "" or sortkey or "") .. "}}"
	end

	if args["ct"] then
		result = result .. "\n\n{{C|ja|" .. args["ct"] .. (args["ct2"] and "|" .. args["ct2"] or "") .. (args["ct3"] and "|" .. args["ct3"] or "") .. (args["ct4"] and "|" .. args["ct4"] or "") .. (args["ct5"] and "|" .. args["ct5"] or "") .. (args["ct6"] and "|" .. args["ct6"] or "") .. (args["defs"] and "" or sortkey or "") .. "}}"
	end

	if args["k"] then
		result = result .."\n\n----\n\n==Korean==\n{{ko-hanjatab}}\n\n===" .. pos_table[args["kp"] or "n"][1] ..
			"===\n{{ko-" .. pos_table[args["kp"] or "n"][2] .. "|hangeul=" .. args["k"] .. (args["mr"] and "|mr=" .. args["mr"] or "") .. (args["yl"] and "|y=" .. args["yl"] or "") .. "}}" .. 
				"\n\n# {{hanja form of|" .. args["k"] .. "|" .. (args["kd"] or args[3]) .. "}}"
	end

	return result
end

return export