Module:pi-headword

From Wiktionary, the free dictionary
Jump to navigation Jump to search
This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}
local pos_functions = {}
local links = require('Module:links')
local labels = require('Module:labels')

local m_lang = require('Module:languages')
local lang = m_lang.getByCode('pi')
local langmul
local PAGENAME = mw.title.getCurrentTitle().text

local translit = require('Module:pi-Latn-translit').tr
local Array = require "Module:array"
local ti = table.insert

function export.alt(frame)

	local args = frame:getParent().args

	local currentScript = lang:findBestScript(PAGENAME)
	local currentScriptCode = currentScript:getCode()
	local availableScripts = lang:getScripts()
	local latinForm = args['Latn'] or currentScriptCode == 'Latn' and PAGENAME or error('No Latin-script form detected.')
	local scriptCode

	local terms, langs
	local first
	local ltrlang = {Latn = 'mul', Brah = 'mul', Deva = 'mul', Beng = 'mul', Sinh = 'mul',
					 Mymr = 'mul', Thai = 'pi',  Lana = 'mul', Laoo = 'mul', Khmr = 'pi',
					 Cakm = 'mul'}
	local output = Array('<div class="NavFrame" style="max-width:40em"><div class="NavHead" style="background:#d9ebff">Alternative forms</div><div class="NavContent" style="text-align:left"><ul>')
	local deflang
	local function lfrom(ord)
		local namels = {}
		ti(namels, scriptCode)
		ti(namels, 'lang')
		ti(namels, ord)
		return args[table.concat(namels)] or deflang
	end
	for _, script in ipairs(availableScripts) do
		scriptCode = script:getCode()
		if args[scriptCode] ~= '-' then
			deflang = (args.letter == '1' or args.letter == 1) and
						(ltrlang[scriptCode] or 'mul')
					or 'pi'
			terms = { args[scriptCode], args[scriptCode..'2'], args[scriptCode..'3'],
				 args[scriptCode..'4'], args[scriptCode..'5'], args[scriptCode..'6'],
				 args[scriptCode..'7'], args[scriptCode..'8']}
			langs =    {lfrom(''), lfrom('2'), lfrom('3'),
						lfrom('4'), lfrom('5'), lfrom('6'),
						lfrom('7'), lfrom('8')}
			if args.hl1 and scriptCode == 'Latn' then -- Discard argument 'Latn'
				terms = {terms[2], terms[3], terms[4], terms[5], terms[6], terms[7], terms[8]}
				langs = {langs[2], langs[3], langs[4], langs[5], langs[6], langs[7], langs[8]}
			end
			local t1, t2, t3, t4
			t1 = translit(latinForm, scriptCode, {}) -- not affect Latn itself
			if scriptCode == 'Thai' or scriptCode == 'Laoo' then
				t2 = translit(latinForm, scriptCode, {impl='no'})
			end
-- For Lao script without implicit vowels, 'y' can also be 'ຢ', but seemingly
-- not if a Buddhist Institute consonant is used.
			if scriptCode == 'Laoo' then
				if not mw.ustring.find(t2, '[ຆຌຏຐຑຒຓຘຠຨຩ]') then
					t3 = mw.ustring.gsub(t2, 'ຍ', 'ຢ')
				end
			end

			if scriptCode == 'Mymr' then
				t2 = translit(latinForm, scriptCode, {variation='1'}) --Mon
				t3 = translit(latinForm, scriptCode, {variation='2'}) --Old Shan
				t4 = translit(latinForm, scriptCode, {variation='3'}) --New Shan
			end

			if #terms == 0 then
				terms[1] = t1
				if t2 and (t1 ~= t2) then ti(terms, t2) end
				if t3 and (t1 ~= t3) and (t2 ~= t3) then ti(terms, t3) end
				if t4 and (t1 ~= t4) and (t2 ~= t4) and (t3 ~= t4) then ti(terms, t4) end
				for k = 1, 4 do langs[k] = deflang end
			elseif terms[1] ~= t1 and terms[2] ~= t1  and terms[3] ~= t1  and terms[4] ~= t1 then
				output:insert('[[Category:Pali terms with inconsistent transliterations]]')
			end

			first = true
			for k, term in ipairs(terms) do
				if term ~= nil and term ~= PAGENAME then
					if first then
						first = false
						output:insert('<li>')
					else
						output:insert(' or ')
					end
					local langobj
					if langs[k] == 'pi' then
						langobj = lang
					elseif langs[k] == 'mul' then
						langmul = langmul or m_lang.getByCode('mul')
						langobj = langmul
					else
						langobj = m_lang.getByCode(langs[k])
					end
					output:insert(links.full_link({lang = langobj, sc = script, term = term, tr = '-'}))
				end
			end
			if not first then
				output:insert(' ' .. labels.show_labels {
					lang = lang,
					labels = { script:getDisplayForm() },
				} .. '</li>')
			end
	
		end
	end
	output:insert('</ul></div></div>')

	return output:concat()

end

function export.show(frame)
	local params = {
		["head"] = {list = true},
		["tr"] = {list = true, allow_holes = true, default = "-"},
	}
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	local is_noun
	if poscat == "nouns" or poscat == "proper nouns" or poscat == "pronouns" or poscat == "noun forms" then
		is_noun = true
		params[1] = {list = "g", default = "?"}
		params["m"] = {list = true}
		params["f"] = {list = true}
		params["n"] = {list = true}
	end
	if poscat == "verbs" then
		params[1] = {default = "?"}
	end
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	for i, v in ipairs(args.tr) do
		if args.tr[i] == '+' then args.tr[i] = nil end
	end

	local genders = {}
	if is_noun then
		genders = args[1]
	end

	local data = {lang = lang, pos_category = poscat, categories = {},
		sccat = true, heads = args.heads, genders = genders, inflections = {}, translits = args.tr
	}
	
	if is_noun then
		if #args.m > 0 then
			args.m.label = "masculine"
			table.insert(data.inflections, args.m)
		end
		if #args.f > 0 then
			args.f.label = "feminine"
			table.insert(data.inflections, args.f)
		end
		if #args.n > 0 then
			args.n.label = "neuter"
			table.insert(data.inflections, args.n)
		end
		if #args.m > 0 or #args.f > 0 or #args.n > 0 then
			table.insert(data.categories, "Pali nouns with other-gender equivalents")
		end
	end
    if poscat == "verbs" then
        table.insert(data.inflections, {label = "root", args[1]})
    end
	
	return require("Module:headword").full_headword(data)
end

return export