Jump to content

Module:Category described in year/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Tom.Reding (talk | contribs) at 01:06, 11 May 2018 (Import). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
require('Module:No globals')

--case sensitive conf/map table
local map = {
	['Fish'] = { --group
		['min'] = 1758, --integer; lowest possible year (and thus decade) displayed in nav bars; default to 1758 per ICZN Art. 5
		['year'] = { --[[Category:Fish described in 1901]]
			['description'] = 'tbd; year; This category should only contain species articles, etc....',
			['parent1'] = 'century', --year/decade/century/formal, i.e. 'century' for [[Category:Fish described in the 20th century]]
			['parent2'] = 'Animals', --[[Category:Animals described in the 20th century]]
		},
		['century'] = { --[[Category:Fish described in the 20th century]]
			['description'] = 'tbd; century; container category, etc....',
			['parent1'] = 'formal', --[[Category:Fish by year of formal description]]
			['parent2'] = 'Animals', --[[Category:Animals described in the 20th century]]
		},
	},
	['Spiders'] = { --group
		['min'] = 1757, --integer; lowest possible year (and thus decade) displayed in nav bars; default to 1758 per ICZN Art. 5
		['year'] = { --[[Category:Spiders described in 1901]]
			['description'] = 'tbd; year; This category should only contain species articles, etc....',
			['parent1'] = 'decade', --[[Category:Spiders described in the 1900s]]
			['parent2'] = 'Animals', --[[Category:Animals described in 1900]]
		},
		['decade'] = { 
			['description'] = 'tbd; decade; container category, etc....',
			['parent1'] = 'century', --[[Category:Spiders described in the 20th century]]
			['parent2'] = 'Animals', --[[Category:Animals described in the 1900s]]
		},
		['century'] = { 
			['description'] = 'tbd; century; container category, etc....',
			['parent1'] = 'formal', --[[Category:Spiders by year of formal description]]
			['parent2'] = 'Animals', --[[Category:Animals described in the 20th century]]
		},
	},
}

local function addOrd( i ) --20 -> 20th, etc.
	if tonumber(i) then
		local s = tostring(i)
		local lastd = mw.ustring.match(s, '%d$')
		if     lastd == '1' then return s..'st'
		elseif lastd == '2' then return s..'nd'
		elseif lastd == '3' then return s..'rd'
		elseif lastd ~= nil then return s..'th'
		else return '' end
	else
		return ''
	end
end

local p = {}
	
