Module:TaxonItalics: Difference between revisions

updated to tested sandbox version; adds "dab" parameter
en>Vivaporius
(Created page with "local p = {} --[[========================================================================= Italicize a taxon name appropriately by invoking italicizeTaxonName. The algorithm...")
 
en>Peter coxhead
(updated to tested sandbox version; adds "dab" parameter)
Line 1:
local p = {}
 
--[[=========================================================================
Italicize a taxon name appropriately by invoking italicizeTaxonName.
Line 15 ⟶ 13:
* Else just add italic markup to the outside of the name.
The module also:
* Ensures that the hybrid symbol, ×, and parentheses are not italicized, as
well as any string inside parentheses if dab is true.
* Has an option to abbreviate all parts of taxon names other than the last
to the first letter (e.g. "Pinus sylvestris var. sylvestris" becomes
Line 21 ⟶ 20:
* Has an option to wikilink the italicized name to the input name.
=============================================================================]]
 
local p = {}
local l = {} -- used to store purely local functions
 
--connecting terms in three part names (e.g. Pinus sylvestris var. sylvestris)
Line 51 ⟶ 53:
--subg.
subgenus = "subg.",
["subgen."] = "subg.",
["subg."] = "subg.",
subg = "subg.",
Line 80 ⟶ 83:
 
--[[=========================================================================
Main function to italicize a taxon name appropriately. For the purpose of the
parameters, see p.italicizeTaxonName().
=============================================================================]]
function p.main(frame)
Line 86 ⟶ 90:
local linked = frame.args['linked'] == 'yes'
local abbreviated = frame.args['abbreviated'] == 'yes'
local dab = frame.args['dab'] == 'yes'
return p.italicizeTaxonName(name, linked, abbreviated, dab)
end
 
--[[=========================================================================
Utility local function to abbreviate an input string to its first character
followed by ".".
Both "×" and an HTML entity at the start of the string are skipped over in
determining first character, as is an opening parenthesis, whichand causesan aopening ",
which cause a matching closing parenthesischaracter to be included.
=============================================================================]]
function pl.abbreviate(str)
local result = ""
local hasParentheses = false
local isQuoted = false
if mw.ustring.len(str) < 2 then
--single character strings are left unchanged
Line 107 ⟶ 113:
hasParentheses = true
result = "("
str = mw.ustring.sub(str,2,mw.ustring.len(str))
elseif mw.ustring.sub(str,1,1) == '"' then
isQuoted = true
result = '"'
str = mw.ustring.sub(str,2,mw.ustring.len(str))
end
Line 121 ⟶ 131:
end
--if there's anything left, reduce it to its first character plus ".",
--adding the closing parenthesis or quote if required
if str ~= "" then
result = result .. mw.ustring.sub(str,1,1) .. "."
if hasParentheses then result = result .. ")" end
elseif isQuoted then result = result .. '"'
end
end
end
Line 131 ⟶ 143:
 
--[[=========================================================================
The function which does the italicization. Parameters:
name (string) – the taxon name to be processed
linked (boolean) – should a wikilink be generated?
abbreviated (boolean) – should the first parts of the taxon name be
reduced to capital letters?
dab (boolean) – should any parenthesized part be treated as a disambiguation
term and left unitalicized?
=============================================================================]]
function p.italicizeTaxonName(name, linked, abbreviated, dab)
name = mw.text.trim(name)
local italMarker = "''"
-- begin by tidyingif the input name: trim;begins replacewith any'[', usethen ofassume theformatting is HTMLpresent
if mw.ustring.sub(name,1,1) == '[' then return name end
-- italic tags by Wikimedia markup; replace any alternatives to the hybrid
-- symbolotherwise begin by thereplacing symbolany itself;use preventof the hybrid symbol beingHTML treateditalic astags
-- italic tags by Wikimedia markup; replace any entity alternatives to the hybrid symbol
-- by the symbol itself; prevent the hybrid symbol being treated as
-- a 'word' by converting a following space to the HTML entity
local italMarker = "''"
name = string.gsub(mw.text.trim(name), "</?i>", italMarker)
name = string.gsub(string.gsub(name, "&#215;", "×"), "&times;", "×")
Line 146 ⟶ 167:
local result = name
if name ~= '' then
if string.sub(name,1,2) == "''"italMarker or string.sub(name,-2) == "''"italMarker then
-- do nothing if the name already has italic markers at the start or end
else
name = string.gsub(name, "''"italMarker, "") -- first remove any internal italics
local words = mw.text.split(name, " ", true)
if #words == 4 and cTerms3[words[3]] then
Line 156 ⟶ 177:
words[3] = '<span style="font-style:normal;">' .. cTerms3[words[3]] .. '</span>'
if abbreviated then
words[1] = pl.abbreviate(words[1])
words[2] = pl.abbreviate(words[2])
end
result = words[1] .. " " .. words[2] .. " " .. words[3] .. " " .. words[4]
Line 165 ⟶ 186:
words[2] = '<span style="font-style:normal;">' .. cTerms2[words[2]] .. '</span>'
if abbreviated then
words[1] = pl.abbreviate(words[1])
end
result = words[1] .. " " .. words[2] .. " " .. words[3]
Line 172 ⟶ 193:
if abbreviated then
if #words > 1 then
result = pl.abbreviate(words[1])
for i = 2, #words-1, 1 do
result = result .. " " .. pl.abbreviate(words[i])
end
result = result .. " " .. words[#words]
Line 185 ⟶ 206:
result = string.gsub(result, "×", '<span style="font-style:normal;">×</span>')
-- deal with any parentheses as they should not be italicized
-- addif outsidedab markupthen
result = string.gsub(string.gsub(result,"%(",'<span style="font-style:normal;">(</span>'),"%)",'<span style="font-style:normal;">)</span>')
result = string.gsub(string.gsub(result,"%(",'<span style="font-style:normal;">('),"%)",')</span>')
-- add outside markup
else
result = string.gsub(string.gsub(result,"%(",'<span style="font-style:normal;">(</span>'),"%)",'<span style="font-style:normal;">)</span>')
end
-- any question marks surrounded by spans can have the spans joined
result = string.gsub(result,'</span>%?<span style="font%-style:normal;">','?')
-- add outside markup
if linked then
if result ~= name then
Line 201 ⟶ 228:
end
 
--[[=========================================================================
Utility function used by other modules to check if a connecting term is
present in a name. The value of name is assumed to be plain text.
=============================================================================]]
function p.hasCT(frame)
return p.hasConnectingTerm(frame.args[1] or '')
end
function p.hasConnectingTerm(name)
local words = mw.text.split(name, " ", true)
if #words == 4 and cTerms3[words[3]] then return true
elseif #words == 3 and cTerms2[words[2]] then return true
else return false
end
end
return p
Anonymous user