Difference between revisions of "Module:TaxonItalics"

From blackwiki
Jump to navigation Jump to search
blackwiki>Peter coxhead
(Undid revision 855480164 by Peter coxhead (talk) doesn't do what I wanted; try in sandbox version)
blackwiki>Peter coxhead
(updated to tested sandbox version)
Line 10: Line 10:
 
   * Else if the name is made up of three words and the second word is a
 
   * 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
 
     botanical connecting term or a variant of "cf.", de-italicize the
     connecting term and add italic markup to the outside of the name.  
+
     connecting term and add italic markup to the outside of the name.
 
   * Else just add italic markup to the outside of the name.
 
   * Else just add italic markup to the outside of the name.
 
=============================================================================]]
 
=============================================================================]]
Line 17: Line 17:
  
 
local cTerms3 = {
 
local cTerms3 = {
subspecies = "subsp.",
+
    subspecies = "subsp.",
["subsp."] = "subsp.",
+
    ["subsp."] = "subsp.",
subsp = "subsp.",
+
    subsp = "subsp.",
["ssp."] = "subsp.",
+
    ["ssp."] = "subsp.",
ssp = "subsp.",
+
    ssp = "subsp.",
varietas = "var.",
+
    varietas = "var.",
["var."] = "var.",
+
    ["var."] = "var.",
var = "var.",
+
    var = "var.",
subvarietas = "subvar.",
+
    subvarietas = "subvar.",
["subvar."] = "subvar.",
+
    ["subvar."] = "subvar.",
subvar = "subvar.",
+
    subvar = "subvar.",
forma = "f.",
+
    forma = "f.",
["f."] = "f.",
+
    ["f."] = "f.",
f = "f.",
+
    f = "f.",
subforma = "subf.",
+
    subforma = "subf.",
["subf."] = "subf.",
+
    ["subf."] = "subf.",
subf = "subf."
+
    subf = "subf."
}
+
    }
 
local cTerms2 = {
 
local cTerms2 = {
subgenus = "subg.",
+
    subgenus = "subg.",
["subg."] = "subg.",
+
    ["subg."] = "subg.",
subg = "subg.",
+
    subg = "subg.",
section = "sect.",
+
    section = "sect.",
["sect."] = "sect.",
+
    ["sect."] = "sect.",
subsection = "subsect.",
+
    subsection = "subsect.",
subsect = "subsect.",
+
    subsect = "subsect.",
["subsect."] = "subsect.",
+
    ["subsect."] = "subsect.",
series = "ser.",
+
    series = "ser.",
ser = "ser.",
+
    ser = "ser.",
["ser."] = "ser.",
+
    ["ser."] = "ser.",
subseries = "subser.",
+
    subseries = "subser.",
subser = "subser.",
+
    subser = "subser.",
["subser."] = "subser.",
+
    ["subser."] = "subser.",
["cf."] = "cf.",
+
    ["cf."] = "cf.",
cf = "cf.",
+
    cf = "cf.",
["c.f."] = "cf."
+
    ["c.f."] = "cf."
}
+
    }
  
 
--[[=========================================================================
 
--[[=========================================================================
Line 59: Line 59:
 
=============================================================================]]
 
=============================================================================]]
 
function p.main(frame)
 
function p.main(frame)
local name = frame.args[1] or ''
+
    local name = frame.args[1] or ''
local linked = frame.args['linked'] == 'yes'
+
    local linked = frame.args['linked'] == 'yes'
return p.italicizeTaxonName(name, linked)
+
    return p.italicizeTaxonName(name, linked)
 
end
 
end
  
 
function p.italicizeTaxonName(name, linked)
 
function p.italicizeTaxonName(name, linked)
local italMarker = "''"
+
    local italMarker = "''"
local hybridSymbol = "×"
+
    -- 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
+
    name = string.gsub(mw.text.trim(name), "</?i>", italMarker)
name = string.gsub(mw.text.trim(name), "</?i>", italMarker)
+
    local result = name
local result = name
+
    if name ~= '' then
if name ~= '' then
+
        if string.sub(name, 1, 2) == "''" or string.sub(name, -2) == "''" 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
-- do nothing if the name already has italic markers at the start or end
+
        else
else
+
            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)
+
            if #words == 4 then
if words[2] == hybridSymbol then
+
                -- test for the third word of a four word name being a connecting term
words[1] = words[1] .. italMarker .. " " .. hybridSymbol
+
                if cTerms3[words[3]] then
words[3] = " " .. italMarker .. words[3]
+
                    -- de-italicize the connecting term by adding internal italic markup
table.remove(words, 2)
+
                    result = words[1] .. " " .. words[2] .. italMarker .. " " .. cTerms3[words[3]] .. italMarker .. " " .. words[4]
-- hybridDeitalicized = true
+
                end
end
+
            elseif #words == 3 then
