يمكن إنشاء صفحة توثيق الوحدة في وحدة:affix/شرح

local m_links = require("Module:links")
local m_utilities = require("Module:utilities")

local export = {}

-- FIXME: should be script-based
-- But we can't do that unless we do script detection before linking.
local hyphens = {
	["ar"] = "ـ",
	["fa"] = "ـ",
	["he"] = "־",
	["ja"] = "",
	["ko"] = "",
	["yi"] = "־",
	["zh"] = "",
}


local function get_hyphen(lang, sc)
	--The script will be "Latn" for transliterations.
	if sc and sc:getCode() == "Latn" then
		return "-"
	else
		return hyphens[lang:getCode()] or "-"
	end
end


local function get_affix_type(lang, sc, part)
	if not part then
		return nil
	end
	
	local hyphen = get_hyphen(lang, sc)
	
	part = part:gsub("^*", "")

	if mw.ustring.sub(part, 1, 1) == hyphen and mw.ustring.sub(part, -1) == hyphen then
		return "infix"
	elseif mw.ustring.sub(part, -1) == hyphen then
		return "prefix"
	elseif mw.ustring.sub(part, 1, 1) == hyphen then
		return "suffix"
	else
		return nil
	end
end


function export.show_affixes(lang, sc, parts, pos, sort_key, nocat)
	pos = pos or "words"
	
	-- Process each part
	local parts_formatted = {}
	local categories_formatted = {}
	local whole_words = 0
	
	for i, part in ipairs(parts) do
		-- Make a link for the part
		local part_formatted = m_links.full_link(part.term, part.alt, part.lang or lang, part.sc or sc, "term", part.id, part.annotations, false)
		
		if part.lang then
			part_formatted = require("Module:etymology language").format(part.lang:getCode(), (not nocat and lang or nil), sort_key) .. " " .. part_formatted
		end
		
		table.insert(parts_formatted, part_formatted)
		
		-- Is it an affix, and if so, what type of affix?
		local affix_type = get_affix_type(part.lang or lang, part.sc or sc, part.term)
		
		if affix_type then
			-- Make a sort key
			-- For the first part, use the second part as the sort key
			local part_sort = part.sort or sort_key
			
			if i == 1 and parts[2] then
				part_sort = part.sort or lang:makeEntryName(parts[2].term)
			end
			
			if affix_type == "infix" then affix_type = "interfix" end
			
			table.insert(categories_formatted, m_utilities.format_categories({lang:getCanonicalName() .. " " .. pos .. " " .. affix_type .. "ed with " .. lang:makeEntryName(part.term)}, lang, part_sort))
		else
			whole_words = whole_words + 1
			if whole_words == 2 then
				table.insert(categories_formatted, m_utilities.format_categories({lang:getCanonicalName() .. " compound " .. pos}, lang, sort_key))
			end
		end
	end
	
	-- If there are no categories, then there were no actual affixes, only regular words.
	-- This function does not support compounds (yet?), so show an error.
	if #categories_formatted == 0 then
		error("The parameters did not include any affixes, and the word is not a compound. Please provide at least one affix.")
	end
	
	return table.concat(parts_formatted, " +‎ ") .. (nocat and "" or table.concat(categories_formatted))
end


function export.show_compound(lang, sc, parts, pos, sort_key, nocat)
	pos = pos or "words"
	local parts_formatted = {}
	local categories_formatted = {}
	table.insert(categories_formatted, m_utilities.format_categories({lang:getCanonicalName() .. " compound words"}, lang, sort_key))
	
	-- Make links out of all the parts
	local whole_words = 0
	for i, part in ipairs(parts) do
		local part_formatted = m_links.full_link(part.term, part.alt, part.lang or lang, part.sc or sc, "term", part.id, part.annotations, false)
		
		if part.lang then
			part_formatted = require("Module:etymology language").format(part.lang:getCode(), (not nocat and lang or nil), sort_key) .. " " .. part_formatted
		end
		
		table.insert(parts_formatted, part_formatted)
		
		local affix_type = get_affix_type(part.lang or lang, part.sc or sc, part.term)
		
		if affix_type == "infix" then
			table.insert(categories_formatted, m_utilities.format_categories({lang:getCanonicalName() .. " " .. pos .. " interfixed with " .. lang:makeEntryName(part.term)}, lang, part.sort or sort_key))
		elseif affix_type then
			require("Module:debug").track("compound")
			require("Module:debug").track("compound/" .. affix_type)
			require("Module:debug").track("compound/" .. affix_type .. "/lang/" .. lang:getCode())
		else
			whole_words = whole_words + 1
		end
	end

	if whole_words == 1 then
		require("Module:debug").track("compound/one whole word")
	elseif whole_words == 0 then
		require("Module:debug").track("compound/looks like confix")
	end
	
	return table.concat(parts_formatted, " +‎ ") .. (nocat and "" or table.concat(categories_formatted))
