Module:ja-new

Hello, you have come here looking for the meaning of the word Module:ja-new. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:ja-new, but we will also tell you about its etymology, its characteristics and you will know how to say Module:ja-new in singular and plural. Everything you need to know about the word Module:ja-new you have here. The definition of the word Module:ja-new will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:ja-new, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.

Used by Template:ja-new. This module should not be transcluded by any page (check).


local export = {}

local pagename = mw.loadData("Module:headword/data").pagename

local find = mw.ustring.find
local length = mw.ustring.len
local sub, gsub = mw.ustring.sub, mw.ustring.gsub
local match, gmatch = mw.ustring.match, mw.ustring.gmatch

local m_ja = require("Module:ja")

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

function export.new(frame, _args) -- _args is used when called from other Lua code
	
	local args = _args or frame:getParent().args
	
	local pos_table_convert = {
		 = { "n", "vsuru" },
		 = { "n", "vsuru" },
		 = { "a", "n" },
		 = { "a", "n" },
		 = { "adv", "vsuru" },
	}

	local pos_table = {
		 = { "Noun", "noun" },  
		 = { "Verb", "verb" },  
		 = { "Verb", "verb-suru" },
		 = { "Verb", "verb form" },
		 = { "Adjective", "adj" },
		 = { "Adverb", "pos|adverb" },
		 = { "Pronoun", "pos|pronoun" },
		 = { "Proper noun", "pos|proper" },
		 = { "Phrase", "phrase" },
		 = { "Interjection", "pos|interjection" },
		 = { "Conjunction", "pos|conjunction" },
		 = { "Particle", "pos|particle" },
		 = { "Preposition", "pos|preposition" },
		 = { "Suffix", "pos|suffix" },
		 = { "Prefix", "pos|prefix" },
		 = { "Affix", "pos|affix" },
		 = { "Proverb", "pos|proverb" },
		 = { "Idiom", "pos|idiom" },
		 = { "Adnominal", "pos|adnominal" },
	}
	local _pos_table = pos_table; pos_table = {}
	for k, v in pairs(_pos_table) do for i in mw.text.gsplit(k, "/") do pos_table = v end end
	
	local function waapuro_r_to_kana(text)
		return require("Module:typing-aids").replace{"ja", text}
	end

	local result = args and ("===Etymology " .. args .. "===") or "==Japanese=="
	local hf = args and "=" or ""

	if args then
		result = result .. "\n{{wp|lang=ja" .. (args ~= "y" and "|" .. args or "") .. "}}"
	end
	wp_count = 2
	while args do
		result = result .. "\n{{wp|lang=ja|" .. args .. "}}"
		wp_count = wp_count + 1
	end

	if args then
		result = result .. "\n{{swp|lang=ja" .. (args ~= "y" and "|" .. args or "") .. "}}"
	end
	swp_count = 2
	while args do
		result = result .. "\n{{swp|lang=ja|" .. args .. "}}"
		swp_count = swp_count + 1
	end

	text = args ~= "" and args or pagename
	text = gsub(text, "%-", "|")
	text = waapuro_r_to_kana(text)

	local function make_tab(original, yomi)
		output_text = ""
		original = gsub(original, " ", "|")
		original = gsub(original, "%.", "|")
		original = gsub(original, "%^", "")
		if find(original, "<") then
			for word in gmatch(original, "<(+)>") do
				output_text = output_text .. "|" .. word
			end
			yomi = yomi or "k"
		else
			output_text = gsub(original, ">()", "|k%1=")
			output_text = find(output_text, "|") and "|" .. output_text or false
		end
		yomi = yomi or (find(original, "%|") and "o") or "n"
		return "\n{{ja-kanjitab" ..
			(output_text or "") ..
			(yomi == "n" and "" or "|yomi=" .. yomi) ..
			(args and "|sort=" .. args or "") ..
			(args and "|r=" .. args or "") ..
			(args and "|kyu=" .. args or "") ..
			(args and "|alt=" .. args or "") .. "}}", yomi
	end

	if find(pagename, "") or args then
		to_add, yomi = make_tab(text, args)
		result = result .. to_add
	end

	if find(text, "<") then
		text = gsub(text, "", "")
	else
		text = gsub(text, "^+>%d+(+)", "%1")
		text = gsub(text, "|+>%d+(+)", "%1")
		text = gsub(text, "()|(あ)", "%1.%2")
		text = gsub(text, "()|(い)", "%1.%2")
		text = gsub(text, "()|(う)", "%1.%2")
		text = gsub(text, "()|()", "%1.%2")
		text = gsub(text, "()|()", "%1.%2")
		text = gsub(text, "|", "")
	end

	if args then
		result = result .. "\n{{swp|lang=ja" .. (args ~= "y" and "|" .. args or "") .. "}}"
	end
	wpd_count = 2
	while args do
		result = result .. "\n{{swp|lang=ja|" .. args .. "}}"
		wpd_count = wpd_count + 1
	end

	if args then
		result = result .. args
	end
	pic_count = 2
	while args do
		result = result .. args
		swp_count = pic_count + 1
	end

	local function other(class, title, args)
		local code, i = "", 2

		if args then
			code = code .. "\n\n===" .. hf .. title .. hf .. "===\n* {{ja-l|" .. args .. "}}"

			while args do
				code = code .. "\n* {{ja-l|" .. args .. "}}"
				i = i + 1
			end
		end

		code = gsub(code, "{{ja%-l|(+)", "{{ja-r|%1|") -- change something like "{{ja-l|辞典:じてん}}" to "{{ja-r|辞典|じてん}}"
		code = gsub(code, "{{ja%-l|(+)}}", "{{ja-r|%1}}") -- change something like "{{ja-l|じてん}}" to "{{ja-r|じてん}}"

		return code
	end

	result = result .. other("alt", "Alternative forms", args)

	if args or args or args or args or args or args or args or args or args or args or args then
		result = result .. "\n\n"
		if not args then result = result .. hf .. "===Etymology===" .. hf .. "\n" end
		if args then
			result = result .. "{{waei|" .. args .. "|" .. args
			we_count = 3
			while args do
				result = result .. "|" .. args
				we_count = we_count + 1
			end
			result = result .. (args and "|sort=" .. args or "") .. "}}"
			if args then
				result = result .. ", {{cal|ja|nocap=y|" .. (args or "en") .. (args and "|" .. args or "") .. (args and "|tr=" .. args or "") .. (args and "||" .. args or "") .. (args and "|lit=" .. args or "") .. (args and "|pos=" .. args or "") .. (args and "|g=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
			end
			if args then
				result = result .. ", equivalent to {{pre|ja|" .. args .. "|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t1=" .. args or "") .. (args and "|t2=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
			elseif args then
				result = result .. ", equivalent to {{suf|ja|" .. args .. "|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t1=" .. args or "") .. (args and "|t2=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
			elseif args then
				result = result .. ", equivalent to {{af|ja|" .. args .. (args and "|alt1=" .. args or "") .. (args and "|tr1=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|t1=" .. args or "") .. "|" .. args .. (args and "|alt2=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t2=" .. args or "")
				co_count = 3
				while args do
					result = result .. "|" .. args .. (args and "|alt" .. co_count .. "=" .. args or "") .. (args and "|tr" .. co_count .. "=" .. args or "") .. (args and "|pos" .. co_count .. "=" .. args or "") .. (args and "|t" .. co_count .. "=" .. args or "")
					co_count = co_count + 1
				end
				result = result .. (args and "|sort=" .. args or "") .. "}}"
			elseif args then
				result = result .. ", equivalent to a {{blend|ja|nocap=y|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|t1=" .. args or "") .. "|" .. args .. (args and "|tr2=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t2=" .. args or "")
				bl_count = 3
				while args do
					result = result .. "|" .. args .. (args and "|tr" .. bl_count .. "=" .. args or "") .. (args and "|pos" .. bl_count .. "=" .. args or "") .. (args and "|t" .. bl_count .. "=" .. args or "")
					bl_count = bl_count + 1
				end
				result = result .. (args and "|sort=" .. args or "") .. "}}"
			end
		elseif args then
			result = result .. "{{ja-yoji|" .. args .. "|" .. args .. (args and "|" .. args or "") .. (args and "|" .. args or "") .. (args and "|tr1=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|tr3=" .. args or "") .. (args and "|tr4=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|pos3=" .. args or "") .. (args and "|pos4=" .. args or "") .. (args and "|t1=" .. args or "") .. (args and "|t2=" .. args or "") .. (args and "|t3=" .. args or "") .. (args and "|t4=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
		elseif args then
			result = result .. "{{cal|ja|" .. (args or "en") .. (args and "|" .. args or "") .. (args and "|tr=" .. args or "") .. (args and "||" .. args or "") .. (args and "|lit=" .. args or "") .. (args and "|pos=" .. args or "") .. (args and "|g=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
			if args then
				result = result .. ", equivalent to {{pre|ja|" .. args .. "|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t1=" .. args or "") .. (args and "|t2=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
			elseif args then
				result = result .. ", equivalent to {{suf|ja|" .. args .. "|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t1=" .. args or "") .. (args and "|t2=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
			elseif args then
				result = result .. ", equivalent to {{af|ja|" .. args .. (args and "|alt1=" .. args or "") .. (args and "|tr1=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|t1=" .. args or "") .. "|" .. args .. (args and "|alt2=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t2=" .. args or "")
				co_count = 3
				while args do
					result = result .. "|" .. args .. (args and "|alt" .. co_count .. "=" .. args or "") .. (args and "|tr" .. co_count .. "=" .. args or "") .. (args and "|pos" .. co_count .. "=" .. args or "") .. (args and "|t" .. co_count .. "=" .. args or "")
					co_count = co_count + 1
				end
				result = result .. (args and "|sort=" .. args or "") .. "}}"
			elseif args then
				result = result .. ", equivalent to a {{blend|ja|nocap=y|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|t1=" .. args or "") .. "|" .. args .. (args and "|tr2=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t2=" .. args or "")
				bl_count = 3
				while args do
					result = result .. "|" .. args .. (args and "|tr" .. bl_count .. "=" .. args or "") .. (args and "|pos" .. bl_count .. "=" .. args or "") .. (args and "|t" .. bl_count .. "=" .. args or "")
					bl_count = bl_count + 1
				end
				result = result .. (args and "|sort=" .. args or "") .. "}}"
			end
		elseif args then
			result = result .. "From {{pre|ja|" .. args .. "|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t1=" .. args or "") .. (args and "|t2=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
		elseif args then
			result = result .. "From {{suf|ja|" .. args .. "|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t1=" .. args or "") .. (args and "|t2=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
		elseif args then
			result = result .. "From {{af|ja|" .. args .. (args and "|alt1=" .. args or "") .. (args and "|tr1=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|t1=" .. args or "") .. "|" .. args .. (args and "|alt2=" .. args or "") .. (args and "|tr2=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t2=" .. args or "")
			co_count = 3
			while args do
				result = result .. "|" .. args .. (args and "|alt" .. co_count .. "=" .. args or "") .. (args and "|tr" .. co_count .. "=" .. args or "") .. (args and "|pos" .. co_count .. "=" .. args or "") .. (args and "|t" .. co_count .. "=" .. args or "")
				co_count = co_count + 1
			end
			result = result .. (args and "|sort=" .. args or "") .. "}}"
		elseif args then
			result = result .. "{{blend|ja|" .. args .. (args and "|tr1=" .. args or "") .. (args and "|pos1=" .. args or "") .. (args and "|t1=" .. args or "") .. "|" .. args .. (args and "|tr2=" .. args or "") .. (args and "|pos2=" .. args or "") .. (args and "|t2=" .. args or "")
			bl_count = 3
			while args do
				result = result .. "|" .. args .. (args and "|tr" .. bl_count .. "=" .. args or "") .. (args and "|pos" .. bl_count .. "=" .. args or "") .. (args and "|t" .. bl_count .. "=" .. args or "")
				bl_count = bl_count + 1
			end
			result = result .. (args and "|sort=" .. args or "") .. "}}"
		elseif args then
			result = result .. "From {{bor|ja|" .. (args or "en") .. (args and "|" .. args or "") .. (args and "|tr=" .. args or "") .. (args and "||" .. args or "") .. (args and "|lit=" .. args or "") .. (args and "|pos=" .. args or "") .. (args and "|g=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
		elseif args then
			result = result .. "{{lbor|ja|" .. (args or "grc") .. (args and "|" .. args or "") .. (args and "|tr=" .. args or "") .. (args and "||" .. args or "") .. (args and "|lit=" .. args or "") .. (args and "|pos=" .. args or "") .. (args and "|g=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"
		else
			result = result .. (args or
				("From {{der|ja|" .. (args or "en") .. (args and "|" .. args or "") .. (args and "|tr=" .. args or "") .. (args and "||" .. args or "") .. (args and "|lit=" .. args or "") .. (args and "|pos=" .. args or "") .. (args and "|g=" .. args or "") .. (args and "|sort=" .. args or "") .. "}}"))
		end
		result = result .. (args and args or "")
	end

	if not args then
		result = result .. "\n\n" .. hf .. "===Pronunciation===" .. hf .. "\n{{ja-pron" .. (args ~= "" and "|" .. gsub(text, '%^', '') or "")
		result = result .. (args and "|acc=" .. args or "") .. (args and "|acc_ref=" .. args or "")
		acc_count = 2
		while args do
			result = result .. "|acc" .. acc_count .. "=" .. args .. (args and "|acc" .. acc_count .. "_ref=" .. args or "")
			acc_count = acc_count + 1
		end
		result = result .. (args and "|dev=" .. args or "") .. (args and "|devm=" .. args or "") .. "}}"
		if args then
			if yomi and not (yomi == "n" or y == "n") then
				result = result .. "\n** "
			else
				result = result .. "\n* "
			end
			result = result .. (args and "{{hmp|ja|" .. args or "")
			hmp_count = 2
			while args do
				result = result .. "|" .. args
				hmp_count = hmp_count + 1
			end
			result = result .. (args and "|sort=" .. args or "") .. "}}"
		end
	end
	
	if not args then args = args end
	if not args then args = args end
	if not args then args = args end
	if not args then args = args end
	if args then
		args = args .. "\n#" .. args
	end
	local eg_count = 2
	while args do
		args = args .. "\n#" .. args
		eg_count = eg_count + 1
	end
	
	-- by this time the unnamed args should be either
	-- {{subst:ja-new|<kana>|<first POS>|<first def>|<second POS>|<second def>|...}} or
	-- {{subst:ja-new|<kana>|<first and second POS>|<first def>|<second def>}}
	-- our task here is to convert the second format to the first
	
	if pos_table_convert] then
		if args then error("use the simpler POS codes, for example change \"suru|def1|def2\" to \"n|def1|vsuru|def2\"") end
		args, args, args, args = pos_table_convert], args, pos_table_convert], args
	end
	
	local function add_pos(header, template_name, def)
		result = result .. "\n\n" .. hf .. "===" .. header .. "===" .. hf .. "\n{{ja-" .. template_name ..
			(args ~= "" and "|" .. text or "") ..
			(args and "|head=" .. args or "") ..
			(template_name == "noun" and args and "|count=" .. args or "") ..
			(args and "|shin=" .. args or "") ..
			(args and "|" .. waapuro_r_to_kana(args) or "") ..
			(args and "|" .. waapuro_r_to_kana(args) or "") ..
			(args and "|" .. waapuro_r_to_kana(args) or "") .. -- TODO: ak
			(args and "|rom=" .. args or "") ..
			(args and "|hhira=" .. waapuro_r_to_kana(args) or "") ..
			(args and "|hkata=" .. waapuro_r_to_kana(args) or "")

		if header == "Adjective" then
			result = result .. "|infl=" .. (args and args or "na")
		end

		if template_name == "verb" then result = result .. (args and "|type=" .. args or "") end
		if template_name == "verb" or template_name == "verb-suru" then result = result .. (args and "|tr=" .. args or "") end
		result = result .. "}}"
		
		result = result .. "\n\n# " .. (def or "{{rfdef|ja}}")

		if header == "Adjective" then
			result = result .. "\n\n" .. hf .. "====Inflection====" .. hf .. "\n"
			if args == "i" or args == "い" then
				result = result .. "{{ja-i" .. (args ~= "" and "|" .. sub(text, 1, -2) or "") .. "}}"
			elseif not args or args == "na" or args == "な" then
				result = result .. "{{ja-na" .. (args ~= "" and "|" .. text or "") .. "}}"
			end
		end

		if template_name == "verb" then
			result = result .. "\n\n" .. hf .. "====Conjugation====" .. hf .. "\n{{ja-"
			if args == "2" then
				result = result .. "ichi"
			else
				local final = table.remove(m_ja.moraify(text))
				final = require("Module:Hrkt-translit").tr(final)
				result = result .. "go-" .. final:match("^*") .. "u"
			end
			result = result .. (args ~= "" and "|" .. sub(text, 1, -2) or "") .. "}}"
		end
		
		if template_name == "verb-suru" then
			result = result .. "\n\n" .. hf .. "====Conjugation====" .. hf .. "\n{{ja-suru" .. (args ~= "" and "|" .. text or "") .. "}}"
		end
	end
	
	if not pos_table] then error("The POS code " .. args .. " is not valid. See ] for a list of valid POS.") end
	add_pos(pos_table], pos_table], args)
	if args then
		result = result .. "\n\n" .. hf .. "====Usage notes====" .. hf .. "\n* " .. args
	end
	result = result .. other("syn", "=Synonyms=", args)
	result = result .. other("ant", "=Antonyms=", args)
	result = result .. other("der", "=Derived terms=", args)
	result = result .. other("rel", "=Related terms=", args)
	
	if args then
		if not pos_table] then error("The POS code " .. args .. " is not valid. See ] for a list of valid POS.") end
		add_pos(pos_table], pos_table], args)
		if args then
			result = result .. "\n\n" .. hf .. "====Usage notes====" .. hf .. "\n* " .. args
		end
		result = result .. other("Syn", "=Synonyms=", args)
		result = result .. other("Ant", "=Antonyms=", args)
		result = result .. other("Der", "=Derived terms=", args)
		result = result .. other("Rel", "=Related terms=", args)
	end
	
	if args then
		if not pos_table] then error("The POS code " .. args .. " is not valid. See ] for a list of valid POS.") end
		add_pos(pos_table], pos_table], args)
		if args then
			result = result .. "\n\n" .. hf .. "====Usage notes====" .. hf .. "\n* " .. args
		end
		result = result .. other("SYN", "=Synonyms=", args)
		result = result .. other("ANT", "=Antonyms=", args)
		result = result .. other("DER", "=Derived terms=", args)
		result = result .. other("REL", "=Related terms=", args)
	end
	
	if args then error("this template only supports up to three POS, please ask someone to expand me") end

	if args or args or args or args then
		result = result .. "\n\n" .. hf .. "===Descendants===" .. hf .. "\n" .. (args and "* {{desc|zh|" .. args .. (args and "|bor=y" or "") .. (args and "|der=y" or "") .. (args and "|clq=y" or "") .. "}}" or "")
		if args then
			if args or args or args then
				result = result .. "\n"
			end
		end
		result = result .. (args and "* {{desc|en|" .. args .. (args and "|bor=y" or "") .. (args and "|der=y" or "") .. (args and "|clq=y" or "") .. "}}" or "")
		if args then
			if args or args then
				result = result .. "\n"
			end
		end
		result = result .. (args and "* {{desc|ko|" .. args .. (args and "|bor=y" or "") .. (args and "|der=y" or "") .. (args and "|clq=y" or "") .. "}}" or "")
		if args then
			if args then
				result = result .. "\n"
			end
		end
		result = result .. (args and "* {{desc|vi|" .. args .. (args and "|bor=y" or "") .. (args and "|der=y" or "") .. (args and "|clq=y" or "") .. "}}" or "")
	end
	result = result .. other("ana", "Anagrams", args)
	result = result .. other("also", "See also", args)
	
	local ref
	if args then
		ref = args ~= "n"
	else
		ref = args or args
	end
	if ref then
		result = result .. "\n\n===References===\n<references/>"
	end

	if args then
		result = result .. "\n\n{{cln|ja|" .. args
	cn_count = 2
	while args do
		result = result .. "|" .. args
		cn_count = cn_count + 1
	end
		result = result .. (args and "|sort=" .. args or "") .. "}}"
	end
	if args then
		result = result .. (args and "\n" or "\n\n") .. "{{C|ja|" .. args
	ct_count = 2
	while args do
		result = result .. "|" .. args
		ct_count = ct_count + 1
	end
		result = result .. (args and "|sort=" .. args or "") .. "}}"
	end

	if args then
		result = result .."\n\n==Korean==\n{{ko-hanjatab}}\n\n===" .. pos_table or "n"] ..
			"===\n{{ko-" .. pos_table or "n"] .. "|hangeul=" .. args .. (args and "|mr=" .. args or "") .. (args and "|y=" .. args or "") .. "}}" ..
			"\n\n# {{hanja form of|" .. args .. "|" .. (args or args) .. "}}"
	end

	return result