local deitalicized = false
+
                -- test for the second word of a three word name being a connecting term
if #words == 4 then
+
                if cTerms2[words[2]] then
-- test for the third word of a four word name being a connecting term
+
                    -- de-italicize the connecting term by adding internal italic markup
if cTerms3[words[3]] then
+
                    result = words[1] .. " " .. italMarker .. cTerms2[words[2]] .. italMarker .. " " .. words[3]
-- de-italicize the connecting term by adding internal italic markup
+
                end
result = words[1] .. " " .. words[2] .. italMarker .. " " .. cTerms3[words[3]] .. " " .. italMarker .. words[4]
+
            else
deitalicized = true
+
                -- do nothing
end
+
                result = name
elseif #words == 3 then
+
            end
-- test for the second word of a three word name being a connecting term
+
            -- deal with any hybrid symbol as it should not be italicized
if cTerms2[words[2]] then
+
            result = string.gsub(result, "×", '<span style="font-style:normal;">×</span>')
-- de-italicize the connecting term by adding internal italic markup
+
            result = string.gsub(result, "&times;", '<span style="font-style:normal;">×</span>')
result = words[1] .. italMarker .. " " .. cTerms2[words[2]] .. " " .. italMarker .. words[3]
+
            result = string.gsub(result, "&#215;", '<span style="font-style:normal;">×</span>')
deitalicized = true
+
            -- add outside markup
end
+
            if linked then
else
+
                if result ~= name then
-- do nothing except un-italicize hybrid symbol
+
                    result = "[[" .. name .. "|" .. italMarker .. result .. italMarker .. "]]"
result = name:gsub(" " .. hybridSymbol .. " ", "''%0''")
+
                else
end
+
                    result = italMarker .. "[[" .. name .. "]]" .. italMarker
-- add outside markup
+
                end
if linked then
+
            else
if deitalicized then
+
                result = italMarker .. result .. italMarker
result = "[[" .. name .. "|" .. italMarker .. result .. italMarker .. "]]"
+
            end
else
+
        end
result = italMarker .. "[[" .. name .. "]]" .. italMarker
+
    end
end
+
    return result
else
 
result = italMarker .. result .. italMarker
 
end
 
end
 
end
 
return result
 
 
end
 
end
  
 
return p
 
return p

Revision as of 17:53, 18 August 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 → Pinus subg. Pinus
  • P. subgenus Pinus → P. subg. Pinus
  • P. subsect. Pinaster → P. subsect. Pinaster
  • Acer tataricum subsp. ginnala → Acer tataricum subsp. ginnala
  • Aster ericoides var. ericoides → Aster ericoides var. ericoides
  • A. ericoides varietas ericoides → A. ericoides var. ericoides
  • A. e. subvar. ericoides → A. e. subvar. ericoides

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 → Elaeagnus × submacrophylla
  • ×Beallara → ×Beallara
  • &times; Beallara → × Beallara
  • {{hybrid}}Beallara → ×Beallara

Linked

Using |linked=yes

Abbreviated

Using |abbreviated=yes

  • Populus sect. Aigeiros → Populus sect. Aigeiros
  • Acer tataricum subsp. ginnala → Acer tataricum subsp. ginnala
  • [also linked] × Sorbaronia mitschurinii → × Sorbaronia mitschurinii
  • [also linked] Elaeagnus × submacrophylla → Elaeagnus × submacrophylla
  • Elaeagnus ×submacrophylla → Elaeagnus ×submacrophylla
  • Elaeagnus {{hybrid}} submacrophylla → Elaeagnus × submacrophylla

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.",
    subsection = "subsect.",
    subsect = "subsect.",
    ["subsect."] = "subsect.",
    series = "ser.",
    ser = "ser.",
    ["ser."] = "ser.",
    subseries = "subser.",
    subser = "subser.",
    ["subser."] = "subser.",
    ["cf."] = "cf.",
    cf = "cf.",
    ["c.f."] = "cf."
    }

--[[=========================================================================
Italicize a taxon name appropriately.
=============================================================================]]
function p.main(frame)
    local name = frame.args[1] or ''
    local linked = frame.args['linked'] == 'yes'
    return p.italicizeTaxonName(name, linked)
end

function p.italicizeTaxonName(name, linked)
    local italMarker = "''"
    -- trim the name and replace any use of the HTML italic tags by Wikimedia markup
    name = string.gsub(mw.text.trim(name), "</?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)
            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]
                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]
                end
            else
                -- do nothing
                result = name
            end
            -- deal with any hybrid symbol as it should not be italicized
            result = string.gsub(result, "×", '<span style="font-style:normal;">×</span>')
            result = string.gsub(result, "&times;", '<span style="font-style:normal;">×</span>')
            result = string.gsub(result, "&#215;", '<span style="font-style:normal;">×</span>')
            -- add outside markup
            if linked then
                if result ~= name 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