Module:Category described in year

This is an old revision of this page, as edited by Tom.Reding (talk | contribs) at 02:12, 9 May 2018 (Mini working example for Fish cats). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals')

local map = {
	['Fish'] = { --group
		['year'] = { --smallest branch, [[Category:Fish described in 1901]]
			['parent1'] = 'century', --'year', 'decade', 'century'
			['parent2'] = 'Animals', --[[Category:Animals described in the 20th century]]
		},
		['century'] = { --parent branch, [[Category:Fish described in the 20th century]]
			['parent1'] = 'formal', --[[Category:Fish by year of formal description]]
			['parent2'] = 'Animals', --[[Category:Animals described in the 20th century]]
		},
	},
}

local p = {}
	
function p.autodetect( frame )
	local currentTitle = mw.title.getCurrentTitle()
	local categories = {}
	local header = 'tbd; This category should only contain species articles, etc....'
	local outString = ''
	
	if currentTitle.namespace == 14 then --Category:
		--determine current cat's properties
		local currCat = currentTitle.text --without namespace nor interwiki prefixes
		local currGroup = mw.ustring.match(currCat, '^%w+') --Fish/Spiders/etc.
		local currYDC = '' --placeholder for year/decade/century
		local currYear = mw.ustring.match(currCat, '%d%d%d%d$')
		local currCent = mw.ustring.match(currCat, 'the (%d+)[snrt][tdh] century')
		if currYear then currYDC = 'year' end
		if currCent then currYDC = 'century' end
		
		local parentCent = ''
		local ord = ''
		if currYear then parentCent = 1 + mw.ustring.match(currYear, '^%d%d') end
		if parentCent then
			local lastd = mw.ustring.match(parentCent, '%d$')
			if lastd == 1 then ord = 'st'
			elseif lastd == 2 then ord = 'nd'
			elseif lastd == 3 then ord = 'rd'
			else ord = 'th' end
		end
		
		local iparent = 1
		local parenti = 'parent' .. iparent
		while currYDC and map[currGroup][currYDC][parenti] do
			if currYDC == 'year' then
				if map[currGroup][currYDC][parenti] == 'century' then
					categories[iparent] = '[[:Category:' .. currGroup .. ' described in the ' .. parentCent .. ord .. ' century]]'
				end
				if map[currGroup][currYDC][parenti] == 'Animals' then
					categories[iparent] = '[[:Category:Animals described in ' .. currYear .. ']]'
				end
			elseif currYDC == 'decade' then
			elseif currYDC == 'century' then
				if map[currGroup][currYDC][parenti] == 'formal' then
					categories[iparent] = '[[:Category:' .. currGroup .. ' by year of formal description|' .. currCent .. ord .. ']]'
				end
				if map[currGroup][currYDC][parenti] == 'Animals' then
					categories[iparent] = '[[:Category:Animals described in the ' .. currCent .. ord .. ' century]]'
				end
			end
			
			iparent = iparent + 1
			parenti = 'parent' .. iparent
		end
		
	elseif currentTitle.namespace == 10 then --Module:/Template: sandbox/testcases/doc
		
	end
	
	outString = header .. outString
	
	if string.sub(currentTitle.subpageText,1,9) == 'testcases' then
		outString = outString .. mw.text.nowiki(table.concat(categories)) .. '<br />'
	elseif currentTitle.namespace == 14 then --Category:
		outString = outString .. table.concat(categories)
	else
		outString = ''
	end
	
	return outString
end

return p