Difference between revisions of "Module:TaxonList"

From blackwiki
Jump to navigation Jump to search
blackwiki>Peter coxhead
blackwiki>Peter coxhead
Line 1: Line 1:
 +
--[[
 +
This module provides the core functionality to a set of templates used to
 +
display a list of taxon names, optionally italicized and/or wikilinked.
 +
]]
 +
 
local p = {}
 
local p = {}
local debugMsg = ''
 
 
function p.rawStr(str)
 
local result = ''
 
for i = 1, #str do
 
result = result .. string.sub(str,i,i) .. '_'
 
end
 
return result
 
end
 
  
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 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 = ''
 
local dagger = ''
if mw.ustring.sub(taxon,1,1) == '†' then
+
if mw.ustring.sub(taxonName,1,1) == '†' then
taxon = mw.ustring.sub(taxon,2,#taxon)
+
taxonName = mw.ustring.sub(taxonName,2,#taxonName)
 
dagger = '†'
 
dagger = '†'
 
else  
 
else  
if string.sub(taxon,1,8) == '&dagger;' then
+
if string.sub(taxonName,1,8) == '&dagger;' then
taxon = string.sub(taxon,9,#taxon)
+
taxonName = string.sub(taxonName,9,#taxonName)
 
dagger = '†'
 
dagger = '†'
 
else
 
else
if string.sub(taxon,1,5) == '<span' then
+
if string.sub(taxonName,1,5) == '<span' then
taxon = string.gsub(taxon, '^.*</span>', '', 1)
+
taxonName = string.gsub(taxonName, '^.*</span>', '', 1)
 
dagger = '†'
 
dagger = '†'
debugMsg = debugMsg .. '<p>stripped taxon=>' .. taxon .. '<</p>'
 
 
end
 
end
 
end
 
end
 
end
 
end
return taxon, dagger
+
return taxonName, dagger
 
end
 
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
 +
|incomplete = yes - to output "(incomplete)" at the end of the list
 +
}}
 +
The invoking template must supply an indefinite even number of arguments in
 +
the format |Name1|Author1|Name2|Author2| ... |NameN|AuthorN
 +
=============================================================================]]
 
function p.main(frame)
 
function p.main(frame)
 
local italic = frame.args['italic'] == 'yes'
 
local italic = frame.args['italic'] == 'yes'
Line 37: Line 55:
 
local taxonArgs = frame:getParent().args
 
local taxonArgs = frame:getParent().args
 
local result = ''
 
local result = ''
local num
 
 
-- iterate over unnamed variables
 
-- iterate over unnamed variables
local taxon
+
local taxonName
 
local dagger
 
local dagger
local first = true
+
local first = true -- is this the first of a taxon name, author pair?
for name, value in pairs(taxonArgs) do
+
for param, value in pairs(taxonArgs) do
if tonumber(name) then
+
if tonumber(param) then
 
if first then
 
if first then
taxon = value
+
taxonName = value
 +
-- if necessary separate any initial † from the taxon name
 
if linked or italic then
 
if linked or italic then
taxon, dagger = p.stripDagger(taxon)
+
taxonName, dagger = p.stripDagger(taxonName)
 
else
 
else
 
dagger = ''
 
dagger = ''
 
end
 
end
 
if linked then
 
if linked then
taxon = '[[' .. taxon .. ']]'
+
taxonName = '[[' .. taxonName .. ']]'
 
end
 
end
 
if italic then
 
if italic then
taxon = '<i>' .. taxon .. '</i>'
+
taxonName = '<i>' .. taxonName .. '</i>'
 
end
 
end
result = result .. '<li>' .. dagger .. taxon
+
result = result .. '<li>' .. dagger .. taxonName
 
else
 
else
 
result = result .. ' <small>' .. value .. '</small></li>'
 
result = result .. ' <small>' .. value .. '</small></li>'
Line 67: Line 85:
 