end

function export.add(frame)
	local args = frame:getParent().args
	
	local page = mw.title.new(pagename):getContent()
	if not find(page, '==Japanese==\n') then return export.new(nil, args) end
	page = match(page, '==Japanese==\n.-%-%-%-%-') or match(page, '==Japanese==\n.*')
	
	-- ensure that there is no whitespace between = and heading title (WT:NORM)
	page = gsub(page, '(\n===*) *(-) *(===*\n)', function(a,b,c) return a .. b .. c end)
	
	local kireme = find(page, '\n===References===') or find(page, '\n%-%-%-%-$')
	if kireme then kireme = kireme + 1 else
		page = gsub(page, '\n$', '') .. '\n\n'
		kireme = find(page, '$')
	end
	local main_part = sub(page, 1, kireme - 1)
	local ending_part = sub(page, kireme, length(page))
	if find(ending_part, '===References===') then args = 'n' end
	
	local function ja_new_and_korean(args)
		local ja_new = export.new(nil, args)
		local korean = nil
		if find(ja_new, '\n\n%-%-%-%-\n\n==Korean==') then
			_, _, ja_new, korean = find(ja_new, '(.+)\n\n%-%-%-%-\n\n(==Korean==.+)')
		end
		return ja_new .. '\n\n', korean
	end
	
	local result, new_section, korean_section
	
	-- case 1, the current entry has multiple Etymology sections, and our task is to append a new etymology section at the end
	local largest_etym_number
	for number in gmatch(main_part, '===Etymology (%d+)===') do largest_etym_number = number end
	if largest_etym_number then
		args = tostring(tonumber(largest_etym_number) + 1)
		new_section, korean_section = ja_new_and_korean(args)
		result = main_part .. new_section .. ending_part
	else
		-- case 2, the current entry has only one word with POS headers on L3, or no word at all (with the sole L3 header being ===Kanji===)
		local kireme
		for l3_name in gmatch(main_part, '\n===(+)') do
			if not find(l3_name, '^Kanji') and not kireme then
				kireme = find(main_part, '\n===' .. l3_name) + 1
			end
		end
		if kireme then
			-- case 2a, the current entry has only one word, and we should put the existing word under Etymology 1 and add the new word as Etymology 2
			local header_and_kanji_section = sub(main_part, 1, kireme - 1)
			local existing_word = sub(main_part, kireme, length(main_part))
			if not find(existing_word, '===Etymology===\n') then existing_word = '===Etymology===\n\n' .. existing_word end
			existing_word = gsub(existing_word, '^===Etymology===', '===Etymology 1===')
			existing_word = gsub(existing_word, '(\n===*) *(-) *(===*\n)', function(a,b,c) return a .. '=' .. b .. '=' .. c end)
			if find(header_and_kanji_section, '{{ja%-kanjitab') then
				local kanjitab = match(header_and_kanji_section, '{{ja%-kanjitab.-\n')
				header_and_kanji_section = gsub(header_and_kanji_section, '{{ja%-kanjitab.-\n', '')
				existing_word = gsub(existing_word, '^===Etymology 1===', function (a) return a .. '\n' .. kanjitab end)
			end
			args = "2"
			new_section, korean_section = ja_new_and_korean(args)
			result = header_and_kanji_section .. existing_word .. new_section .. ending_part
		else
			-- case 2b, the current entry has only a ===Kanji=== section
			new_section, korean_section = ja_new_and_korean(args)
			new_section = gsub(new_section, '^==Japanese==\n*', '')
			result = main_part .. new_section .. ending_part
		end
	end
	
	if korean_section then result = result .. '\n' .. korean_section end
	result = gsub(result, '\n\n\n', '\n\n')
	return result
end

return export