end


function export.show_circumfix(lang, sc, prefix, base, suffix, pos, sort_key, nocat)
	local categories = {}
	pos = pos or "مفردة"
	
	-- Pluralize the part of speech name
	if pos:find("[sx]$") then
		pos = pos .. "es"
	else
		pos = pos .. "s"
	end
	
	-- Hyphenate the affixes
	prefix.term = export.make_affix(prefix.term, prefix.lang or lang, prefix.sc or sc, "prefix")
	prefix.alt = export.make_affix(prefix.alt, prefix.lang or lang, prefix.sc or sc, "prefix")
	
	if prefix.annotations then
		prefix.annotations.tr = export.make_affix(prefix.annotations.tr, prefix.lang or lang, require("Module:scripts").getByCode("Latn"), "prefix")
	end
	
	suffix.term = export.make_affix(suffix.term, suffix.lang or lang, suffix.sc or sc, "suffix")
	suffix.alt = export.make_affix(suffix.alt, suffix.lang or lang, suffix.sc or sc, "suffix")
	
	if suffix.annotations then
		suffix.annotations.tr = export.make_affix(suffix.annotations.tr, suffix.lang or lang, require("Module:scripts").getByCode("Latn"), "suffix")
	end
	
	local prefix_affix_type = get_affix_type(prefix.lang or lang, prefix.sc or sc, prefix.term)
	if prefix_affix_type ~= "prefix" then
		require("Module:debug").track("circumfix")
		require("Module:debug").track("circumfix/prefix")
		require("Module:debug").track("circumfix/prefix/" .. (prefix_affix_type or "none"))
		require("Module:debug").track("circumfix/prefix/" .. (prefix_affix_type or "none") .. "/lang/" .. lang:getCode())
	end
	
	local base_affix_type = get_affix_type(base.lang or lang, base.sc or sc, base.term)
	if base_affix_type then
		require("Module:debug").track("circumfix")
		require("Module:debug").track("circumfix/base")
		require("Module:debug").track("circumfix/base/" .. base_affix_type)
		require("Module:debug").track("circumfix/base/" .. base_affix_type .. "/lang/" .. lang:getCode())
	end
	
	local suffix_affix_type = get_affix_type(suffix.lang or lang, suffix.sc or sc, suffix.term)
	if suffix_affix_type ~= "suffix" then
		require("Module:debug").track("circumfix")
		require("Module:debug").track("circumfix/suffix")
		require("Module:debug").track("circumfix/suffix/" .. (suffix_affix_type or "none"))
		require("Module:debug").track("circumfix/suffix/" .. (suffix_affix_type or "none") .. "/lang/" .. lang:getCode())
	end
	
	-- Create circumfix term
	local circumfix = nil
	
	if prefix.term and suffix.term then
		circumfix = prefix.term .. " " .. suffix.term
		prefix.alt = prefix.alt or prefix.term
		suffix.alt = suffix.alt or suffix.term
		prefix.term = circumfix
		suffix.term = circumfix
	end
	
	-- Make links out of all the parts
	local parts_formatted = {}
	local sort_base = lang:makeEntryName(base.term)
	
	table.insert(parts_formatted, m_links.full_link(prefix.term, prefix.alt, prefix.lang or lang, prefix.sc or sc, "term", prefix.id, prefix.annotations, false))
	table.insert(parts_formatted, m_links.full_link(base.term, base.alt, base.lang or lang, base.sc or sc, "term", base.id, base.annotations, false))
	table.insert(parts_formatted, m_links.full_link(suffix.term, suffix.alt, suffix.lang or lang, suffix.sc or sc, "term", suffix.id, suffix.annotations, false))
	
	-- Insert the categories
	table.insert(categories, lang:getCanonicalName() .. " " .. pos .. " circumfixed with " .. lang:makeEntryName(circumfix, lang))
	
	return table.concat(parts_formatted, " +‎ ") .. (nocat and "" or m_utilities.format_categories(categories, lang, sort_key, sort_base))
end


