မဝ်ဂျူ:sa-headword

နူ ဝိက်ရှေန်နရဳ

Documentation for this module may be created at မဝ်ဂျူ:sa-headword/doc

local export = {}
local pos_functions = {}
local m_links = require("Module:links")

local lang = require("Module:languages").getByCode("sa")
local langname = lang:getCanonicalName()
local PAGENAME = mw.title.getCurrentTitle().text
local Array = require "Module:array"

local suffix_categories = {
	["နာမဝိသေသန"] = true,
	["ကြိယာဝိသေသန"] = true,
	["နာမ်"] = true,
	["ကြိယာ"] = true,
}

local function track(page)
	require("Module:debug").track("sa-headword/" .. page)
	return true
end

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

function export.alt(frame)
	local args = frame:getParent().args
	local currentScript = require("Module:scripts").findBestScript(PAGENAME, lang)
	local currentScriptCode = currentScript:getCode()
	local availableScripts = lang:getScripts()
	local devaForm = currentScriptCode == "Deva" and PAGENAME or args["Deva"] or error("No Devanagari-script form detected.")
	local scriptCode
	local translit = require("Module:sa-convert").tr
	local m_labels = require("Module:labels")
	
	local terms
	local first
	local output = {[1] = ""}
	local output = Array('<div class="NavFrame" style="max-width:40em"><div class="NavHead" style="background:#d9ebff">ရုဲကေတ်အခဝ်မွဲမွဲဂမၠိုင်</div><div class="NavContent" style="text-align:left"><ul>')
	for _,script in ipairs(availableScripts) do
		scriptCode = script:getCode()
		terms = { args[scriptCode], args[scriptCode.."2"], args[scriptCode.."3"] }

		if scriptCode ~= "Deva" then
			local auto = translit(devaForm, scriptCode)
			if #terms == 0 then
				terms[1] = auto
			elseif auto == "" then
				-- Helpless - skip script.
			elseif terms[1] ~= auto then
				mw.addWarning("Expected "..auto.." for "..scriptCode.." but given "..terms[1]);
				output:insert("[[Category:Sanskrit terms with inconsistent transliterations]]")
			end
		end
		if terms[1] ~= "" then
			first = true
			for _,term in ipairs(terms) do
				if term ~= nil and term ~= PAGENAME then
					if first then
						first = false
						output:insert("<li>")
					else
						output:insert(" ဝါ ")
					end
					output:insert(m_links.full_link({lang = lang, sc = script, term = term, tr = "-"}))
				end
			end
			if not first then
				output:insert(" ")
				output:insert(m_labels.show_labels({ script:getDisplayForm() }, lang) .. "</li>")
			end
		end
	end
	output:insert("</ul></div></div>")

	return output:concat()

end

-- The main entry point.
function export.show(frame)
	local tracking_categories = {}
	
	local iparams = {
		[1] = {required = true},
		["def"] = {}, -- default value
	}

	local iargs = require("Module:parameters").process(frame.args, iparams)
	local poscat = iargs[1]

	local params = {
		["head"] = {list = true},
		["tr"] = {list = true, allow_holes = true},
		["sc"] = {},
		["id"] = {},
		["sort"] = {},
		["suff"] = {type = "boolean"},
	}

	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end
	
    local parargs = frame:getParent().args
	local args = require("Module:parameters").process(parargs, params)
	local heads = args["head"]
	if #heads == 0 and mw.title.getCurrentTitle().nsText == "ထာမ်ပလိက်" then
		heads = {iargs["def"]}
	end
	local data = {
		lang = lang,
		sc = require("Module:scripts").getByCode(args["sc"], "sc"),
		pos_category = poscat,
		categories = {},
		heads = heads,
		translits = args["tr"],
		genders = {},
		inflections = {},
		id = args["id"],
		sort_key = args["sort"],
		categories = {},
		sccat = true,
	}
	
	if args["suff"] then
		data.pos_category = "suffixes"
		
		if suffix_categories[poscat] then
			local singular_poscat = poscat:gsub("s$", "")
			table.insert(data.categories, langname .. " " .. singular_poscat .. "-forming suffixes")
		else
			error("No category exists for suffixes forming " .. poscat .. ".")
		end
	end
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data, tracking_categories)
	end
	
	return require("Module:headword").full_headword(data)
		.. require("Module:utilities").format_categories(tracking_categories, lang)
