Jump to content

Module:Geological time

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Hike395 (talk | contribs) at 20:29, 12 December 2021 (finish coding). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local hatnoteModule = require('Module:Hatnote')
local hatnote = hatnoteModule._hatnote
local formatLink = hatnoteModule._formatLink
local makeWikitextError = hatnoteModule.makeWikitextError

local p = {}

p.eon = {'Hadean', 'Archean', 'Proterozoic', 'Phanerozoic'}
p.era = {'Eoarchean', 'Paleoarchean', 'Mesoarchean', 'Neoarchean',
	'Paleoproterozoic', 'Mesoproterozoic', 'Neoproterozoic',
	'Paleozoic', 'Mesozoic', 'Cenozoic'}
p.period = {'Siderian', 'Rhyacian', 'Orosirian', 'Statherian',
	'Calymmian', 'Ectasian', 'Stenian',
	'Tonian', 'Cryogenian', 'Ediacaran',
	'Cambrian', 'Ordovician', 'Silurian', 'Devonian', 'Carboniferous', 'Permian',
	'Triassic', 'Jurassic', 'Cretaceous', 'Paleogene', 'Neogene', 'Quaternary'}
p.epoch = {'Terraneuvian', 'Cambrian Series 2', 'Miaolingian', 'Furongian',
	'Early Ordovician', 'Middle Ordovician', 'Late Ordovician',
	'Llandovery', 'Wenlock', 'Ludlow', 'Pridoli',
	'Early Devonian', 'Middle Devonian', 'Late Devonian',
	'Mississippian', 'Pennsylvanian',
	'Cisuralian', 'Guadalupian', 'Lopingian',
	'Early Triassic', 'Middle Triassic', 'Late Triassic',
	'Early Jurassic', 'Middle Jurassic', 'Late Jurassic',
	'Early Cretaceous', 'Late Cretaceous',
	'Paleocene', 'Eocene', 'Oligocene',
	'Miocene', 'Pliocene', 'Pleistocene', 'Holocene'}
p.age = {'Fortunian', 'Cambrian Stage 2', 'Cambrian Stage 3', 'Cambrian Stage 4',
	'Wuliuan', 'Drumian', 'Guzhangian', 'Paibian', 'Jiangshanian', 'Cambrian Stage 10',
	'Tremadocian', 'Floian', 'Dapingian', 'Darriwilian', 'Sandbian', 'Katian', 'Hirnantian',
	'Rhuddanian', 'Aeronian', 'Telychian', 'Sheinwoodian', 'Homerian', 'Gortian', 'Ludfordian', 'Pridoli',
	'Lochkovian', 'Pragian', 'Emsian', 'Eifelian', 'Givetian', 'Frasnian', 'Famennian',
	'Tournaisian', 'Viséan', 'Serpukhovian', 'Bashkirian', 'Moscovian', 'Kasimovian', 'Gzhelian',
	'Asselian', 'Sakmarian', 'Artinskian', 'Kungurian', 'Roadian', 'Wordian', 'Capitanian', 'Wuchiapingian', 'Changhsingian',
	'Induan', 'Olenekian', 'Anisian', 'Ladinian', 'Carnian', 'Norian', 'Rhaetian',
	'Hettangian', 'Sinemurian', 'Pliensbachian', 'Toarcian', 'Aalenian', 'Bajocian',
	'Bathonian', 'Callovian', 'Oxfordian', 'Kimmeridgian', 'Tithonian',
	'Berriasian','Valanginian','Hauterivian','Barremian','Aptian','Albian',
	'Cenomanian','Turonian','Coniacian','Santonian','Campanian','Maastrichtian',
	'Danian','Selandian','Thanetian','Ypresian','Lutetian','Bartonian','Priabonian','Rupelian','Chattian',
	'Aquitanian','Burdigalian','Langhian','Serravallian','Tortonian','Messinian','Zanclean','Piacenzian',
	'Gelasian','Calabrian','Calabrian','Late Pleistocene','Greenlandian','Northgrippian','Meghalayan'}

local function startsWith(s, sub)
	sLen = mw.ustring.len(s)
	subLen = mw.ustring.len(sub)
	if subLen > sLen then
		return false
	end
	return mw.ustring.sub(s:lower(),1,subLen) == sub:lower()
end

local function find(s)
	for _, list in ipairs({p.eon, p.era, p.period, p.epoch, p.age}) do
		local listLen = #list
		for i, span in ipairs(list) do
			if startsWith(s, span) then
				prev = i > 1 and list[i-1]
				next = i < listLen and list[i+1]
				return {prev=prev, next=next}
			end
		end
	end
	return nil		
end

function p._pair(namespace, prev, next)
	local prevTitle = prev and mw.title.new(prev,namespace)
	local nextTitle = next and mw.title.new(next,namespace)
	if not prevTitle and not nextTitle then
		return ""
	end
	prevTitle = prevTitle.exists and prevTitle
	nextTitle = nextTitle.exists and nextTitle
	if not prevTitle and not nextTitle then
		return ""
	end
	local note = "See also the"
	if prevTitle then
		note = note.." preceding "..formatLink{link=prevTitle.fullText}
	end
	if prevTitle and nextTitle then
		note = note.." and the"
	end
	if nextTitle then
		note = note.." succeeding "..formatLink{link=nextTitle.fullText}
	end
	return hatnote(note,{extraclasses="seealso"})
end

function p._seeAlso(args)
	local title = args[1] and mw.title.new(args[1]) or mw.title.getCurrentTitle()
	if not title then
		return makeWikitextError("Title failure in Module:Geological time")
	end
	local basetext = title.baseText
	local namespace = title.namespace
	local adjacent = find(basetext)
	if not adjacent then
		return makeWikitextError("Cannot find geological time frame in "..basetext)
	end
	return p._pair(namespace, adjacent.prev, adjacent.next)
end

function p.seeAlso(frame)
	local args = getArgs(frame)
	return p._seeAlso(args)
end