وحدة:Navseasoncats with centuries below decade

من ويكيبيديا، الموسوعة الحرة

local p = {}
local nsc = require('Module:Navseasoncats')

function p.cbd( frame )
	local currtitle = mw.title.getCurrentTitle()
	local testcases = (currtitle.subpageText == 'testcases')
	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/doc' and      --avoid self
						currtitle.text ~= 'Navseasoncats with centuries below decade/sandbox' and  --avoid self
						currtitle.text ~= 'Navseasoncats with centuries below decade/ملعب' and  --avoid self
						(currtitle.nsText ~= 'Template' or 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('Function find_var can\'t recognize the decade for category "'..pagename..'".')
			errorout = nsc.failedcat(errors, 'P')
			if testcases then string.gsub(errorout, '(%[%[)(تصنيف)', '%1:%2') end
		end
		return errorout
	end
	mw.log("findvar[1]"  .. findvar[1])
	mw.log("findvar[2]"  .. findvar[2])
	local decade = tonumber(string.match(findvar[2], '^(%d+)'))
	if decade == nil or findvar[1] ~= 'decade' then
		local errorout = ''
		if avoidself then
			local errors = nsc.errorclass('{{Navseasoncats with centuries below decade}} can\'t recognize the decade for category "'..pagename..'".')
			errorout = nsc.failedcat(errors, 'P')
			if testcases then string.gsub(errorout, '(%[%[)(تصنيف)', '%1:%2') end
		end
		return errorout
	end
	
	local firstpart, lastpart = string.match(pagename, '^(.*)عقد '..findvar[2]..'(.*)$')
	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 to pass frame & args together
	else
		nav1 = nsc.navseasoncats(frame)
	end
	
	local century = math.floor( ((decade-1)/100) + 1 ) --from {{CENTURY}}
	if string.match(decade, '00$') then century = century + 1 end --'2000' is technically in the 20th, but the rest of the 2000s is in the 21st
	local centurycat = mw.text.trim( firstpart..' القرن '..nsc.addord(century)..' '..lastpart )
	mw.log( "centurycat: " .. centurycat  )
	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 to pass frame & args together
		return nav1..nav2
	else
		return nav1
	end
end

return p