Difference between revisions of "Module:TaxonItalics"

From blackwiki
Jump to navigation Jump to search
blackwiki>Peter coxhead
m (Peter coxhead moved page Module:Sandbox/Peter coxhead/TaxonItalics to Module:TaxonItalics without leaving a redirect: ready to be deployed)
blackwiki>Peter coxhead
(add code to link)
Line 51: Line 51:
 
function p.italicizeTaxonName(frame)
 
function p.italicizeTaxonName(frame)
 
local name = frame.args[1] or ''
 
local name = frame.args[1] or ''
 +
local linked = frame.args['linked'] == 'yes'
 
local italMarker = "''"
 
local italMarker = "''"
 
-- trim the name and replace any use of the HTML italic tags by Wikimedia markup
 
-- trim the name and replace any use of the HTML italic tags by Wikimedia markup
Line 61: Line 62:
 
name = string.gsub(name, "''", "") -- first remove internal italics
 
name = string.gsub(name, "''", "") -- first remove internal italics
 
local words = mw.text.split(name, " ", true)
 
local words = mw.text.split(name, " ", true)
 +
local deitalicized = false
 
if #words == 4 then
 
if #words == 4 then
 
-- test for the third word of a four word name being a connecting term
 
-- test for the third word of a four word name being a connecting term
Line 66: Line 68:
 
-- de-italicize the connecting term by adding internal italic markup
 
-- de-italicize the connecting term by adding internal italic markup
 
result = words[1] .. " " .. words[2] .. italMarker .. " " .. cTerms3[words[3]] .. italMarker .. " " .. words[4]
 
result = words[1] .. " " .. words[2] .. italMarker .. " " .. cTerms3[words[3]] .. italMarker .. " " .. words[4]
 +
deitalicized = true
 
end
 
end
 
elseif #words == 3 then
 
elseif #words == 3 then
Line 72: Line 75:
 
-- de-italicize the connecting term by adding internal italic markup
 
-- de-italicize the connecting term by adding internal italic markup
 
result = words[1] .. " " .. italMarker .. cTerms2[words[2]] .. italMarker .. " " .. words[3]
 
result = words[1] .. " " .. italMarker .. cTerms2[words[2]] .. italMarker .. " " .. words[3]
 +
deitalicized = true
 
end
 
end
 
else
 
else
Line 78: Line 82:
 
end
 
end
 
-- add outside markup
 
-- add outside markup
result = italMarker .. result .. italMarker
+
if linked then
 +
if deitalicized then
 +
result = "[[" .. name .. "|" .. italMarker .. result .. italMarker .. "]]"
 +
else
 +
result = italMarker .. "[[" .. name .. "]]" .. italMarker
 +
end
 +
else
 +
result = italMarker .. result .. italMarker
 +
end
 
end
 
end
 
end
 
end

Revision as of 16:50, 12 February 2018

Module:TaxonItalics Template:Toolbar

Purpose

The module supports the correct italicization of scientific names. Botanical (ICNafp) names may contain "connecting terms"; these must not be italicized. The hybrid symbol, ×, should also not be italicized. The module optionally wikilinks and abbreviates italicized names.

For non-virus taxa, italics are used at the rank of genus or below. The module does not decide whether a scientific name should be italicized. Use {{Is italic taxon}} for this purpose.

