Module:Sandbox/Jts1882/Biota infobox/Autotaxobox explained

--

local TaxonItalics = require('Module:TaxonItalics') -- use a function from Module:TaxonItalics to italicize a taxon namelocal p =

--

Limit the maximum depth of a taxonomic hierarchy that can be traversed;avoids excessive processing time and protects against incorrectly set uphierarchies, e.g. loops.

local MaxSearchLevels = 100

function p.getMaxSearchLevels return MaxSearchLevelsend

--taxoboxColour|TAXON}}function p.taxoboxColour(frame) local currTaxon = frame.args[1] or local i = 1 -- count levels processed local searching = currTaxon ~= -- still searching for a colour? local foundICTaxon = false -- record whether 'incertae sedis' found local colour = -- default is no colour while searching and i <= MaxSearchLevels do local plainCurrTaxon = p.stripExtra(currTaxon) -- remove trailing text after / if string.lower(plainCurrTaxon)

'incertae sedis' then foundICTaxon = true else local possibleColour = frame:expandTemplate if string.sub(possibleColour,1,3)

'rgb' then colour = possibleColour searching = false end end if searching then local ok, parent = p.getTaxonInfoItem(frame, 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 return colour elseif foundICTaxon then return frame:expandTemplate elseif searching then -- hierarchy exceeds MaxSearchLevels levels return frame:expandTemplate else return 'transparent' endend

--taxoboxList|TAXON|display_taxa = the number of taxa *above* TAXON to force to be displayed|authority = taxonomic authority for TAXON|parent_authority = taxonomic authority for TAXON's parent|gparent_authority = taxonomic authority for TAXON's grandparent|ggparent_authority = taxonomic authority for TAXON's greatgrandparent|ggparent_authority = taxonomic authority for TAXON's greatgreatgrandparent|bold_first = 'bold' to bold TAXON in its row}}function p.taxoboxList(frame) local currTaxon = frame.args[1] or local displayN = (tonumber(frame.args['display_taxa']) or 1) + 1 local auth = frame.args['authority'] or local parentAuth = frame.args['parent_authority'] or local gParentAuth = frame.args['gparent_authority'] or local ggParentAuth = frame.args['ggparent_authority'] or local gggParentAuth = frame.args['gggparent_authority'] or local boldFirst = frame.args['bold_first'] or 'link' -- values 'link' or 'bold' local taxonTable = p.makeTable(frame, currTaxon) local res = -- display all taxa above possible greatgreatgrandparent for i = taxonTable.n, 6, -1 do res = res .. frame:expandTemplate end -- display greatgreatgrandparent, if it exists if taxonTable.n >= 5 then res = res .. frame:expandTemplate end -- display greatgrandparent, if it exists; force the display if an infrataxon is below if taxonTable.n >= 4 then local force = tostring(displayN >= 4) or frame.expandTemplate

'true' or frame.expandTemplate

'true' res = res .. frame:expandTemplate end -- display grandparent, if it exists; force the display if an infrataxon is below if taxonTable.n >= 3 then local force = tostring(displayN >= 3) or frame.expandTemplate

'true' res = res .. frame:expandTemplate end -- display parent, if it exists if taxonTable.n >= 2 then res = res .. frame:expandTemplate end -- display target taxon res = res .. frame:expandTemplate return resend

--taxonomyList|TAXON}}function p.taxonomyList(frame) local currTaxon = frame.args[1] or if currTaxon

then return '| ||ERROR: no taxon supplied\n|-' end local taxonTable = p.makeTable(frame, currTaxon) local rankTable = p.getRankTable local lastRankVal = 1000000 local orderOk local res = for i = taxonTable.n, 1, -1 do -- check ranks are in right order in the hierarchy local ok, rank = p.getTaxonInfoItem(frame, taxonTable[i], 'rank') local currRankVal = rankTable[string.lower(rank)] if currRankVal then orderOk = currRankVal < lastRankVal if orderOk then lastRankVal = currRankVal end else orderOk = true end -- now return a row of the taxonomy table with anomalous ranks marked local errorStr = if not orderOk then errorStr = 'true' end res = res .. frame:expandTemplate end -- if the last row has an anomalous rank, put the page in the error-tracking category; category statements don't work -- inside tables, so need to close the current table first and then open a dummy one (close is in Template:Taxonomy key) if not orderOk then res = res .. '\n|}\n\n

" .. linkText end return frame:expandTemplateend

--taxonInfo|TAXON|ITEM}}ITEM is one of: 'parent', 'rank', 'link target', 'link text', 'link', 'extinct','always display', 'refs', 'same as' or 'all'.If ITEM is not specified, the default is 'all' – all values in a single stringseparated by '$'.function p.taxonInfo(frame) local taxon = frame.args[1] or local item = frame.args[2] or if item

then item = 'all' end local ok, info = p.getTaxonInfoItem(frame, taxon, item) return infoend

--taxonLink|taxon= : having '/?' at the end triggers the output of ' (?)'|extinct= : 'yes' or 'true' trigger the output of '†'|bold= : 'yes' makes the core output bold and not wikilinked|italic= : 'yes' makes the core output italic|link_target= : target for the wikilinklink_text= : text of the wikilink (may be same as link_target), without †, italics, etc.}}function p.taxonLink(frame) local taxon = frame.args['taxon'] or local extinct = string.lower(frame.args['extinct'] or ) local bold = frame.args['bold'] or local italic = frame.args['italic'] or local linkTarget = frame.args['link_target'] or local linkText = frame.args['link_text'] or frame.args['plain_link_text'] or --temporarily allow alternative args -- if link text is missing, try to find a replacement if linkText

