Difference between revisions of "Module:TaxonItalics"
Jump to navigation
Jump to search
m (1 revision: Empetrum nigrum) |
m (1 revision: Cornus (two species)) |
||
(One intermediate revision by the same user not shown) | |||
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.", | |
− | + | ["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 = { | 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." | ||
+ | } | ||
--[[========================================================================= | --[[========================================================================= | ||
Line 50: | Line 59: | ||
=============================================================================]] | =============================================================================]] | ||
function p.main(frame) | function p.main(frame) | ||
− | + | local name = frame.args[1] or '' | |
− | + | local linked = frame.args['linked'] == 'yes' | |
− | + | return p.italicizeTaxonName(name, linked) | |
end | end | ||
function p.italicizeTaxonName(name, linked) | 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, "×", '<span style="font-style:normal;">×</span>') | |
− | + | result = string.gsub(result, "×", '<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 | end | ||
return p | return p |
Revision as of 18:01, 20 August 2018
--[[========================================================================= 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, "×", '×') result = string.gsub(result, "×", '×') result = string.gsub(result, "×", '×') -- add outside markup if linked then if result ~= name then result = "" .. italMarker .. result .. italMarker .. "" else result = italMarker .. "" .. name .. "" .. italMarker end else result = italMarker .. result .. italMarker end end end return result
end
return p