Module:pa-decl-auto

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

local export = {}
local m_translit = require("Module:pa-translit")

local gsub = mw.ustring.gsub
local sub = mw.ustring.sub
local match = mw.ustring.match
local len = mw.ustring.len

local function wordify(text)
	local words, translits = {}, {}
	for word in mw.text.split(text, ",") do
		table.insert(words, pa_format(word))
		table.insert(translits, m_translit.tr(word, "pa", "Guru"))
	end
	text = table.contact(words, ", ") .. "<br><span style=\"color:#888\">" .. table.concat(translits, ", ") .. "</span>"
	return text
end

function pa_format(text)
	text = ('<span lang="pa" class="Guru">[[%s]]</span>'):format(text)
	return text
end

-- masculine ends in ਆ-ਾ
local function decline(word, gender)
	if gender == "m" then
		if match(word, "ਆਾ$") then
			local stem = gsub(word, "ਆਾ$", "")
			s_obl, s_voc, p_nom, p_obl, p_voc = stem .. "ੇ", stem .. "ਿਆ", stem .. "ੇ", stem .. "ਿਆੰ", stem .. "ਿਓ"
		else
			s_obl, s_voc, p_nom, p_obl, p_voc = word .. "ੇ", word .. "ਿਆ", word .. "ੇ", word .. "ਿਆੰ", word .. "ਿਓ"
		end 
	end
	if gender == "f" then
		s_obl, s_voc, p_nom, p_obl, p_voc = word, word .. "ੇ", word .. "ਆੰ", word .. "ਆੰ", word .. "ਓ"
	end 
	return s_obl, s_voc, p_nom, p_obl, p_voc
end

local function make_row(case, s, p)
	local data = {}

	table.insert(data, [=[
		|- class="vsHide"
		| style="background:#eff7ff" | '']=] .. case .. "''")
	table.insert(data, '|' .. wordify(s))
	table.insert(data, '|' .. wordify(p))

	return table.concat(data, "\n")
end

function export.show(frame)
	local args = frame:getParent().args
	local word = args[1] or mw.title.getCurrentTitle().text
	local gender = args[2] or args["g"]

	if gender ~= "m" and gender ~= "f" then
		error("Not a valid gender; must be 'm' or 'f'.")
	end

	local s_obl, s_voc, p_nom, p_obl, p_voc = decline(word, gender)
	local s_nom = word
	local data = {'{| class="inflection-table vsSwitcher" data-toggle-category="inflection" style="background:#F9F9F9; text-align:center; border: 1px solid #CCC; width: 35em"'}
	table.insert(data, '|- style="background: #d9ebff;"')
	table.insert(data, '! class="vsToggleElement" style="text-align: left;" colspan="3" | Declension of ' .. pa_format(word))
	table.insert(data, [=[
		|- class="vsHide"
		! style="background:#eff7ff" |
		! style="background:#eff7ff" | Singular
		! style="background:#eff7ff" | Plural]=])
	table.insert(data, make_row('Direct', s_nom, p_nom))
	table.insert(data, make_row('Oblique', s_obl, p_obl))
	table.insert(data, make_row('Vocative', s_voc, p_voc))
	table.insert(data, "|}")
	
	return table.concat(data, "\n")
end

return export