Jump to content

Module:TaxonList

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Peter coxhead (talk | contribs) at 14:40, 11 November 2017 (Peter coxhead moved page Module:Sandbox/Peter coxhead/TaxonList to Module:TaxonList without leaving a redirect: ready to be deployed, at least via sandbox versions of templates). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

--[[
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 †
* 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