Module:head/languages: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
 
(10 intermediate revisions by 2 users not shown)
Line 5: Line 5:


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


function export.get_args(args, params, inflection_params)
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
if inflection_params then
for _, v in ipairs(inflection_params) do
for _, v in ipairs(inflection_params) do
Line 21: Line 38:
for _, v in ipairs(inflection_params) do
for _, v in ipairs(inflection_params) do
local inflection = p[v.param]
local inflection = p[v.param]
inflection["label"] = v.label
if #inflection < 1 then
inflection["glossary"] = v.glossary
inflection = {export.attempt_auto_inflection(v, p, term)}
table.insert(inflections, inflection)
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
end
end
Line 29: 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(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)
local args, inflections = export.get_args(frame:getParent().args, default_params, inflection_params[pos])
local args, inflections = export.get_args(frame:getParent().args, default_params, inflection_params[pos], term)
return m_head.full_head{
return m_head.full_head{
term = mw.title.getCurrentTitle().text,
term = term,
head = args["head"],
head = args["head"],
language = language,
language = language,
pos = pos,
pos = pos,
genders = args["gender"],
genders = args[1],
labels = args["label"],
labels = args["label"],
inflections = inflections,
inflections = inflections,

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