Module:umu-headword


local export = {}
local pos_functions = {}
local rfind = mw.ustring.find
local rmatch = mw.ustring.match
local rsubn = mw.ustring.gsub
local rsplit = mw.text.split

local lang = require("Module:languages").getByCode("umu")
local langname = lang:getCanonicalName()

local suffix_categories = {
	["nouns"] = true,
	["verbs"] = true,
	["particles"] = true,
}

-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
	local retval = rsubn(term, foo, bar)
	return retval
end

local function track(page)
	require("Module:debug").track("umu-headword/" .. page)
	return true
end

local function glossary_link(entry, text)
	text = text or entry
	return "[[Appendix:Glossary#" .. entry .. "|" .. text .. "]]"
end

-- mw.title.new() returns nil if there are weird chars in the pagename.
local function exists(pagename)
	local title = mw.title.new(pagename)
	return title and title.exists
end

local function check_exists(forms, cats, pos)
	for _, form in ipairs(forms) do
		if type(form) == "table" then
			form = form.term
		end
		if not exists(form) then
			table.insert(cats, langname .. " " .. pos .. " with red links in their headword lines")
			return false
		end
	end
	return true
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")

	local params = {
		["head"] = {list = true},
		["splithyph"] = {type = "boolean"},
		["nolinkhead"] = {type = "boolean"},
		["pagename"] = {}, -- for testing
	}

	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end

	local parargs = frame:getParent().args
	local args = require("Module:parameters").process(parargs, params)

	local pagename = args.pagename or mw.title.getCurrentTitle().text

	local heads = args["head"]
	if pos_functions[poscat] and pos_functions[poscat].param1_is_head and args[1] then
		table.insert(heads, 1, args[1])
	end
	if args.nolinkhead then
		if #heads == 0 then
			heads = {pagename}
		end
	else
		local auto_linked_head = require("Module:romance utilities").add_lemma_links(pagename, args.splithyph,
			no_split_apostrophe_words)
		if #heads == 0 then
			heads = {auto_linked_head}
		else
			for _, head in ipairs(heads) do
				if head == auto_linked_head then
					track("redundant-head")
				end
			end
		end
	end

	local data = {
		lang = lang,
		pos_category = poscat,
		categories = {},
		heads = heads,
		no_redundant_head_cat = #args.head == 0,
		genders = {},
		inflections = {},
		categories = {},
		pagename = pagename
	}

	if pagename:find("^%-") and suffix_categories[poscat] then
		data.pos_category = "suffixes"
		local singular_poscat = poscat:gsub("s$", "")
		table.insert(data.categories, langname .. " " .. singular_poscat .. "-forming suffixes")
	end

	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end

	return require("Module:headword").full_headword(data)
end

local allowed_genders = {
	["a"] = true,
	["i"] = true,
	["ad"] = true,
	["id"] = true
}

local function get_noun_pos(pos)
	return {
		params = {
			[1] = {},
			["g"] = {list = true},
			[2] = {list = true},
			["a"] = {list = true},
			["i"] = {list = true},
			["ad"] = {list = true},
			["id"] = {list = true}
			}
		}
	end
		
pos_functions["particles"] = get_misc_pos()

pos_functions["verbs"] = {
	param1_is_head = true,
	params = {
		[1] = {},
		["type"] = {list = true},
	},
	func = function(args, data)
		local pos = "verbs"
		for _, ty in ipairs(args.type) do
			local category, label
			if ty == "auxiliary" then
				category = "auxiliary"
			elseif ty == "defective" then
				category = "defective"
				label = glossary_link("defective")
			elseif ty == "impersonal" then
				category = "impersonal"
				label = glossary_link("impersonal")
			elseif ty == "modal" then
				category = "modal"
			elseif ty == "reflexive" then
				category = "reflexive"
			elseif ty == "transitive" then
				label = glossary_link("transitive")
				category = "transitive"
			elseif ty == "intransitive" then
				label = glossary_link("intransitive")
				category = "intransitive"
			elseif ty == "ambitransitive" or ty == "ambi" then
				category = {"transitive", "intransitive"}
				label = glossary_link("transitive") .. " and " .. glossary_link("intransitive")
			end
			if category then
				if type(category) == "table" then
					for _, cat in ipairs(category) do
						table.insert(data.categories, langname .. " " .. cat .. " " .. pos)
					end
				else
					table.insert(data.categories, langname .. " " .. category .. " " .. pos)
				end
			end
			if label then
				table.insert(data.inflections, {label = label})
			end
		end
	end
}

return export