end

pos_functions["ကြိယာ"] = {
	params = {
		[1] = {},
		[2] = {},
		[3] = {},
		[4] = {},
		[5] = {},
		[6] = {},
		[7] = {},
		["norootcat"] = {type = "boolean"},
	},
	func = function(args, data, tracking_categories)
		local function tooltip(mousetext, anchor, underline)
			return mw.getCurrentFrame():expandTemplate {
				title = "tooltip", args = {mousetext, anchor, und = underline and "1" or nil}
			}
		end
		local function type_to_text(typ)
			return (
				typ == "A" and {"ātmanepada"} or
				typ == "P" and {"parasmaipada"} or
				typ == "U" and {"ubhayapada"} or
				typ == "UP" and {"ubhayapada", "parasmaipada"} or
				typ == "UA" and {"ubhayapada", "ātmanepada"} or
				error("Unrecognized Sanskrit verb type: '" .. typ .. "'; should be one of A, P, U, UP or UA")
			)
		end
		local function type_to_tooltip(typ)
			local desc = table.concat(type_to_text(typ), " — ")
			return tooltip(desc, typ, true)
		end
		local function mode_to_cat(mode)
			if mode == "ပစ္စုပ္ပန်" or mode == "ပရောဟိုတ်" or mode == "ဂဟဲစွံလဝ်" or mode == "မြဟ်ဗွဲမလောန်" or
				mode == "ပါ်တၞဟ်ခြာ" then
				return mode .. "ကြိယာ"
			elseif mode == "ထပ်တုဲထပ်ပၠန်" then
				return "ကြိယာမြဟ်ဗွဲမလောန်ဂမၠိုင်"
			elseif mode == "ဒုင်ယၟု" then
				return "နာမ်ပါ်ခြာကြိယာ"
			elseif mode == "က္ဍိုပ်ပေင်" or mode == "ဟွံက္ဍိုပ်ပေင်" or mode == "aorist" or mode == "ဟွံချဳဒရာင်" then
				return nil -- FIXME: What about these verbs?
			else
				error("Unrecognized Sanskrit verb mode: '" .. mode .. "'; should be 'present', 'causative', 'desiderative', etc.")
			end
		end

		if args[1] then
			table.insert(data.inflections, {label = "တံရိုဟ်", args[1]})
			if not args.norootcat then
				
			end
		end
		local function handle_class_type_mode(class, typ, mode, notfirst)
			if class then
				if not class:find("^[1-9]$") and class ~= "၁၀" then
					error("Unrecognized Sanskrit verb class: '" .. class .. "'; should be a number from 1 to 10")
				end
				table.insert(data.inflections, {label = (notfirst and "or " or "") .. "ကဆံၚ် " .. class})
				table.insert(data.categories, "ကြိယာ" ..  langname .. "ကဆံၚ် " .. class .. " တန်")
			end
			if typ then
				table.insert(data.inflections, {label = "type " .. type_to_tooltip(typ)})
				for _, desc in ipairs(type_to_text(typ)) do
					table.insert(data.categories, langname .. " " .. desc .. "ကြိယာ")
				end
			end
			if mode then
				table.insert(data.inflections, {label = mode})
				local cat = mode_to_cat(mode)
				if cat then
					table.insert(data.categories, langname .. " " .. cat)
				end
			end
		end
		handle_class_type_mode(args[2], args[3], args[4])
		handle_class_type_mode(args[5], args[6], args[7], "notfirst")
	end,
}