then if string.find(taxon, 'Incertae sedis', 1, true) then linkText = "incertae sedis" linkTarget = 'Incertae sedis' else linkText = p.stripExtra(taxon) end end if linkTarget

then linkTarget = linkText end if italic

'yes' then linkText = TaxonItalics.italicizeTaxonName(linkText, false) end local link = if bold

'yes' then link = '' .. linkText .. '' else if linkTarget

linkText then link = linkText else link = linkTarget .. '

' .. linkText end link = '' .. link .. '' end if (extinct

'yes' or extinct

'true') and not string.find(link, '†', 1, true) then link = '

' .. link end if string.sub(taxon, -2)

'/?' and not string.find(link, '?', 1, true) then link = link .. '

(?)' end return linkend

--showRankTable}}

function p.showRankTable(frame) local rankTable = p.getRankTable local res = '

Ranks checked in taxonomy templates\n! Rank !! Shown as !! Value\n' for k, v in pairs(rankTable) do local rankShown = frame:expandTemplate res = res .. '
-\n' .. k .. '' .. rankShown .. '' .. v .. '\n' end return res .. '
\n'end

--find|TAXON|RANK}}

function p.find(frame) local currTaxon = frame.args[1] or if currTaxon

then return '

no taxon supplied' end local rank = frame.args[2] or if rank

then return '

no rank supplied' end local inHierarchy = true -- still in the taxonomic hierarchy or off the top? local searching = true -- still searching while inHierarchy and searching do local ok, parent = p.getTaxonInfoItem(frame, currTaxon, 'parent') if ok and parent ~= then currTaxon = parent local ok, currRank = p.getTaxonInfoItem(frame, currTaxon, 'rank') if currRank

rank then searching = false end else inHierarchy = false end end if inHierarchy and not searching then return currTaxon else return '

rank not found' endend

--nth|TAXON|n=N}}function p.nth(frame) local currTaxon = frame.args[1] or if currTaxon

then return 'ERROR: no taxon supplied' end local n = tonumber(frame.args['n'] or 1) if n > MaxSearchLevels then return 'Exceeded maximum number of levels allowed (' .. MaxSearchLevels .. ')' end local i = 1 local inHierarchy = true -- still in the taxonomic hierarchy or off the top? while i < n and inHierarchy do local ok, parent = p.getTaxonInfoItem(frame, currTaxon, 'parent') if ok and parent ~= then currTaxon = parent i = i + 1 else inHierarchy = false end end if inHierarchy then return currTaxon else return 'Level ' .. n .. ' is past the top of the taxonomic hierarchy' endend

--nLevels|TAXON}}function p.nLevels(frame) local currTaxon = frame.args[1] or if currTaxon

then return 'ERROR: no taxon supplied' end local i = 1 local inHierarchy = true -- still in the taxonomic hierarchy or off the top? while inHierarchy and i < MaxSearchLevels do local ok, parent = p.getTaxonInfoItem(frame, currTaxon, 'parent') if ok and parent ~= then currTaxon = parent i = i + 1 else inHierarchy = false end end if inHierarchy then return MaxSearchLevels .. '+' else return i endend

--listAll|TAXON}}function p.listAll(frame) local currTaxon = frame.args[1] or if currTaxon

then return 'ERROR: no taxon supplied' end return p.listTaxa(p.makeTable(frame, currTaxon))end

--

Internal functions

--= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Internal 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 endend

--= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Internal utility function to convert a taxon table to a comma-separated list.= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =function p.listTaxa(taxonTable) local lst = taxonTable[1] for i = 2, taxonTable.n, 1 do lst = lst .. ', ' .. taxonTable[i] end return lstend

--= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Internal utility function to extract an item of information from a taxonomy template, following one 'same as' link if required.= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =function p.getTaxonInfoItem(frame, taxon, item) -- item

'link' is a special case if item

'link' then return p.getTaxonInfoLink(frame, taxon) end local ok, info -- item

'dagger' is another special case if item

'dagger' then ok, info = p.getTaxonInfoItem(frame, taxon, 'extinct') if ok then if info

'yes' or info

'true' then info = '&dagger;' else info = end end -- item ~= 'link' or 'dagger' else ok, info = pcall(frame.expandTemplate, frame,) if ok then if info

then -- try 'same as' local sameAsTaxon = frame:expandTemplate if sameAsTaxon ~= then ok, info = pcall(frame.expandTemplate, frame,) end end end end if ok then -- if item is 'link_text' check whether '(?)' needs to be added if item

'link_text' and string.sub(taxon, -2)

'/?' and not string.find(info, '?', 1, true) then info = info .. '

(?)' end else info = '' --error indicator in code before conversion to Lua end return ok, infoend

function p.getTaxonInfoLink(frame, taxon) local ok, linkText, linkTarget local link = ok, linkText = p.getTaxonInfoItem(frame, taxon, 'link_text') if ok then ok, linkTarget = p.getTaxonInfoItem(frame, taxon, 'link_target') if ok then if linkText

linkTarget then link = linkText else link = linkTarget .. '

' .. linkText end end end return ok, linkend

--function p.makeTable(frame, currTaxon) local i = 1 local inHierarchy = true -- still in the taxonomic hierarchy or off the top? local taxonTable = taxonTable[1] = currTaxon; while i < MaxSearchLevels and inHierarchy do local ok, parent = p.getTaxonInfoItem(frame, currTaxon, 'parent') if ok and parent ~= then currTaxon = parent i = i + 1 taxonTable[i] = currTaxon else inHierarchy = false -- run off the top of the hierarchy or tried to use non-existent taxonomy template end end taxonTable.n = i return taxonTableend

--function p.getRankTable return end

return p