function export.show_confix(lang, sc, prefix, base, suffix, pos, sort_key, nocat)
	local categories = {}
	pos = pos or "مفردة"
	
	-- Pluralize the part of speech name
	if pos:find("[sx]$") then
		pos = pos .. "es"
	else
		pos = pos .. "s"
	end
	
	-- Hyphenate the affixes
	prefix.term = export.make_affix(prefix.term, prefix.lang or lang, prefix.sc or sc, "prefix")
	prefix.alt = export.make_affix(prefix.alt, prefix.lang or lang, prefix.sc or sc, "prefix")
	
	if prefix.annotations then
		prefix.annotations.tr = export.make_affix(prefix.annotations.tr, prefix.lang or lang, require("Module:scripts").getByCode("Latn"), "prefix")
	end
	
	suffix.term = export.make_affix(suffix.term, suffix.lang or lang, suffix.sc or sc, "suffix")
	suffix.alt = export.make_affix(suffix.alt, suffix.lang or lang, suffix.sc or sc, "suffix")
	
	if suffix.annotations then
		suffix.annotations.tr = export.make_affix(suffix.annotations.tr, suffix.lang or lang, require("Module:scripts").getByCode("Latn"), "suffix")
	end
	
	local prefix_affix_type = get_affix_type(prefix.lang or lang, prefix.sc or sc, prefix.term)
	if prefix_affix_type ~= "prefix" then
		require("Module:debug").track("confix")
		require("Module:debug").track("confix/prefix")
		require("Module:debug").track("confix/prefix/" .. (prefix_affix_type or "none"))
		require("Module:debug").track("confix/prefix/" .. (prefix_affix_type or "none") .. "/lang/" .. lang:getCode())
	end
	
	if base then
		local base_affix_type = get_affix_type(base.lang or lang, base.sc or sc, base.term)
		if base_affix_type then
			require("Module:debug").track("confix")
			require("Module:debug").track("confix/base")
			require("Module:debug").track("confix/base/" .. base_affix_type)
			require("Module:debug").track("confix/base/" .. base_affix_type .. "/lang/" .. lang:getCode())
		end
	end
	
	local suffix_affix_type = get_affix_type(suffix.lang or lang, suffix.sc or sc, suffix.term)
	if suffix_affix_type ~= "suffix" then
		require("Module:debug").track("confix")
		require("Module:debug").track("confix/suffix")
		require("Module:debug").track("confix/suffix/" .. (suffix_affix_type or "none"))
		require("Module:debug").track("confix/suffix/" .. (suffix_affix_type or "none") .. "/lang/" .. lang:getCode())
	end
	
	-- Make links out of all the parts
	local parts_formatted = {}
	local sort_base = nil
	
	table.insert(parts_formatted, m_links.full_link(prefix.term, prefix.alt, prefix.lang or lang, prefix.sc or sc, "term", prefix.id, prefix.annotations, false))
	
	if base then
		sort_base = lang:makeEntryName(base.term)
		table.insert(parts_formatted, m_links.full_link(base.term, base.alt, base.lang or lang, base.sc or sc, "term", base.id, base.annotations, false))
	end
	
	table.insert(parts_formatted, m_links.full_link(suffix.term, suffix.alt, suffix.lang or lang, suffix.sc or sc, "term", suffix.id, suffix.annotations, false))
	
	-- Insert the categories
	table.insert(categories, lang:getCanonicalName() .. " " .. pos .. " مسبقة بـ " .. lang:makeEntryName(prefix.term, lang))
	table.insert(categories, lang:getCanonicalName() .. " " .. pos .. " ملحقة بـ " .. lang:makeEntryName(suffix.term, lang))
	
	return table.concat(parts_formatted, " +‎ ") .. (nocat and "" or m_utilities.format_categories(categories, lang, sort_key, sort_base))
end


