Module:TaxonList: Difference between revisions

Content deleted Content added
No edit summary
Copy from sandbox per request on talk
 
(20 intermediate revisions by 4 users not shown)
Line 1:
--[[
local p = {}
This module provides the core functionality to a set of templates used to
local debugMsg = ''
display a list of taxon name/authority pairs, with the taxon names optionally
italicized, wikilinked and/or emboldened. Such lists are usually part of
taxoboxes.
]]
 
-- use a function from Module:TaxonItalics to italicize a taxon name
function p.rawStr(str)
local TaxonItalics = require("Module:TaxonItalics")
local result = ''
local IfPreview = require([[Module:If preview]])
for i = 1, #str do
result = result .. string.sub(str,i,i) .. '_'
end
return result
end
 
local p = {}
function p.stripDagger(taxon)
 
debugMsg = debugMsg .. '<p>taxon=' .. p.rawStr(taxon) .. '</p>'
--[[=========================================================================
Utility function to strip off any initial † present to mark the taxon as
extinct. The † must not be italicized, emboldened, or included in the
wikilinked text, so needs to be added back afterwards.
† is assumed to be present as one of:
* the unicode character †
* the HTML entity &dagger;
* the output of {{extinct}} – this will have been expanded before reaching this
module and is assumed to have the form '<span ... </span>'
The function returns two values: the taxon name with any † before it removed
and either '†' if it was present or the empty string if not.
=============================================================================]]
function p.stripDagger(taxonName)
local dagger = ''
if mw.ustring.sub(taxontaxonName,1,1) == '†' then
taxontaxonName = mw.ustring.sub(taxontaxonName,2,#taxontaxonName)
dagger = '†'
else
if string.sub(taxontaxonName,1,8) == '&dagger;' then
taxontaxonName = string.sub(taxontaxonName,9,#taxontaxonName)
dagger = '†'
else
-- did the taxon name originally have {{extinct}} before it?
if string.sub(taxon,1,5) == '<span' then
taxon =if (string.gsubsub(taxontaxonName,1,5) == '^.*</span>abbr') and mw.ustring.find(taxonName, '', 1) then
taxonName = string.gsub(taxonName, '^.*</abbr>', '', 1)
dagger = '†'
debugMsg = debugMsg .. '<p>stripped taxon=>' .. taxon .. '<</p>'
end
end
end
return taxontaxonName, dagger
end
 
--[[=========================================================================
Utility function to do the following:
 
1. Strip off any initial † present to mark the taxon as extinct. We outsource
to p.stripDagger() for this.
 
2. Strip off any double quotation marks present to mark the taxon as invalid.
The double-quotation marks, too, should not be formatted.
 
3. Strip off any Candidatus or Ca. to mark the taxon as Candidatus.
 
The function returns four values:
* the taxon name with all of the three modifiers removed
* either '†' if it was present or the empty string if not
* either a single dquote if it was present in a pair or the empty string if not
* either italicized "Candidatus " or "Ca. " if it was present or the empty string if not
 
The function can error in case of an unpaired quotation mark. In that case, a
IfPreview._warning() is mixed into the first return.
=============================================================================]]
function p.parseName(taxonName)
local name, dagger = p.stripDagger(taxonName)
local dquote = ''
if string.sub(name,1,1) == '"' then
name = string.sub(name,2)
dquote = '"'
if string.sub(name,#name) == '"' then
name = string.sub(name,1,#name-1)
else
name = '|' .. IfPreview._warning({'"' .. name .. ' has an unpaired double quote.'})
end
end
 
local candidatus = ''
if string.sub(name,1,11) == 'Candidatus ' then
name = string.sub(name,12)
candidatus = "''Candidatus'' "
elseif string.sub(name,1,4) == 'Ca. ' then
name = string.sub(name,5)
candidatus = "''Ca.'' "
end
 
return name, dagger, dquote, candidatus
end
 
--[[=========================================================================
The function returns a list of taxon names and authorities, appropriately
formatted.
Usage:
{{#invoke:TaxonList|main
|italic = yes - to italicize the taxon name
|linked = yes - to wikilink the taxon name
|bold = yes - to emboldent the taxon name
|incomplete = yes - to output "(incomplete)" at the end of the list
}}
The template that transcludes the invoking template must supply an indefinite
even number of arguments in the format
|Name1|Author1 |Name2|Author2| ... |NameN|AuthorN
=============================================================================]]
function p.main(frame)
local italic = frame.args['italic'] == 'yes'
local bold = frame.args['bold'] == 'yes'
local linked = frame.args['linked'] == 'yes'
if bold then linked = false end -- must not have bold and wikilinked
local abbreviated = frame.args['abbreviated'] == 'yes'
local incomplete = frame.args['incomplete'] == 'yes'
local taxonArgs = frame:getParent().args
local result = ''
local num
-- iterate over unnamed variables
local taxontaxonName
local dagger
local first = truedquote
local candidatus
for name, value in pairs(taxonArgs) do
local first = true -- is this the first of a taxon name/author pair?
if tonumber(name) then
for param, value in pairs(taxonArgs) do
if tonumber(param) then
if first then
taxontaxonName = mw.text.trim(value)
-- if linkednecessary separate orany italicinitial thenmodifier
taxontaxonName, dagger, dquote, candidatus = p.stripDaggerparseName(taxontaxonName)
if linked and not (italic and candidatus == '') then
else
daggertaxonName = '[[' .. taxonName .. ']]'
end
if linkeditalic and candidatus == '' then
taxonName = TaxonItalics.italicizeTaxonName(taxonName, linked, abbreviated)
taxon = '[[' .. taxon .. ']]'
end
taxonName = candidatus .. taxonName
if italic then
if bold then
taxon = '<i>' .. taxon .. '</i>'
taxonName = '<b>' .. taxonName .. '</b>'
end
result = result .. '<li>' .. dagger .. taxondquote .. taxonName .. dquote
else
result = result .. ' <small>' .. value .. '</small></li>'
Line 67 ⟶ 144:
result = result .. '<small>(incomplete list)</small>'
end
return '<ul styleclass="plainlisttaxonlist">' .. result .. '</ul>' .. debugMsg
end