function p.autodetect( frame )
	local currentTitle = mw.title.getCurrentTitle()
	local header = ' ' --header template(s), nav bar, and category description text; whitespace-initialized for convenience
	local nav = nil
	local description = nil
	local categories = {}
	local outString = nil
	
	if currentTitle.namespace == 14 or --Category:
	   currentTitle.namespace == 10 then --Module:/Template: sandbox/testcases/doc
		
		--prelim cat/mod/temp housekeeping
		local currCat = nil
		if currentTitle.namespace == 14 then 
			currCat = currentTitle.text --without namespace nor interwiki prefixes
		else
			--accept 1 category parameter for testing/doc purposes
			currCat = '...'
		end
		
		--determine current/related/adjacent cats' properties/vars/etc
		local currGroup = mw.ustring.match(currCat, '^%w+') --Fish/Spiders/etc.
		local currYDC = nil --placeholder for year/decade/century
		local currYear = mw.ustring.match(currCat, 'in (%d%d%d%d)$')
		local currDeca = mw.ustring.match(currCat, 'the (%d%d%d%d)s$')
		local currCent = mw.ustring.match(currCat, 'the (%d+)[snrt][tdh] century')
		local parentDeca = nil --used with currYear (i.e. Spiders) & currDeca (i.e. Spiders)
		local parentCent = nil --used with currYear (i.e. Fish) & currDeca (i.e. Spiders)
		local lastCent, nextCent = nil, nil --used with currYear (i.e. Fish) & currCent (i.e. Fish)
		local minYear = tonumber(map[currGroup].min)
		if    minYear == nil then minYear = 1758 end --default to 1758 per ICZN Art. 5
		if currYear then
			currYDC = 'year'
			parentDeca = mw.ustring.match(currYear, '^(%d%d%d)%d$') .. '0'
			if mw.ustring.match(currYear, '^%d%d00') then --1900 in 19th century
				parentCent = mw.ustring.match(currYear, '^%d%d')
			else --1901 in 20th century
				parentCent = 1 + mw.ustring.match(currYear, '^%d%d')
			end
			lastCent = parentCent - 1
			nextCent = parentCent + 1
		elseif currDeca then
			currYDC = 'decade'
			parentDeca = mw.ustring.match(currDeca, '^(%d%d%d)%d$') .. '0'
			parentCent = mw.ustring.match(parentDeca, '^%d%d') + 1
		elseif currCent then
			currYDC = 'century'
			lastCent = currCent - 1
			nextCent = currCent + 1
		else
			return '' --add err msg/cat?
		end
		description = map[currGroup][currYDC].description
		
		--produce cats & navs
		local iparent = 1
		local parenti = 'parent' .. iparent
		while map[currGroup][currYDC][parenti] do
			local parent = map[currGroup][currYDC][parenti]
			
			if currYDC == 'year' then
				if nav == nil then
					local args = { year = currYear, min = minYear, cat = currGroup .. ' described in' }
					nav = frame:expandTemplate{ title = 'Category in year', args = args }
				end
				if parent == 'decade' then
					categories[iparent] = '[[Category:' .. currGroup .. ' described in the ' .. parentDeca .. 's]]'
				elseif parent == 'century' then
					categories[iparent] = '[[Category:' .. currGroup .. ' described in the ' .. addOrd(parentCent) .. ' century]]'
				else --i.e. Animals; require capital first letter?
					categories[iparent] = '[[Category:' .. parent .. ' described in ' .. currYear .. ']]'
				end
				
			elseif currYDC == 'decade' then
				if nav == nil then
					nav = frame:expandTemplate{ title = 'Container category' }
					local args = { decade = currDeca, min = minYear, cat = currGroup .. ' described in the' }
					nav = frame:expandTemplate{ title = 'Category by decade', args = args }
				end
				if parent == 'century' then
					categories[iparent] = '[[Category:' .. currGroup .. ' described in the ' .. addOrd(parentCent) .. ' century]]'
				else --i.e. Animals; require capital first letter?
					categories[iparent] = '[[Category:' .. parent .. ' described in the ' .. currDeca .. 's]]'
				end
				
			elseif currYDC == 'century' then
				if nav == nil then
					nav = frame:expandTemplate{ title = 'Container category' }
					local args = { currGroup .. ' described in the ' .. addOrd(lastCent) .. ' century',
								   currGroup .. ' described in the ' .. addOrd(nextCent) .. ' century' }
					nav = nav .. frame:expandTemplate{ title = 'Category pair', args = args }
				end
				if parent == 'formal' then
					categories[iparent] = '[[Category:' .. currGroup .. ' by year of formal description|' .. addOrd(currCent) .. ']]'
				else --i.e. Animals; require capital first letter?
					categories[iparent] = '[[Category:' .. parent .. ' described in the ' .. addOrd(currCent) .. ' century]]'
				end
			end
			
			iparent = iparent + 1
			parenti = 'parent' .. iparent
		end
		
	end
	
	--produce header
	if nav then header = nav end
	if description then header = header .. '\n\n' .. description end
	header = mw.text.trim(header)
	
	--append header to outString
	if outString then outString = outString .. header
	else outString = header end
	
	--append cats to outString
	if currentTitle.namespace == 10 then --Module:/Template: sandbox/testcases/doc
		outString = outString .. mw.string.gsub(table.concat(categories, '<br />'), '%[%[%', '[[:') .. '<br />'
	elseif currentTitle.namespace == 14 then --Category:
		outString = outString .. table.concat(categories)
	else
		outString = ''
	end
	
	return outString
end

return p