Module:User:Stujul/sh-headword

This is a private module sandbox of Stujul, for their own experimentation. Items in this module may be added and removed at Stujul's discretion; do not rely on this module's stability.


local export = {}
local pos_functions = {}
local lang = require("Module:languages").getByCode("sh")
local langname = lang:getCanonicalName()

----------------------------------------------- Helper functions --------------------------------------------
local function otherscript(inflections, args)
	local title = mw.title.getCurrentTitle()
	local sc = lang:findBestScript(title.subpageText)
	
	local other_sc
	
	if sc:getCode() == "Latn" then
		other_sc = "Cyrl"
	elseif sc:getCode() == "Cyrl" then
		other_sc = "Latn"
	end
	
	other_sc = require("Module:scripts").getByCode(other_sc)
	local inflection = {label = other_sc:getCanonicalName() .. " spelling"}

	local heads = args["head"]
	if #heads == 0 then
		heads = {title.subpageText}
	end
	
	if args["tr"][1] == "-" then
		inflection.label = "not attested in " .. other_sc:getCanonicalName() .. " spelling"
	else
		for i, head in ipairs(heads) do
			local tr = args["tr"][i]
			
			if not tr then
				tr = require("Module:sh-translit").tr(require("Module:links").remove_links(head), "sh", sc:getCode())
			end
			
			table.insert(inflection, {term = tr, sc = other_sc})
		end
	end
	
	table.insert(inflections, inflection)
end

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

----------------------------------------------- Main entry point --------------------------------------------
function export.show(frame)		
	local iparams = {
		[1] = {required = true},
	}
	
	local iargs = require("Module:parameters").process(frame.args, iparams)
	local poscat = iargs[1]

	local params = {
		["head"] = {list = true},
		[1] = {list = true, alias_of = "head"},
		["tr"] = {list = true, allow_holes = true},
	}
	
	if pos_functions[poscat] then
		local posparams = pos_functions[poscat].params
		if type(posparams) == "function" then
			posparams = posparams(langcode)
		end
		for key, val in pairs(posparams) do
			params[key] = val
		end
	end

	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local data = {
		lang = lang,
		langname = langname,
		pos_category = poscat,
		categories = {},
		heads = args.head,
		genders = {},
		inflections = {}
	}
	
	otherscript(data.inflections, args)
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end

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

----------------------------------------------- Adjectives --------------------------------------------
local function get_adjective_pos()
	local params = {
		["def"] = {list = true},
		["comp"] = {list = true},
		["adv"] = {list = true},
		["indecl"] = {type = "boolean"},
	}
		
	return {
		params = params,
		func = function(args, data)
			-- Add parameters
			local param_list = {
				{"def", "definite"},
				{"comp", "comparative"},
				{"adv", "derived adverb"},
			}
			
			if args["indecl"] then
				table.insert(data.inflections, {label = glossary_link("indeclinable")})
			end
			for _, val in pairs(param_list) do
				local param_name, label = unpack(val)
				local forms = args[param_name]
				if forms[1] then
					forms.label = (param_name == "adv") and label or glossary_link(label)
					table.insert(data.inflections, forms)
				end
			end
		end
	}
end
pos_functions["adjectives"] = get_adjective_pos()

----------------------------------------------- Letters --------------------------------------------
local function get_letter_pos()
	local params = {
		["upper"] = {},
		["lower"] = {},
		}

	return {
		params = params,
		func = function(args, data)
			if args["upper"] then
				table.insert(data.inflections, {label = "lower case", nil})
				table.insert(data.inflections, {label = "upper case", args["upper"]})
			elseif args["lower"] then
				table.insert(data.inflections, {label = "upper case", nil})
				table.insert(data.inflections, {label = "lower case", args["lower"]})
			end
		end
	}
end
pos_functions["letters"] = get_letter_pos()

----------------------------------------------- Nouns --------------------------------------------
local function get_noun_pos()
	local params = {
		["g"] = {list = true, default = "?"},
		["m"] = {list = true},
		["f"] = {list = true},
        ["dim"] = {list = true},
        ["aug"] = {list = true},
        ["adj"] = {list = true},
        ["indecl"] = {type = "boolean"}
	}

	local gender_cats = {
		["m"] = "masculine",
		["f"] = "feminine",
		["n"] = "neuter",
		["m-p"] = "masculine",
		["f-p"] = "feminine",
		["n-p"] = "neuter",
	}

	return{
		params = params,
		func = function(args, data)
			for i, gender in ipairs(args.g) do
				if gender_cats[gender] then
					table.insert(data.genders, gender)
					table.insert(data.categories, lang:getCanonicalName() .. " " .. gender_cats[gender] .. " nouns")
				else
					data.genders[i] = "?"
				end
			end
			
			-- Add parameters
			local param_list = {
				{"f", "feminine"},
				{"m", "masculine"},
				{"dim", "diminutive"},
				{"aug", "augmentative"},
				{"adj", "relational adjective"},
			}
			if args["indecl"] then
				table.insert(data.inflections, {label = glossary_link("indeclinable")})
			end
			for _, val in pairs(param_list) do
				local param_name, label = unpack(val)
				local forms = args[param_name]
				if forms[1] then
					forms.label = glossary_link(label)
					table.insert(data.inflections, forms)
				end
			end
		end
	}
end
pos_functions["nouns"] = get_noun_pos()

----------------------------------------------- Proper nouns --------------------------------------------
local function get_proper_noun_pos()
	local params = {
		["g"] = {list = true, default = "?"},
		["adj"] = {list = true},
		}
	
	return{
		params = params,
		func = function(args, data)
			for i, gender in ipairs(args.g) do
				if gender_cats[gender] then
					table.insert(data.genders, gender)
					table.insert(data.categories, lang:getCanonicalName() .. " " .. gender_cats[gender] .. " nouns")
				else
					data.genders[i] = "?"
				end
			end
			
			-- Add parameters
			local param_list = {
				{"adj", "relational adjective"},
			}
			for _, val in pairs(param_list) do
				local param_name, label = unpack(val)
				local forms = args[param_name]
				if forms[1] then
					forms.label = glossary_link(label)
					table.insert(data.inflections, forms)
				end
			end
		end
	}
end
pos_functions["proper nouns"] = get_proper_noun_pos()

----------------------------------------------- Verbs --------------------------------------------
local function get_verb_pos()
	local params = {
		["a"] = {},
		["pf"] = {list = true},
		["impf"] = {list = true}
		}
	
	return {
		params = params,
		func = function(args, data)
			if args["a"] == "impf" or args["a"] == "pf" then
				table.insert(data.genders, args["a"])
			elseif args["a"] == "impf-pf" or args["a"] == "pf-impf" or args["a"] == "dual" or args["a"] == "ip" then
				table.insert(data.genders, "impf")
				table.insert(data.genders, "pf")
			else
				table.insert(data.genders, "?")
			end
			
			--add perfective equivalent
			local pf = args["pf"]
			if #pf > 0 then
				pf.label = "perfective"
				table.insert(data.inflections, pf)
			end
			
			--add imperfective equivalent
			local impf = args["impf"]
			if #impf > 0 then
				impf.label = "imperfective"
				table.insert(data.inflections, impf)
			end
		end
	}
end
pos_functions["verbs"] = get_verb_pos()

return export