function export.show_infix(lang, sc, base, infix, pos, sort_key, nocat)
	local categories = {}
	pos = pos or "مفردة"
	
	-- Pluralize the part of speech name
	if pos:find("[sx]$") then
		pos = "مفردات"
	else
		pos = "مفردات"
	end
	
	-- Hyphenate the affixes
	infix.term = export.make_affix(infix.term, infix.lang or lang, infix.sc or sc, "infix")
	infix.alt = export.make_affix(infix.alt, infix.lang or lang, infix.sc or sc, "infix")
	
	if infix.annotations then
		infix.annotations.tr = export.make_affix(infix.annotations.tr, infix.lang or lang, require("Module:scripts").getByCode("Latn"), "infix")
	end
	
	local base_affix_type = get_affix_type(base.lang or lang, base.sc or sc, base.term)
	if base_affix_type then
		require("Module:debug").track("infix")
		require("Module:debug").track("infix/base")
		require("Module:debug").track("infix/base/" .. base_affix_type)
		require("Module:debug").track("infix/base/" .. base_affix_type .. "/lang/" .. lang:getCode())
	end
	
	local infix_affix_type = get_affix_type(infix.lang or lang, infix.sc or sc, infix.term)
	if infix_affix_type ~= "infix" then
		require("Module:debug").track("infix")
		require("Module:debug").track("infix/infix")
		require("Module:debug").track("infix/infix/" .. (infix_affix_type or "none"))
		require("Module:debug").track("infix/infix/" .. (infix_affix_type or "none") .. "/lang/" .. lang:getCode())
	end
	
	-- Make links out of all the parts
	local parts_formatted = {}
	
	table.insert(parts_formatted, m_links.full_link(base.term, base.alt, base.lang or lang, base.sc or sc, "term", base.id, base.annotations, false))
	table.insert(parts_formatted, m_links.full_link(infix.term, infix.alt, infix.lang or lang, infix.sc or sc, "term", infix.id, infix.annotations, false))
	
	-- Insert the categories
	table.insert(categories, lang:getCanonicalName() .. " " .. pos .. " infixed with " .. lang:makeEntryName(infix.term))
	
	return table.concat(parts_formatted, " +‎ ") .. (nocat and "" or m_utilities.format_categories(categories, lang, sort_key))
end


function export.show_prefixes(lang, sc, prefixes, base, pos, sort_key, nocat)
	local categories = {}
	pos = pos or "مفردة"
	
	-- Pluralize the part of speech name
	if pos:find("[sx]$") then
		pos = "مفردات"
	else
		pos = "مفردات"
	end
	
	-- Hyphenate the affixes
	for i, prefix in ipairs(prefixes) do
		prefixes[i].term = export.make_affix(prefix.term, prefix.lang or lang, prefix.sc or sc, "prefix")
		prefixes[i].alt = export.make_affix(prefix.alt, prefix.lang or lang, prefix.sc or sc, "prefix")
		
		if prefix.annotations then
			prefixes[i].annotations.tr = export.make_affix(prefix.annotations.tr, prefix.lang or lang, require("Module:scripts").getByCode("Latn"), "prefix")
		end
	end
	
	for i, prefix in ipairs(prefixes) do
		local prefix_affix_type = get_affix_type(prefix.lang or lang, prefix.sc or sc, prefix.term)
		if prefix_affix_type ~= "prefix" then
			require("Module:debug").track("prefix")
			require("Module:debug").track("prefix/prefix")
			require("Module:debug").track("prefix/prefix/" .. (prefix_affix_type or "none"))
			require("Module:debug").track("prefix/prefix/" .. (prefix_affix_type or "none") .. "/lang/" .. lang:getCode())
		end
	end
	
	if base then
		local base_affix_type = get_affix_type(base.lang or lang, base.sc or sc, base.term)
		if base_affix_type then
			require("Module:debug").track("prefix")
			require("Module:debug").track("prefix/base")
			require("Module:debug").track("prefix/base/" .. base_affix_type)
			require("Module:debug").track("prefix/base/" .. base_affix_type .. "/lang/" .. lang:getCode())
		end
	end
	
	-- Make links out of all the parts
	local parts_formatted = {}
	local sort_base = nil
	
	for i, prefix in ipairs(prefixes) do
		local prefix_formatted = m_links.full_link(prefix.term, prefix.alt, prefix.lang or lang, prefix.sc or sc, "term", prefix.id, prefix.annotations, false)
		
		if prefix.lang then
			prefix_formatted = require("Module:etymology language").format(prefix.lang:getCode(), (not nocat and lang or nil), sort_key) .. " " .. prefix_formatted
		end
		
		table.insert(parts_formatted, prefix_formatted)
	end
	
	if base then
		sort_base = lang:makeEntryName(base.term)
		local base_formatted = m_links.full_link(base.term, base.alt, base.lang or lang, base.sc or sc, "term", base.id, base.annotations, false)
		
		if base.lang then
			base_formatted = require("Module:etymology language").format(base.lang:getCode(), (not nocat and lang or nil), sort_key) .. " " .. base_formatted
		end
		
		table.insert(parts_formatted, base_formatted)
	else
		table.insert(parts_formatted, "")
	end
	
	-- Insert the categories
	for i, prefix in ipairs(prefixes) do
		table.insert(categories, pos .. " " .. lang:getCanonicalName() .. " مسبقة بـ " .. " " .. lang:makeEntryName(prefix.term, lang))
	end
	
	return table.concat(parts_formatted, " +‎ ") .. (nocat and "" or m_utilities.format_categories(categories, lang, sort_key, sort_base))
