Մոդուլ:Ամսաթիվ ծնունդներ-մահեր

Documentation for this module may be created at Մոդուլ:Ամսաթիվ ծնունդներ-մահեր/doc

local p = {}

local nameIndexMap = {
	['Հունվարի'] = 1,
	['Փետրվարի'] = 2,
	['Մարտի'] = 3,
	['Ապրիլի'] = 4,
	['Մայիսի'] = 5,
	['Հունիսի'] = 6,
	['Հուլիսի'] = 7,
	['Օգոստոսի'] = 8,
	['Սեպտեմբերի'] = 9,
	['Հոկտեմբերի'] = 10,
	['Նոյեմբերի'] = 11,
	['Դեկտեմբերի'] = 12,
	[1] = 'Հունվարի',
	[2] = 'Փետրվարի',
	[3] = 'Մարտի',
	[4] = 'Ապրիլի',
	[5] = 'Մայիսի',
	[6] = 'Հունիսի',
	[7] = 'Հուլիսի',
	[8] = 'Օգոստոսի',
	[9] = 'Սեպտեմբերի',
	[10] = 'Հոկտեմբերի',
	[11] = 'Նոյեմբերի',
	[12] = 'Դեկտեմբերի',
}

local maxDays = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}

local function getValidMonthCat(monthIndex, day, i, suffix)
	local newDay = day + i
	if newDay > 0 and newDay <= maxDays[monthIndex] then
		return nameIndexMap[monthIndex] .. ' ' .. newDay .. ' ' .. suffix
	elseif newDay > maxDays[monthIndex] then
		local newMonth = monthIndex + 1
		if newMonth == 13 then
			newMonth = 1
		end
		return nameIndexMap[newMonth] .. ' ' .. newDay - maxDays[monthIndex] .. ' ' .. suffix
	elseif newDay <= 0 then
		local newMonth = monthIndex - 1
		if newMonth == 0 then
			newMonth = 12
		end
		return nameIndexMap[newMonth] .. ' ' .. maxDays[newMonth] + newDay .. ' ' .. suffix
	end
	return newDay .. ''
end
local function main(title, suffix)
	local month, day = mw.ustring.match(title, '(.-) (%d%d?) ' .. suffix)
	local monthIndex = nameIndexMap[month]
	local categories = {}
	for i = 1, 3 do
		local newTitle = getValidMonthCat(monthIndex, day, -i, suffix)
		table.insert(categories, 1, '[[:Կատեգորիա:' .. newTitle .. '|' ..  newTitle:gsub(' ' .. suffix, '') .. ']]')
	end
	table.insert(categories, '[[:Կատեգորիա:' .. title .. '|' ..  title:gsub(' ' .. suffix, '') .. ']]')
	for i = 1, 3 do
		local newTitle = getValidMonthCat(monthIndex, day, i, suffix)
		table.insert(categories, '[[:Կատեգորիա:' .. newTitle .. '|' ..  newTitle:gsub(' ' .. suffix, '') .. ']]')
	end
	local result = '<center>' .. table.concat(categories, ' • ') .. '</center>'
	result = result .. '[[Կատեգորիա:' .. month .. ' ' .. suffix .. ']]'
	result = result .. '[[Կատեգորիա:' .. month .. ' ' .. day .. ']]'
	return result
end

function p.births(frame)
	return main(mw.title.getCurrentTitle().text, 'ծնունդներ')
end

function p.deaths(frame)
	return main(mw.title.getCurrentTitle().text, 'մահեր')
end

return p