Module:cbk-headword

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Powers Chavacano headword-line templates.


local export = {}
local pos_functions = {}

local force_cat = false -- for testing

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

-- 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 require("Module:string utilities").pluralize(args["pos"]) or error("Part of speech has not been specified. Please pass parameter 1= or pos= to the module invocation.")

	local params = {
		["head"] = {list = true, allow_holes = true},
		[1] = {alias_of = "head"},
		["id"] = {},
		["suff"] = {type = "boolean"},
		["nolinkhead"] = {type = "boolean"},
		["json"] = {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 args = require("Module:parameters").process(frame:getParent().args, params)
	local pagename = args.pagename or mw.title.getCurrentTitle().subpageText

	local user_specified_heads = args.head

	local data = {
		lang = lang,
		pos_category = poscat,
		categories = {},
		heads = args.head,
		user_specified_heads = user_specified_heads,
		inflections = {},
		pagename = pagename,
		id = args.id,
		force_cat_output = force_cat,
	}

	local is_suffix = false
	if args.suff then
		is_suffix = true
		data.pos_category = "suffixes"
		local singular_poscat = require("Module:string utilities").singularize(poscat)
		table.insert(data.categories, langname .. " " .. singular_poscat .. "-forming suffixes")
		table.insert(data.inflections, {label = singular_poscat .. "-forming suffix"})
	end

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

	local content = mw.title.new(pagename):getContent()
	local code = content and mw.ustring.match(content, "{{cbk%-IPA[^}]*}}")

	--Categorize words without [[Template:cbk-IPA]]
	if not code then
		table.insert(data.categories, langname .. " terms without cbk-IPA template")
	end

	if args.json then
		return require("Module:JSON").toJSON(data)
	end

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


local function handle_infl(args, data, argpref, label, accelform)
	local forms = args[argpref]
	if #forms > 0 then
		forms.label = label
		if accelform then
			forms.accel = {form = accelform}
		end
		table.insert(data.inflections, forms)
	end
end

pos_functions["adjectives"] = {
	params = {
		["pl"] = {list = true},
		[2] = {alias_of = "pl"},
		["m"] = {list = true},
		["f"] = {list = true},
	},
		func = function(args, data)
		handle_infl(args, data, "pl", "plural", "p")
		handle_infl(args, data, "f", "feminine")
		handle_infl(args, data, "m", "masculine")
	end
}


pos_functions["nouns"] = {
	params = {
		["pl"] = {list = true},
		[2] = {alias_of = "pl"},
		["m"] = {list = true},
		["f"] = {list = true},
	},
	func = function(args, data)
		handle_infl(args, data, "pl", "plural", "p")
		handle_infl(args, data, "f", "feminine")
		handle_infl(args, data, "m", "masculine")
	end
}


return export