- Bombus (Pyrobombus) ~ Bombus (Pyrobombus)
- Bombus (Pyrobombus) ~ B. (Pyrobombus)
- Bombus (Pyrobombus) ~ Pyrobombus [link text may just be the subgenus]
- Heteractinida ~ "Heteractinida" [double-quotes are ignored in link text]
- "Heteractinida" ~ Heteractinida [double-quotes are ignored in base taxon name]
- Incertae sedis ~ anything [link text is ignored for matching in this case]
- Cetotheriidae with qualifier=? ~ Cetotheriidae (?)
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =]]
function l.matchTaxonLink(taxon, linkText, rankBelowGenus)
local dummy
linkText, dummy = mw.ustring.gsub(linkText, "*", *) -- remove any italic wikitext in the link text
linkText, dummy = mw.ustring.gsub(linkText, '<.->', '') -- strip all tags used to format the link text
linkText, dummy = mw.ustring.gsub(linkText, '"', '') -- remove any occurrences of " in the link text
local baseTaxon, disambig, subgenus, qualifier = l.splitTaxonName(taxon) -- split up the taxon name
baseTaxon, dummy = mw.ustring.gsub(linkText, '"', '') -- remove any occurrences of " in the base taxon name
local match = linkText == baseTaxon or
linkText == subgenus or
linkText == baseTaxon .. ' (' .. subgenus .. ')' or
linkText == mw.ustring.sub(baseTaxon, 1, 1) .. '. (' .. subgenus .. ')' or
baseTaxon == 'Incertae sedis' or
rankBelowGenus and linkText == mw.ustring.gsub(baseTaxon, '([A-Z]).- (.*)', '%1. %2') or
mw.ustring.find(qualifier, '?', 1, true) and mw.ustring.find(linkText, baseTaxon, 1, true) == 1
return match
end  
--[[= = = = = = = = = = = = = makeTable = = = = = = = = = = = = = = = = = = =
Internal utility function to return a table (array) constructed from a
taxonomic hierarchy stored in "Template:Taxonomy/..." templates.
TABLE.n holds the total number of taxa; TABLE[1]..TABLE[TABLE.n] the taxon
names.
The last taxon in the table will either (a) have a taxonomy template but with
no parent given (e.g. 'Life') or (b) not have a taxonomy template.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =]]
function l.makeTable(, currTaxon)
local taxonTable = {}
local taxonRankTable = {}
local ok, rank, parent
local i = 1
local topReached = false -- reached the top of the taxonomic hierarchy?
repeat
taxonTable[i] = currTaxon
ok, rank = p.getTaxonInfoItem(, currTaxon, 'rank')
if ok then taxonRankTable[i] = string.lower(rank) else taxonRankTable[i] = '' end
ok, parent = p.getTaxonInfoItem(, currTaxon, 'parent')
if ok and parent ~= '' then
currTaxon = parent
i = i + 1
else
topReached = true -- reached the top of the hierarchy or tried to use a non-existent taxonomy template
end
until topReached or i > MaxSearchLevels
taxonTable.n = math.min(i, MaxSearchLevels)
return taxonTable, taxonRankTable
end  
--= = = = = = = = = = = = getRankTable = = = = = = = = = = = = = = = = = =
Internal utility function to set up a table of numerical values corresponding
to 'Linnaean' ranks, with upper ranks having higher values. In a valid
taxonomic hierarchy, a lower rank should never have a higher value than a
higher rank. The actual numerical values are arbitrary so long as they are
ordered.
The ranks should correspond to those in Template:Anglicise ranks.  
function l.getRankTable
return {
classis = 1400,
cohort = 1100,
divisio = 1500,
domain = 1700,
epifamilia = 802,
familia = 800,
forma = 100,
genus = 600,
grandordo = 1005,
['grandordo-mb'] = 1002,
hyperfamilia = 805;
infraclassis = 1397,
infralegio = 1197,
infraordo = 997,
infraphylum = 1497,
infraregnum = 1597,
infratribus = 697,
legio = 1200,
magnordo = 1006,
microphylum = 1495,
micrordo = 995,
mirordo = 1004,
['mirordo-mb'] = 1001,
nanophylum = 1494,
nanordo = 994,
ordo = 1000,
parafamilia = 800,
parvclassis = 1396; -- same as subterclassis
parvordo = 996,
phylum = 1500,
regnum = 1600,
sectio = 500,
--series = 400, used too inconsistently to check
species = 300,
subclassis = 1398,
subcohort = 1098,
subdivisio = 1498,
subfamilia = 798,
subgenus = 598,
sublegio = 1198,
subordo = 998,
subphylum = 1498,
subregnum = 1598,
subsectio = 498,
subspecies = 298,
subterclassis = 1396; -- same as parvclassis
subtribus = 698,
superclassis = 1403,
supercohort = 1103,
superdivisio = 1503,
superdomain = 1703,
superfamilia = 803,
superlegio = 1203,
superordo = 1003,
superphylum = 1503,
superregnum = 1603,
supersectio = 503,
supertribus = 703,
tribus = 700,
varietas = 200,
zoodivisio = 1300,
zoosectio = 900,
zoosubdivisio = 1298,
zoosubsectio = 898,
}
end  
--= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
Function to look up the arbitrary numerical value of a rank in a rank value
table. "Ichno" and "oo" ranks are not stored separately, so if present the
prefix is removed.  
function l.lookupRankVal(rankValTable, rank)
local rankVal = rankValTable[rank]
if not rankVal then
-- may be an "ichno" or "oo" rank; try removing "ichno-" or "oo-"
local baseRank = mw.ustring.gsub(mw.ustring.gsub(rank, '^ichno', *), '^oo', *)
if baseRank == 'rdo' then baseRank = 'ordo' end
-- if an "ichno" or "oo" rank, lower rank value slightly so it is ok below the base rank
rankVal = rankValTable[baseRank]
if rankVal then
rankVal = rankVal - 0.1
end
end
return rankVal
end  
--= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =  
function l.tableCell(arg1, arg2)
local text, style
if arg2 then
style = arg1
text = arg2
else
text = arg1
end
local res = '|'
if style ~= '' then
res = res .. style .. '|'
end
return res .. text .. '\n'
end  
return p