Usage

  • {{#invoke:TaxonItalics|main|TAXON_NAME}} – italicizes a taxon name
  • {{#invoke:TaxonItalics|main|TAXON_NAME|linked=yes}} – italicizes a taxon name, wikilinking the italicized output to the unchanged input
  • {{#invoke:TaxonItalics|main|TAXON_NAME|abbreviated=yes}} – italicizes a taxon name, abbreviating all but the last part to the first letter

It can also be used via {{Taxon italics}}.

Examples

Just italicized

Connecting terms
  • Pinus subg. Pinus → Script error: The function "main" does not exist.
  • P. subgenus Pinus → Script error: The function "main" does not exist.
  • P. subsect. Pinaster → Script error: The function "main" does not exist.
  • Acer tataricum subsp. ginnala → Script error: The function "main" does not exist.
  • Aster ericoides var. ericoides → Script error: The function "main" does not exist.
  • A. ericoides varietas ericoides → Script error: The function "main" does not exist.
  • A. e. subvar. ericoides → Script error: The function "main" does not exist.

Botanical names may contain only one infraspecific epithet; a string like "Fragaria vesca subsp. vesca f. semperflorens" is a classification, not a name, and is not handled by the module.

Hybrid symbols
  • Elaeagnus × submacrophylla → Script error: The function "main" does not exist.
  • ×Beallara → Script error: The function "main" does not exist.
  • × Beallara → Script error: The function "main" does not exist.
  • {{hybrid}}Beallara → Script error: The function "main" does not exist.

Linked

Using |linked=yes

  • Populus sect. Aigeiros → Script error: The function "main" does not exist.
  • Elaeagnus × submacrophylla → Script error: The function "main" does not exist.

Abbreviated

Using |abbreviated=yes

  • Populus sect. Aigeiros → Script error: The function "main" does not exist.
  • Acer tataricum subsp. ginnala → Script error: The function "main" does not exist.
  • [also linked] × Sorbaronia mitschurinii → Script error: The function "main" does not exist.
  • [also linked] Elaeagnus × submacrophylla → Script error: The function "main" does not exist.
  • Elaeagnus ×submacrophylla → Script error: The function "main" does not exist.
  • Elaeagnus {{hybrid}} submacrophylla → Script error: The function "main" does not exist.

For even more examples, see the testcases.



--[[=========================================================================
Italicize a taxon name appropriately by invoking italicizeTaxonName.
The algorithm used is:
* If the name has italic markup at the start or the end, do nothing.
* Else
  * Remove (internal) italic markup.
  * If the name is made up of four words and the third word is a
    botanical connecting term, de-italicize the connecting term and add italic
    markup to the outside of the name.
  * Else if the name is made up of three words and the second word is a
    botanical connecting term or a variant of "cf.", de-italicize the
    connecting term and add italic markup to the outside of the name. 
  * Else just add italic markup to the outside of the name.
=============================================================================]]

local p = {}

local cTerms3 = {
	subspecies = "subsp.",
	["subsp."] = "subsp.",
	subsp = "subsp.",
	["ssp."] = "subsp.",
	ssp = "subsp.",
	varietas = "var.",
	["var."] = "var.",
	var = "var.",
	subvarietas = "subvar.",
	["subvar."] = "subvar.",
	subvar = "subvar.",
	forma = "f.",
	["f."] = "f.",
	f = "f.",
	subforma = "subf.",
	["subf."] = "subf.",
	subf = "subf."
	}
local cTerms2 = {
	subgenus = "subg.",
	["subg."] = "subg.",
	subg = "subg.",
	section = "sect.",
	["sect."] = "sect.",
	["cf."] = "cf.",
	cf = "cf.",
	["c.f."] = "cf."
	}

--[[=========================================================================
Italicize a taxon name appropriately.
=============================================================================]]
function p.italicizeTaxonName(frame)
	local name = frame.args[1] or ''
	local linked = frame.args['linked'] == 'yes'
	local italMarker = "''"
	-- trim the name and replace any use of the HTML italic tags by Wikimedia markup
	name = string.gsub(string.gsub(mw.text.trim(name), "<i>", italMarker), "</i>", italMarker)
	local result = name
	if name ~= '' then
		if string.sub(name, 1, 2) == "''" or string.sub(name, -2) == "''" then
			-- do nothing if the name already has italic markers at the start or end
		else
			name = string.gsub(name, "''", "") -- first remove internal italics
			local words = mw.text.split(name, " ", true)
			local deitalicized = false
			if #words == 4 then
				-- test for the third word of a four word name being a connecting term
				if cTerms3[words[3]] then
					-- de-italicize the connecting term by adding internal italic markup
					result = words[1] .. " " .. words[2] .. italMarker .. " " .. cTerms3[words[3]] .. italMarker .. " " .. words[4]
					deitalicized = true
				end
			elseif #words == 3 then
				-- test for the second word of a three word name being a connecting term
				if cTerms2[words[2]] then
					-- de-italicize the connecting term by adding internal italic markup
					result = words[1] .. " " .. italMarker .. cTerms2[words[2]] .. italMarker .. " " .. words[3]
					deitalicized = true
				end
			else
				-- do nothing
				result = name
			end
			-- add outside markup
			if linked then
				if deitalicized then
					result = "[[" .. name .. "|" .. italMarker .. result .. italMarker .. "]]"
				else
					result = italMarker .. "[[" .. name .. "]]" .. italMarker
				end
			else
				result = italMarker .. result .. italMarker
			end
		end
	end
	return result
end

return p