Module:head/languages

From Laenkea
< Module:head
Revision as of 14:44, 13 August 2023 by Maria (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:head/languages/documentation

local export = {}

local m_head = require("Module:head")
local m_languages = require("Module:languages")

local default_params = {
	[1] = {list = true},
	["head"] = {},
	["stem"] = {list = true, allow_holes = true},
	["label"] = {list = true},
	["noauto"] = {type = "boolean"},
}

function export.attempt_auto_inflection(inflection_param, args, term)
	if args["noauto"] then return nil end
	if not inflection_param.auto then return nil end
	local i = 1
	local auto = mw.ustring.gsub(inflection_param.auto, "$0", term)
	while i < (args["stem"]["maxindex"] + 1) do
		if args["stem"][i] then
			auto = mw.ustring.gsub(auto, "$" .. i .. "%??", args["stem"][i])
		end
		i = i + 1
	end
	auto = mw.ustring.gsub(auto, "$%d%?", term)
	return auto
end

function export.get_args(args, params, inflection_params, term)
	if inflection_params then
		for _, v in ipairs(inflection_params) do
			params[v.param] = {list = true}
		end
	end
	local p = require("Module:parameters").process(args, params)
	local inflections = {}
	if inflection_params then
		for _, v in ipairs(inflection_params) do
			local inflection = p[v.param]
			if #inflection < 1 then
				inflection = {export.attempt_auto_inflection(v, p, term)}
			end
			if inflection[1] ~= "-" then
				inflection["label"] = v.label
				inflection["glossary"] = v.glossary
				table.insert(inflections, inflection)
			end
		end
	end
	return p, inflections
end

function export.get_term()
	return mw.title.getCurrentTitle().text
end

function export.show(frame)
	local language = m_languages.get_by_code(frame.args[1])
	local term = export.get_term()
	local pos = frame.args[2]
	local inflection_params = mw.loadData("Module:head/languages/" .. language.code)
	local args, inflections = export.get_args(frame:getParent().args, default_params, inflection_params[pos], term)
	return m_head.full_head{
		term = term,
		head = args["head"],
		language = language,
		pos = pos,
		genders = args[1],
		labels = args["label"],
		inflections = inflections,
	}
end

return export