Module:TaxonItalics: Difference between revisions
m
1 revision imported
en>Peter coxhead (updated to tested sandbox version; adds "dab" parameter) |
Vivaporius (talk | contribs) m (1 revision imported) |
||
Line 1:
local p = {}▼
--[[=========================================================================
Italicize a taxon name appropriately by invoking italicizeTaxonName.
Line 13 ⟶ 15:
* Else just add italic markup to the outside of the name.
The module also:
* Ensures that the hybrid symbol, ×, and parentheses are not italicized
* 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 20 ⟶ 21:
* Has an option to wikilink the italicized name to the input name.
=============================================================================]]
▲local p = {}
--connecting terms in three part names (e.g. Pinus sylvestris var. sylvestris)
Line 53 ⟶ 51:
--subg.
subgenus = "subg.",
["subg."] = "subg.",
subg = "subg.",
Line 83 ⟶ 80:
--[[=========================================================================
Main function to italicize a taxon name appropriately.
=============================================================================]]
function p.main(frame)
Line 90 ⟶ 86:
local linked = frame.args['linked'] == 'yes'
local abbreviated = frame.args['abbreviated'] == 'yes'
▲ return p.italicizeTaxonName(name, linked, abbreviated, dab)
end
--[[=========================================================================
Utility
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,
=============================================================================]]
function
local result = ""
local hasParentheses = false
if mw.ustring.len(str) < 2 then
--single character strings are left unchanged
Line 113 ⟶ 107:
hasParentheses = true
result = "("
str = mw.ustring.sub(str,2,mw.ustring.len(str))
end
Line 131 ⟶ 121:
end
--if there's anything left, reduce it to its first character plus ".",
--adding the closing parenthesis
if str ~= "" then
result = result .. mw.ustring.sub(str,1,1) .. "."
if hasParentheses then result = result .. ")" end
end
end
Line 143 ⟶ 131:
--[[=========================================================================
The function which does the italicization.
=============================================================================]]
function p.italicizeTaxonName(name, linked, abbreviated
-- 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 = "''"
-- begin by tidying the input name: trim; replace any use of the HTML
▲ -- symbol by the symbol itself; prevent the hybrid symbol being treated as
▲ -- a 'word' by converting a following space to the HTML entity
name = string.gsub(mw.text.trim(name), "</?i>", italMarker)
name = string.gsub(string.gsub(name, "×", "×"), "×", "×")
Line 167 ⟶ 146:
local result = name
if name ~= '' then
if string.sub(name,1,2) ==
-- do nothing if the name already has italic markers at the start or end
else
name = string.gsub(name,
local words = mw.text.split(name, " ", true)
if #words == 4 and cTerms3[words[3]] then
Line 177 ⟶ 156:
words[3] = '<span style="font-style:normal;">' .. cTerms3[words[3]] .. '</span>'
if abbreviated then
words[1] =
words[2] =
end
result = words[1] .. " " .. words[2] .. " " .. words[3] .. " " .. words[4]
Line 186 ⟶ 165:
words[2] = '<span style="font-style:normal;">' .. cTerms2[words[2]] .. '</span>'
if abbreviated then
words[1] =
end
result = words[1] .. " " .. words[2] .. " " .. words[3]
Line 193 ⟶ 172:
if abbreviated then
if #words > 1 then
result =
for i = 2, #words-1, 1 do
result = result .. " " ..
end
result = result .. " " .. words[#words]
Line 206 ⟶ 185:
result = string.gsub(result, "×", '<span style="font-style:normal;">×</span>')
-- deal with any parentheses as they should not be italicized
--
▲ result = string.gsub(string.gsub(result,"%(",'<span style="font-style:normal;">(</span>'),"%)",'<span style="font-style:normal;">)</span>')
if linked then
if result ~= name then
Line 228 ⟶ 201:
end
return p
|