end
-- now generate a row of the table
res = res .. TableRow
local cellContent = ''
local anglicizedRank = :expandTemplate{ title = 'Template:Anglicise rank', args = { rank } }
if boldRank then
cellContent = cellContent .. '<b>' .. anglicizedRank .. '</b>:'
else
cellContent = cellContent .. anglicizedRank .. ':'
end
if error == 'yes' then
cellContent = '<span>' .. cellContent .. '</span>'
end
res = res .. l.tableCell(cellContent)
.. l.tableCell('<span>' .. link .. '</span>')
.. l.tableCell('<span>' .. qualifier .. '</span>')
.. l.tableCell('<span>' .. :expandTemplate{ title = 'Template:Edit a taxon', args = { taxon } } .. '</span>')
return res
end  
--========================= callTaxonomyKey ===============================
Prepares for, and then calls, Template:Taxonomy key to display a taxonomy
template page. It does this by building up the information the template
requires, following one 'same as' link, if required.
Usage:  
=============================================================================
local PARENT = 1
local RANK = 2
local LINK_TARGET = 3
local LINK_TEXT = 4
local ALWAYS_DISPLAY = 5
local EXTINCT = 6
local SAME_AS = 7
local REFS = 8  
function p.callTaxonomyKey
local taxon = .args['taxon'] or ''
local parent = .args['parent'] or ''
local rank = .args['rank'] or ''
local extinct = string.lower(.args['extinct']) or ''
local alwaysDisplay = string.lower(.args['always_display']) or ''
local linkTarget = .args['link_target'] or ''
local linkText = .args['link_text'] or * -- this is the "raw" link text, and can be *
local refs = .args['refs'] or ''
local sameAsTaxon = .args['same_as'] or ''
if sameAsTaxon ~= '' then
-- try using the 'same as' taxon; it's an error if it doesn't exist
local ok, sameAsInfoStr = pcall(.expandTemplate, , { title = 'Template:Taxonomy/' .. sameAsTaxon, args = {['machine code'] = 'all' } })
if ok then
local sameAsInfo = mw.text.split(sameAsInfoStr, '$', true)
--'same as' taxon's taxonomy template must not have a 'same as' link
if sameAsInfo[SAME_AS] == '' then
if parent == '' then parent = sameAsInfo[PARENT] end
if rank == '' then rank = sameAsInfo[RANK] end
if extinct == '' then extinct = string.lower(sameAsInfo[EXTINCT]) end
if alwaysDisplay == '' then alwaysDisplay = string.lower(sameAsInfo[ALWAYS_DISPLAY]) end
if linkTarget == '' then linkTarget = sameAsInfo[LINK_TARGET] end
if linkText == '' then linkText = sameAsInfo[LINK_TEXT] end
if refs == '' and parent == sameAsInfo[PARENT] then refs = sameAsInfo[REFS] end
else
return '<span>Error: attempt to follow two "same as" links</span>: <code>same_as = ' .. sameAsTaxon .. '</code>, but Template:Taxonomy/' .. sameAsTaxon .. ' also has a<code>same_as</code> parameter.'
end
else
return :expandTemplate{ title = 'Template:Taxonomy key/missing template', args = {taxon=sameAsTaxon, msg='given as the value of <code>same as</code>'} }
end
end
local link = linkTarget
if linkText ~= '' and linkText ~= linkTarget then link = link .. "|" .. linkText end
-- check consistency of extinct status; if this taxon is not extinct, parent must not be either
local extinctError = 'no'
if parent ~= * and (extinct == * or extinct == 'no' or extinct == 'false') then
local ok, parentExtinct = p.getTaxonInfoItem(, parent, 'extinct')
if ok and (parentExtinct == 'yes' or parentExtinct == 'true') then extinctError = 'yes' end
end
return :expandTemplate{ title = 'Template:Taxonomy key',
args = {taxon=taxon, parent=parent, rank=rank, extinct=extinct, always_display=alwaysDisplay, link_target=linkTarget, link=link, refs=refs, same_as=sameAsTaxon, extinct_error = extinctError} }
end  
--============================= showRefs ==================================
Shows the refs field in a taxonomy template, handing incertae sedis taxa and
using '–' for absent refs.
Usage:
=============================================================================
function p.showRefs
local taxonName = .args[1] or ''
local refs = .args[2] or ''
return l.doShowRefs(taxonName, refs)
end  
--= = = = = = = = = = = = = = doShowRefs = = = = = = = = = = = = = = = = =
Show the refs field in a taxonomy template.  
function l.doShowRefs(taxonName, refs)
if mw.text.split(taxonName, '/', true)[1] == 'Incertae sedis' then
refs = 'not applicable (<i>incertae sedis</i>)'
elseif refs == '' then
refs = '–'
end
return refs
end  
--============================ taxonInfo ==================================
Extracts and returns information from Template:Taxonomy/TAXON, following
one 'same as' link if required.
Usage:
ITEM is one of: 'parent', 'rank', 'link target', 'link text', 'extinct',
'always display', 'refs', 'same as' or 'all'.
If ITEM is not specified, the default is 'all' – all values in a single string
separated by '$'.
=============================================================================
function p.taxonInfo
local taxon = .args[1] or ''
local item = .args[2] or ''
if item == '' then item = 'all' end
local ok, info = p.getTaxonInfoItem(, taxon, item)
return info
end  
--= = = = = = = = = = = getTaxonInfoItem = = = = = = = = = = = = = = = = =
Utility function to extract an item of information from a
taxonomy template, following one 'same as' link if required.  
function p.getTaxonInfoItem(, taxon, item)
local ok, info
-- item == 'dagger' is a special case
if item == 'dagger' then
ok, info = p.getTaxonInfoItem(, taxon, 'extinct')
if ok then
if info == 'yes' or info == 'true' then
info = '&dagger;'
else
info = ''
end
end
-- item ~= 'dagger'
else
ok, info = pcall(.expandTemplate, , { title = 'Template:Taxonomy/' .. taxon, args = {['machine code'] = item } })
if ok then
if info == '' then
-- try 'same as'
local sameAsTaxon = :expandTemplate{ title = 'Template:Taxonomy/' .. taxon, args = {['machine code'] = 'same as' } }
if sameAsTaxon ~= '' then
ok, info = pcall(.expandTemplate, , { title = 'Template:Taxonomy/' .. sameAsTaxon, args = {['machine code'] = item } })
end
end
end
end
if ok then
-- if item is 'link_text', trim info and check whether '(?)' needs to be added
if item == 'link_text' then
-- there is a newline at the end of linkText when taxonomy template has "|link = LINK_TARGET|LINK_TEXT"
info = mw.text.trim(info)
if string.sub(taxon, -2) == '/?' and not string.find(info, '?', 1, true) then
info = info .. '<span>&nbsp;(?)</span>'
end
end
else
info = 'Template:Taxonomy/' .. taxon .. '' --error indicator in code before conversion to Lua
end
return ok, info
end  
--============================ taxonLink ==================================
Returns a wikilink to a taxon, if required including '†' before it and
' (?)' after it, and optionally italicized or bolded without a wikilink.
Usage:  
=============================================================================
function p.taxonLink
local taxon = .args['taxon'] or ''
local extinct = string.lower(.args['extinct'] or '')
local bold = .args['bold'] or ''
local italic = .args['italic'] or ''
local abbreviated = .args['abbreviated'] or ''
local linkTarget = .args['link_target'] or ''
local linkText = .args['link_text'] or .args['plain_link_text'] or '' --temporarily allow alternative args
return l.makeLink(taxon, extinct, bold, italic, abbreviated, linkTarget, linkText)
end  
--= = = = = = = = = = = = = = getTaxonLink = = = = = = = = = = = = = = = =
Internal function to drive l.makeLink.  
function l.getTaxonLink(, taxon, rank, bold, italic, abbreviated, virus)
local ok, extinct = p.getTaxonInfoItem(, taxon, 'extinct')
if italic == '' then
italic = :expandTemplate{ title = 'Template:Is italic taxon', args = { rank, virus = virus } }
end
local ok, linkTarget = p.getTaxonInfoItem(, taxon, 'link_target')
local ok, linkText = p.getTaxonInfoItem(, taxon, 'link_text')
return l.makeLink(taxon, extinct, bold, italic, abbreviated, linkTarget, linkText)
end  
--= = = = = = = = = = = = = = makeLink = = = = = = = = = = = = = = = = = =
Actually make the link.  
function l.makeLink(taxon, extinct, bold, italic, abbreviated, linkTarget, linkText)
local dummy