pos_functions["နာမဝိသေသန"] = {
	params = {
		[1] = {alias_of = "comp"},
		["comp"] = {list = true}, --comparative(s)
		[2] = {alias_of = "sup"},
		["sup"] = {list = true}, --superlative(s)
	},
	func = function(args, data, tracking_categories)
		if args.comp[1] == "-" then
			table.insert(data.inflections, {label = "ပတုပ်ရံင်ဟွံမာန်"})
			
		else
			if #args.comp > 0 then
				args.comp.label = glossary_link("ပတုပ်ရံင်")
				args.comp.accel = {form = "comparative"}
				table.insert(data.inflections, args.comp)
			end
		
			if #args.sup > 0 then
				args.sup.label = glossary_link("သဒ္ဒာ")
				args.sup.accel = {form = "superlative"}
				table.insert(data.inflections, args.sup)
			end
		end
	end
}

local noun_params = {
	["indecl"] = {type = "boolean"},
	["g"] = {list = true}, --gender(s)
	["f"] = {list = true}, --feminine form(s)
	["m"] = {list = true}, --masculine form(s)
	["n"] = {list = true}, --neuter form(s)
}

local allowed_genders = {
	["m"] = true,
	["f"] = true,
	["n"] = true,
	["m-p"] = true,
	["f-p"] = true,
	["n-p"] = true,
	["mf"] = true,
	["mfbysense"] = true,
	["mf-p"] = true,
	["mfbysense-p"] = true,
	["fm"] = true,
	["mn"] = true,
	["nm"] = true,
}

local function do_nouns(plpos, args, data, tracking_categories)
	local genders = {}
	for _, g in ipairs(args.g) do
		if not allowed_genders[g] then
			error("Unrecognized gender: " .. g)
		end
		if g == "fm" then
			table.insert(genders, "f")
			table.insert(genders, "m")
		elseif g == "mn" then
			table.insert(genders, "m")
			table.insert(genders, "n")
		elseif g == "nm" then
			table.insert(genders, "n")
			table.insert(genders, "m")
		else
			-- mf, mfbysense handled internally by [[Module:gender and number]]
			table.insert(genders, g)
		end
	end

	if #genders > 0 then
		data.genders = genders
	else
		data.genders = {"?"}
	end

	if args.indecl then
		table.insert(data.inflections, {label = "ဟွံပြံင်လှာဲ"})
	end

	if #args.m > 0 then
		args.m.label = "ပုလ္လိင်"
		table.insert(data.inflections, args.m)
	end

	if #args.f > 0 then
		args.f.label = "ဣတ္တိလိင်"
		table.insert(data.inflections, args.f)
	end

	if #args.n > 0 then
		args.n.label = "နပုလ္လိင်"
		table.insert(data.inflections, args.n)
	end
end

pos_functions["နာမ်"] = {
	params = noun_params,
	func = function(args, data, tracking_categories)
		return do_nouns("နာမ်", args, data, tracking_categories)
	end,
}

pos_functions["နာမ်မကိတ်ညဳ"] = {
	params = noun_params,
	func = function(args, data, tracking_categories)
		return do_nouns("proper nouns", args, data, tracking_categories)
	end,
}

local function pos_with_gender()
	return {
		params = {
			["g"] = {list = true},
		},
		func = function(args, data)
			data.genders = args["g"]
		end,
	}
end

pos_functions.numerals = pos_with_gender()
pos_functions.pronouns = pos_with_gender()
pos_functions.suffixes = pos_with_gender()
pos_functions["adjective forms"] = pos_with_gender()
pos_functions["noun forms"] = pos_with_gender()
pos_functions["proper noun forms"] = pos_with_gender()
pos_functions["pronoun forms"] = pos_with_gender()
pos_functions["determiner forms"] = pos_with_gender()
pos_functions["verb forms"] = pos_with_gender()

return export