Module:head/languages: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by one other user not shown)
Line 44: Line 44:
inflection["label"] = v.label
inflection["label"] = v.label
inflection["glossary"] = v.glossary
inflection["glossary"] = v.glossary
inflection["nolink"] = v.nolink or false
table.insert(inflections, inflection)
table.insert(inflections, inflection)
end
end
Line 51: Line 52:
end
end


function export.get_term()
function export.get_term(language)
return mw.title.getCurrentTitle().text
local title = mw.title.getCurrentTitle().text
if language.proto then
title = mw.ustring.gsub(title, "^[^/]+/", "")
end
return title
end
end


function export.show(frame)
function export.show(frame)
local language = m_languages.get_by_code(frame.args[1])
local language = m_languages.get_by_code(frame.args[1])
local term = export.get_term()
local term = export.get_term(language)
local pos = frame.args[2]
local pos = frame.args[2]
local inflection_params = mw.loadData("Module:head/languages/" .. language.code)
local inflection_params = mw.loadData("Module:head/languages/" .. language.code)

Latest revision as of 18:42, 18 February 2024

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
				inflection["nolink"] = v.nolink or false
				table.insert(inflections, inflection)
			end
		end
	end
	return p, inflections
end

function export.get_term(language)
	local title = mw.title.getCurrentTitle().text
	if language.proto then
		title = mw.ustring.gsub(title, "^[^/]+/", "")
	end
	return title
end

function export.show(frame)
	local language = m_languages.get_by_code(frame.args[1])
	local term = export.get_term(language)
	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