Jump to content

Module:Autotaxobox/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Peter coxhead (talk | contribs) at 11:33, 8 December 2016 (Created page with '--[[ This module provides support to the automated taxobox system – the templates Automatic taxobox, Speciesbox, Subspeciesbox, Infraspeciesbox, etc. In parti...'). 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)
--[[
This module provides support to the automated taxobox system – the templates
Automatic taxobox, Speciesbox, Subspeciesbox, Infraspeciesbox, etc.

In particular it provides a way of traversing the taxonomic hierarchy encoded
in taxonomy templates (templates with names of the form
"Template:Taxonomy/TAXON_NAME") without causing template expansion depth errors.
]]

local p = {}

--[[
Limit the maximum depth of a taxonomic hierarchy that can be traversed;
avoids excessive processing time and protects against incorrectly set up
hierarchies, e.g. loops.
]]
local maxN = 100 

--[[
Utility function to strip off any extra parts of a taxon name, i.e. anything
after a '/'. Thus "Felidae/?" would be reduced to "Felidae".
]]
function p.stripExtra(taxonName)
	local i = string.find(taxonName,'/')
	if i then
		return string.sub(taxonName,1,i-1)
	else
		return taxonName
	end
end

--[[
Utility function primarily intended for use in checking and debugging.
Returns the nth ancestor of a taxon, where the parent is counted as the 1st
ancestor.
Usage: {{#invoke:Autotaxobox|nth|TAXON|n=N}}
]]
function p.nth(frame)
	local currTaxon = frame.args[1]
	local n = tonumber(frame.args['n'])
	if n > maxN then return 'Exceeded maximum number of levels allowed (' .. maxN .. ')' end
	local i = 1
	local searching = true
	while i < n and searching do
		local currCall = 'Template:Taxonomy/' .. currTaxon
		local parent = frame:expandTemplate{ title = currCall, args = {['machine code'] = 'parent' } }
		if parent ~= '' then
			currTaxon = parent
			i = i + 1
		else
			searching = false
		end
	end
	if searching then return currTaxon
	else return 'Nothing at level ' .. n
	end
end

--[[
Utility function primarily intended for use in checking and debugging.
Returns number of levels in a taxonomic hierarchy, starting from
the supplied taxon as level 1.
Usage: {{#invoke:Autotaxobox|nLevels|TAXON}}
]]
function p.nLevels(frame)
	local currTaxon = frame.args[1]
	local i = 1
	local searching = true
	while searching and i < maxN  do
		local parent = frame:expandTemplate{ title = 'Template:Taxonomy/' .. currTaxon, args = {['machine code'] = 'parent' } }
		if parent ~= '' then
			currTaxon = parent
			i = i + 1
		else
			searching = false
		end
	end
	if searching then return maxN .. '+'
	else return i
	end
end

--[[
Utility function primarily intended for use in checking and debugging.
Returns a comma separated list of a taxonomic hierarchy, starting from
the supplied taxon.
Usage: {{#invoke:Autotaxobox|listAll|TAXON}}
]]
function p.listAll(frame)
	local currTaxon = frame.args[1]
	local i = 1
	local searching = true
	local lst = currTaxon
	while i < maxN and searching do
		local currCall = 'Template:Taxonomy/' .. currTaxon
		local parent = frame:expandTemplate{ title = currCall, args = {['machine code'] = 'parent' } }
		if parent ~= '' then
			currTaxon = parent
			lst = lst .. ', ' .. currTaxon
			i = i + 1
		else
			searching = false
		end
	end
	if searching then lst = lst .. '...' end
	return lst
end

--[[ probably not for ultimate release
function p.taxoboxColourList(frame)
	local currTaxon = frame.args[1]
	local i = 1
	local searching = currTaxon ~= ''
	local lst = ''
	while i < maxN and searching do
		local colour = frame:expandTemplate{ title = 'Template:Taxobox colour', args = { currTaxon } }
		if string.sub(colour,1,3) == 'rgb' then
			lst = lst .. ', ' .. currTaxon
		end
		local parent = frame:expandTemplate{ title = 'Template:Taxonomy/' .. currTaxon, args = {['machine code'] = 'parent' } }
		if parent ~= '' then
			currTaxon = parent
			i = i + 1
		else
			searching = false
		end
	end
	return lst
end
]]

--[[
Determines the correct colour for a taxobox, by searching up the taxonomic
hierarchy from the supplied taxon for the first taxon (other than
'incertae sedis') that sets a taxobox colour. It is assumed that a valid
taxobox colour is defined using CSS rgb() syntax.
If no taxon that sets a taxobox colour is found, then 'transparent' is
returned unless the taxonomic hierarchy is too deep, when the error colour is
returned.
Usage: {{#invoke:Autotaxobox|taxoboxColour|TAXON}}
]]
function p.taxoboxColour(frame)
	local currTaxon = frame.args[1]
	local i = 1
	local searching = currTaxon ~= ''
	local colour = ''
	local foundICTaxon = false -- record whether 'incertae sedis' found
	while searching and i < maxN do
		local plainCurrTaxon = p.stripExtra(currTaxon)
		if string.lower(plainCurrTaxon) == 'incertae sedis' then
			foundICTaxon = true
		else
			local possColour = frame:expandTemplate{ title = 'Template:Taxobox colour', args = { plainCurrTaxon } }
			if string.sub(possColour,1,3) == 'rgb' then
				colour = possColour
				searching = false
			end
		end
		local parent = frame:expandTemplate{ title = 'Template:Taxonomy/' .. currTaxon, args = {['machine code'] = 'parent' } }
		if parent ~= '' then
			currTaxon = parent
			i = i + 1
		else
			searching = false
		end
	end
	if colour ~= '' then
		return colour
	elseif foundICTaxon then
		return frame:expandTemplate{ title = 'Template:Taxobox colour', args = { 'incertae sedis' } }
	elseif searching then
		return frame:expandTemplate{ title = 'Template:Taxobox/Error colour', args = { } }
	else
		return 'transparent'
	end
end

return p