پودمان:Navseasoncats with year navigation

از اسلامیکال
نسخهٔ تاریخ ‏۱۸ ژانویهٔ ۲۰۲۳، ساعت ۰۹:۱۳ توسط Aboammar (بحث | مشارکت‌ها) (ساخت الگو)
(تفاوت) → نسخهٔ قدیمی‌تر | نمایش نسخهٔ فعلی (تفاوت) | نسخهٔ جدیدتر ← (تفاوت)
پرش به ناوبری پرش به جستجو

توضیحات این پودمان می‌تواند در پودمان:Navseasoncats with year navigation/توضیحات قرار گیرد.

local p = {}
local nsc = require('Module:Navseasoncats')
local num_con = require('Module:Numeral converter').convert

local errorList = {
	["FIND_VAR"] = "Function find_var can't recognize the decade for category %s.",
	["NO_YEAR"] = "{{Navseasoncats with centuries below decade}} can't recognize the year for category %s.",
	["NO_DECADE"] = "{{Navseasoncats with centuries below decade}} can't recognize the decade for category %s."
}

local function create_category(firstPart, lastPart, dateValue, dateWord)
	if mw.ustring.find(dateWord, "دهه") then
		local category = mw.text.trim(firstPart .. ' دهه ' .. num_con("fa", dateValue) .. ' ' .. lastPart)
	else
		local category = mw.text.trim(firstPart .. ' ' .. num_con("fa", dateValue) .. ' ' .. lastPart)
	end

	if (mw.title.new(category, 'رده').exists) then
		return category
	else
		return nil
	end
end

local function getCentury(year)
	year = num_con("en", year)
	decade = tonumber(year)
	local century = math.floor(((year - 1) / 100) + 1) --from {{CENTURY}}
	if (string.match(year, '00$')) then
		century = century + 1
	end --'2000' is technically in the 20th, but the rest of the 2000s is in the 21st
	
	return century
end

local function getDecade(year)
	year = num_con("en", year)
	if tonumber(year) then
		year = tonumber(year)
		local decade = year / 10
		decade = math.floor(decade)*10
		return decade
	end
end

local function getNestTierDateCategory(dateArgs, dateValue, firstPart, lastPart, decade)
	local nextTierDateCategory = ""
	if (dateArgs.dateType == "year") then
		local decade = getDecade(dateValue)
		nextTierDateCategory = firstPart .. ' دهه ' .. num_con("fa", decade) .. ' ' .. lastPart
	elseif (dateArgs.dateType == "decade") then
		local century = getCentury(num_con("en", dateValue))
		nextTierDateCategory = firstPart .. ' سده ' .. num_con("fa", century) .. ' ' .. lastPart
	end

	return nextTierDateCategory
end

local function isCategoryValid(dateValue, dateType, dateArgs)
	if ((dateValue) and (dateType == dateArgs.dateType)) then
		return true
	else
		return false
	end
end

local function getError(pageName, avoidSelf, testcases, errorMessage)
	local errorOut = ''
	if (avoidSelf) then
		local errors = nsc.errorclass(string.format(errorMessage, pageName))
		errorOut = nsc.failedcat(errors, 'P')
		if (testcases) then
			string.gsub(errorOut, '(%[%[)(Category)', '%1:%2')
		end
	end
	return errorOut
end

local function getAvoidSelf(currentTitle, testcases)
	local avoidSelf = (currentTitle.text ~= 'Navseasoncats with year navigation' and
		currentTitle.text ~= 'Navseasoncats with year navigation/doc' and
		currentTitle.text ~= 'Navseasoncats with year navigation/sandbox' and
		(currentTitle.nsText ~= 'Template' or testcases)) --avoid nested transclusion errors
	return avoidSelf
end

local function main(frame, dateArgs)
	local currentTitle = mw.title.getCurrentTitle()
	local testcases = (currentTitle.subpageText == 'testcases')
	local avoidSelf = getAvoidSelf(currentTitle, testcases)

	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)
	
	local testcase = args[1]

	if ((testcase == nil) and (avoidself == false)) then
		return ''
	end

	local pageName = testcase or currentTitle.baseText
	
	local findVar = nsc.find_var(pageName) --picks up decades/seasons/etc.
	if (findVar[1] == 'error') then
		return getError(pageName, avoidSelf, testcases, errorList["FIND_VAR"])
	end

	local dateValue = findVar[2]

	local nav1 = ''
	if (testcase) then
		nav1 = frame:expandTemplate{title = 'Navseasoncats', args = {testcase = testcase}} --not sure how else to pass frame & args together
	else
		nav1 = nsc.navseasoncats(frame)
	end

	local firstPart, lastPart = string.match(pageName, '^(.*)' .. num_con("fa", findVar[2]) .. '(.*)$')
	firstPart = mw.text.trim(firstPart or '')
	lastPart  = mw.text.trim(lastPart or '')

	local nextTierDateCategory = getNestTierDateCategory(dateArgs, dateValue, firstPart, lastPart, decade)

	if (nextTierDateCategory) then
		local nav2 = frame:expandTemplate{title = 'Navseasoncats', args = {[dateArgs.argName] = nextTierDateCategory}} --not sure how else to pass frame & args together
		return '<div style="display:block !important; max-width: calc(100% - 25em);">' .."\n" .. nav1 .. nav2 .."\n" .. '</div>'
	else
		return nav1
	end
end

function p.centuriesBelowDecade(frame)
	local dateArgs = {dateType = "decade", pattern = '^(%d+)$', argName = "century-below-decade", errorMessage = errorList["NO_DECADE"]}
	return main(frame, dateArgs)
end

function p.decadesBelowYear(frame)
	local dateArgs = {dateType = "year", pattern = '^(%d+)$', argName = "decade-below-year", errorMessage = errorList["NO_YEAR"]}
	return main(frame, dateArgs)
end

return p