# Module:Autotaxobox

--*************************************************************************
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.
-  
require('strict')
local TaxonItalics = require('Module:TaxonItalics') -- use a function from Module:TaxonItalics to italicize a taxon name
local TableRow = '|-\n'
local TableEnd = '|}\n'
local p = {} -- functions made public
local l = {} -- internal functions, kept separate
local colour = '' -- colour for taxobox and taxonomy listings  
--=========================================================================
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.
The value can be obtained externally via  
=============================================================================
local MaxSearchLevels = 100  
function p.getMaxSearchLevels
return MaxSearchLevels
end  
--========================== taxoboxColour ================================
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:
=============================================================================
function p.taxoboxColour
return p.getTaxoboxColour(, .args[1] or '')
end  
function p.getTaxoboxColour(, currTaxon)
-- note that colour is global to this function; default is empty string
local i = 1 -- count levels processed
local searching = currTaxon ~= '' -- still searching for a colour?
local foundICTaxon = false -- record whether 'incertae sedis' found
while searching and i <= MaxSearchLevels do
local plainCurrTaxon, dummy = l.stripExtra(currTaxon) -- remove trailing text after /
if string.lower(plainCurrTaxon) == 'incertae sedis' then
foundICTaxon = true
else
local possibleColour = :expandTemplate{ title = 'Template:Taxobox colour', args = { plainCurrTaxon } }
if string.sub(possibleColour,1,3) == 'rgb' then
colour = possibleColour
searching = false
end
end
if searching then
local ok, parent = p.getTaxonInfoItem(, currTaxon, 'parent')
if ok and parent ~= '' then
currTaxon = parent
i = i + 1
else
searching = false -- run off the top of the hierarchy or tried to use non-existent taxonomy template
end
end
end
if colour == '' then
if foundICTaxon then
colour = :expandTemplate{ title = 'Template:Taxobox colour', args = { 'incertae sedis' } }
elseif searching then
-- hierarchy exceeds MaxSearchLevels levels
colour = :expandTemplate{ title = 'Template:Taxobox/Error colour', args = { } }
else
colour = 'transparent'
end
end
return colour
end  
--= = = = = = = = = = = = = topLevelTaxon = = = = = = = = = = = = = = = =
Defines the correct top level taxa, one of which should terminate every
taxonomic hierarchy encoded in taxonomy templates.