end


function export.show_suffixes(lang, sc, base, suffixes, pos, sort_key, nocat)
	local categories = {}
	pos = pos or "مفردة"
	
	-- Pluralize the part of speech name
	if pos:find("[sx]$") then
		pos = "مفردات"
	else
		pos = "مفردات"
	end
	
	-- Hyphenate the affixes
	for i, suffix in ipairs(suffixes) do
		suffixes[i].term = export.make_affix(suffix.term, suffix.lang or lang, suffix.sc or sc, "suffix")
		suffixes[i].alt = export.make_affix(suffix.alt, suffix.lang or lang, suffix.sc or sc, "suffix")
		
		if suffix.annotations then
			suffixes[i].annotations.tr = export.make_affix(suffix.annotations.tr, suffix.lang or lang, require("Module:scripts").getByCode("Latn"), "suffix")
		end
	end
	
	if base then
		local base_affix_type = get_affix_type(base.lang or lang, base.sc or sc, base.term)
		if base_affix_type then
			require("Module:debug").track("suffix")
			require("Module:debug").track("suffix/base")
			require("Module:debug").track("suffix/base/" .. base_affix_type)
			require("Module:debug").track("suffix/base/" .. base_affix_type .. "/lang/" .. lang:getCode())
		end
	end
	
	for i, suffix in ipairs(suffixes) do
		local suffix_affix_type = get_affix_type(suffix.lang or lang, suffix.sc or sc, suffix.term)
		if suffix_affix_type ~= "suffix" then
			require("Module:debug").track("suffix")
			require("Module:debug").track("suffix/suffix")
			require("Module:debug").track("suffix/suffix/" .. (suffix_affix_type or "none"))
			require("Module:debug").track("suffix/suffix/" .. (suffix_affix_type or "none") .. "/lang/" .. lang:getCode())
		end
	end
	
	-- Make links out of all the parts
	local parts_formatted = {}
	
	if base then
		local base_formatted = m_links.full_link(base.term, base.alt, base.lang or lang, base.sc or sc, "term", base.id, base.annotations, false)
		
		if base.lang then
			base_formatted = require("Module:etymology language").format(base.lang:getCode(), (not nocat and lang or nil), sort_key) .. " " .. base_formatted
		end
		
		table.insert(parts_formatted, base_formatted)
	else
		table.insert(parts_formatted, "")
	end
	
	for i, suffix in ipairs(suffixes) do
		local suffix_formatted = m_links.full_link(suffix.term, suffix.alt, suffix.lang or lang, suffix.sc or sc, "term", suffix.id, suffix.annotations, false)
		
		if suffix.lang then
			suffix_formatted = require("Module:etymology language").format(suffix.lang:getCode(), (not nocat and lang or nil), sort_key) .. " " .. suffix_formatted
		end
		
		table.insert(parts_formatted, suffix_formatted)
	end
	
	-- Insert the categories
	for i, suffix in ipairs(suffixes) do
		table.insert(categories, pos .. " " .. lang:getCanonicalName() .. " ملحقة بـ " .. " " .. lang:makeEntryName(suffix.term, lang))
	end
	
	return table.concat(parts_formatted, " +‎ ") .. (nocat and "" or m_utilities.format_categories(categories, lang, sort_key))
end


-- Adds a hyphen to a word in the appropriate place
function export.make_affix(term, lang, sc, affixtype)
	if not term then
		return nil
	elseif affixtype == "circumfix" then
		return term
	elseif affixtype == "interfix" then
		affixtype = "infix"
	end
	
	local detected_type = get_affix_type(lang, sc, term)

	if affixtype and detected_type == affixtype then
		return term
	end


	--remove an asterisk if the morpheme is reconstructed and add it in the end
	local reconstructed = ""
	if term:find("^*") then 
		reconstructed = "*"
		term = term:gsub("^*", "")
	end
	
	local hyphen = get_hyphen(lang, sc)
	
	if affixtype == "suffix" then
		term = hyphen .. term
	elseif affixtype == "prefix" then
		term = term .. hyphen
	elseif affixtype == "infix" then
		term = hyphen .. term .. hyphen
	else
		error("Invalid affix type")
	end
	
	return reconstructed .. term
end

return export