پودمان:Navseasoncats with centuries below decade

از اسلامیکال
پرش به ناوبری پرش به جستجو

توضیحات این پودمان می‌تواند در پودمان:Navseasoncats with centuries below decade/توضیحات قرار گیرد.

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

function p.cbd( frame )
	local currtitle = mw.title.getCurrentTitle()
	local testcases = (currtitle.subpageText == 'آزمایشی')
	local avoidself =  (currtitle.text ~= 'Navseasoncats with centuries below decade' and          --avoid self
						currtitle.text ~= 'Navseasoncats with centuries below decade/توضیحات' and      --avoid self
						currtitle.text ~= 'Navseasoncats with centuries below decade/تمرین' and  --avoid self
						(currtitle.nsText ~= 'الگو' or testcases)) --avoid nested transclusion errors
	
	local testcase = frame:getParent().args[1]
	if testcase == nil and avoidself == false then return '' end
	
	local pagename = testcase or currtitle.baseText
	local findvar = nsc.find_var(pagename) --picks up decades/seasons/etc.
	if findvar[1] == 'error' then
		local errorout = ''
		if avoidself then
			local errors = nsc.errorclass('from findvar error '..'findvar[1]='..findvar[1]..'findvar[2]='..findvar[2])
			errorout = nsc.failedcat(errors, 'P')
			if testcases then mw.ustring.gsub(errorout, '(%[%[)(Category)', '%1:%2') end
		end
		return errorout
	end
	
	local decade = tonumber(num_con("en", findvar[2]))
	if decade == nil then
		local errorout = ''
		if avoidself then
			local errors = nsc.errorclass('from decade==nil'..'findvar[1]='..findvar[1]..'findvar[2]='..findvar[2])
			errorout = nsc.failedcat(errors, 'P')
			if testcases then mw.ustring.gsub(errorout, '(%[%[)(Category)', '%1:%2') end
		end
		return errorout
	end
	
	local firstpart, lastpart = mw.ustring.match(pagename, '^(.*)'..num_con("fa", decade)..'(.*)$')
	firstpart = mw.text.trim(firstpart or '')
	lastpart  = mw.text.trim(lastpart or '')
	
	local nav1 = ''
	if testcase then
		local args = { testcase = testcase }
		nav1 = frame:expandTemplate{ title = 'Navseasoncats', args = args } --not sure how else to pass frame & args together
	else
		nav1 = nsc.navseasoncats(frame)
	end
	
	local century = math.floor( ((num_con("en", decade)-1)/100) + 1 ) --from {{CENTURY}}
	if mw.ustring.match(decade, '۰۰$') then century = century + 1 end --'2000' is technically in the 20th, but the rest of the 2000s is in the 21st
	
	local firstpart_cen = mw.ustring.gsub(firstpart, "دهه", "سده", 1 )
	local centurycat = mw.text.trim(firstpart_cen..' '..num_con("fa", century)..' '..lastpart)
	local exists = mw.title.new( centurycat, 'رده' ).exists
	
	if exists then
		local args = { ['century-below-decade'] = centurycat }
		local nav2 = frame:expandTemplate{ title = 'Navseasoncats', args = args } --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

return p