result = result .. '<small>(incomplete list)</small>'
 
result = result .. '<small>(incomplete list)</small>'
 
end
 
end
return '<ul style="plainlist">' .. result .. '</ul>' .. debugMsg
+
return '<ul style="plainlist">' .. result .. '</ul>'
 
end
 
end
  
 
return p
 
return p

Revision as of 14:29, 11 November 2017

Module:TaxonList Template:Toolbar

The purpose of this module is to provide support for a set of templates that produce a list of taxon names and their authorities, with the taxon names possibly italicized, wikilinked or emboldened. It allows these templates to have an indefinite number of arguments of the form taxonName1|authority1|taxonName2|authority2| ... |taxonNameN|authorityN. Templates supported are: {{Bold species list}}, {{Linked species list}}, {{Linked taxon list}}, {{Species list}} and {{Taxon list}}.

Usage

The main function in this module must only be invoked from within a template that itself is transcluded from a page that supplies taxon name/authority pairs as arguments, since main picks up these arguments from the parent frame.

{{#invoke:TaxonList|main|PARAMETERS}}

where PARAMETERS may be

  • italic – set to yes to italicize each taxon name
  • linked – set to yes to wikilink each taxon name
  • bold – set to yes to embolden each taxon name (also turns off wikilinking)
  • incomplete – set to yes to output "(incomplete)" at the end of the list

Examples

{{Taxon list}} contains {{#invoke:TaxonList|main|incomplete={{{incomplete|no}}}}}. Hence:

{{Taxon list |Asparagales|Bromhead |Iridales|Dumortier}}
Template:Taxon list

{{Linked species list}} contains {{#invoke:TaxonList|main|linked=yes|italic=yes|incomplete={{{incomplete|no}}}}}. Hence:

{{Linked species list |Poecilotheria fasciata|(Latreille, 1804) |Poecilotheria ornata|Pocock, 1899 |Poecilotheria rajaei|Nanayakkara et al., 2012 |incomplete=yes}}



--[[
This module provides the core functionality to a set of templates used to
display a list of taxon names, optionally italicized and/or wikilinked.
]]

local p = {}

--[[=========================================================================
Utility function to strip off any initial † present to mark the taxon as
extinct. The † must not be italicized 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(taxonName,1,1) == '†' then
		taxonName = mw.ustring.sub(taxonName,2,#taxonName)
		dagger = '†'
	else 
		if string.sub(taxonName,1,8) == '&dagger;' then
			taxonName = string.sub(taxonName,9,#taxonName)
			dagger = '†'
		else
			if string.sub(taxonName,1,5) == '<span' then
				taxonName = string.gsub(taxonName, '^.*</span>', '', 1)
				dagger = '†'
			end
		end
	end
	return taxonName, dagger
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
|incomplete = yes - to output "(incomplete)" at the end of the list
}}
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 linked = frame.args['linked'] == 'yes'
	local incomplete = frame.args['incomplete'] == 'yes'
	local taxonArgs = frame:getParent().args
	local result = ''
	-- iterate over unnamed variables
	local taxonName
	local dagger
	local first = true -- is this the first of a taxon name, author pair?
	for param, value in pairs(taxonArgs) do
		if tonumber(param) then
			if first then
				taxonName = value
				-- if necessary separate any initial † from the taxon name
				if linked or italic then
					taxonName, dagger = p.stripDagger(taxonName)
				else
					dagger = ''
				end
				if linked then
					taxonName = '[[' .. taxonName .. ']]'
				end
				if italic then
					taxonName = '<i>' .. taxonName .. '</i>'
				end
				result = result .. '<li>' .. dagger .. taxonName
			else
				result = result .. ' <small>' .. value .. '</small></li>'
			end
			first = not first
		end
	end
	if incomplete then
		result = result .. '<small>(incomplete list)</small>'
	end
	return '<ul style="plainlist">' .. result .. '</ul>'
end

return p