Modulis:Locījumi

Vikipēdijas lapa
Dokumentācijas ikona Moduļa dokumentācija[izveidot]
-- This module generates templates for dealing with Latvian declensions.

local libraryUtil = require('libraryUtil')

local DATA_PAGE_PREFIX = 'Modulis:Locījumi/'
local NO_RESULT = "Nederīgi dati"

local function main(dataPage, code, declen)
	-- Get the data page first, as we want a big red error message if the data
	-- page is bad.
	libraryUtil.checkType('main', 1, dataPage, 'string')
	local data = mw.loadData(DATA_PAGE_PREFIX .. dataPage)

	-- Check argument types and do preprocessing.
	if type(code) == 'string' then
		if tonumber(code) then
			code = tonumber(code)
		else
			code = mw.ustring.lower(code)
		end
	elseif type(code) ~= 'number' then
		return NO_RESULT
	end
	if type(declen) ~= 'string' then
		return NO_RESULT
	end

	-- Get the data.
	local termData = data[code]
	if termData == nil then
		return NO_RESULT
	elseif type(termData) ~= 'table' then
		error(string.format(
			"the data page '%s' contains malformatted data for code '%s'",
			dataPage,
			tostring(code)
		), 2)
	end
	if declen == 'Ģ' or declen == 'ģ' then
		-- Normalize declension string without using mw.ustring, which can be
		-- slow.
		declen = 'g'
	else
		declen = string.lower(declen)
	end
	return termData[declen] or NO_RESULT
end

return setmetatable(
	{main = main},
	{
		__index = function (p, key)
			return function (frame)
				local args = require('Module:Arguments').getArgs(frame, {
					parentOnly = true
				})
				return main(key, args[1], args[2])
			end
		end
	}
)