Module:TaxonItalics/sandbox: Difference between revisions
Artoria2e5 (talk | contribs) Ablate more function... Tag: Reverted |
Artoria2e5 (talk | contribs) |
||
Line 1: | Line 1: | ||
--[[========================================================================= |
|||
--[[ |
|||
Italicize a taxon name appropriately by invoking italicizeTaxonName. |
|||
This module provides the core functionality to a set of templates used to |
|||
The algorithm used is: |
|||
display a list of taxon name/authority pairs, with the taxon names optionally |
|||
* If the name has italic markup at the start or the end, do nothing. |
|||
italicized, wikilinked and/or emboldened. Such lists are usually part of |
|||
* Else |
|||
taxoboxes. |
|||
* 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 |
|||
-- use a function from Module:TaxonItalics to italicize a taxon name |
|||
markup to the outside of the name. |
|||
local TaxonItalics = require("Module:TaxonItalics") |
|||
* Else if the name is made up of three words and the second word is a |
|||
local IfPreview = require([[Module:If preview]]) |
|||
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. |
|||
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 |
|||
"P. s. var. sylvestris"). |
|||
* Has an option to wikilink the italicized name to the input name. |
|||
=============================================================================]] |
|||
local p = {} |
local p = {} |
||
local l = {} -- used to store purely local functions |
|||
--connecting terms in three part names (e.g. Pinus sylvestris var. sylvestris) |
|||
local cTerms3 = { |
|||
--subsp. |
|||
subspecies = "subsp.", |
|||
["subsp."] = "subsp.", |
|||
subsp = "subsp.", |
|||
["ssp."] = "subsp.", |
|||
ssp = "subsp.", |
|||
--var. |
|||
varietas = "var.", |
|||
["var."] = "var.", |
|||
var = "var.", |
|||
--subvar. |
|||
subvarietas = "subvar.", |
|||
["subvar."] = "subvar.", |
|||
subvar = "subvar.", |
|||
--f. |
|||
forma = "f.", |
|||
["f."] = "f.", |
|||
f = "f.", |
|||
--subf. |
|||
subforma = "subf.", |
|||
["subf."] = "subf.", |
|||
subf = "subf." |
|||
} |
|||
--connecting terms in two part names (e.g. Pinus sect. Pinus) |
|||
local cTerms2 = { |
|||
--subg. |
|||
subgenus = "subg.", |
|||
["subgen."] = "subg.", |
|||
["subg."] = "subg.", |
|||
subg = "subg.", |
|||
--supersect. |
|||
supersection = "supersect.", |
|||
["supersect."] = "supersect.", |
|||
supersect = "supersect.", |
|||
--sect. |
|||
section = "sect.", |
|||
["sect."] = "sect.", |
|||
sect = "sect.", |
|||
--subsect. |
|||
subsection = "subsect.", |
|||
["subsect."] = "subsect.", |
|||
subsect = "subsect.", |
|||
--ser. |
|||
series = "ser.", |
|||
["ser."] = "ser.", |
|||
ser = "ser.", |
|||
--subser. |
|||
subseries = "subser.", |
|||
["subser."] = "subser.", |
|||
subser = "subser.", |
|||
--cf. |
|||
cf = "cf.", |
|||
["cf."] = "cf.", |
|||
["c.f."] = "cf." |
|||
} |
|||
--[[========================================================================= |
--[[========================================================================= |
||
Main function to italicize a taxon name appropriately. For the purpose of the |
|||
parameters, see p.italicizeTaxonName(). |
|||
extinct. The † must not be italicized, emboldened, or included in the |
|||
wikilinked text, so needs to be added back afterwards. |
|||
† is assumed to be present as one of: |
|||
* the unicode character † |
|||
* the HTML entity † |
|||
* the output of {{extinct}} – this will have been expanded before reaching this |
|||
module and is assumed to have the form '<span ... </span>' |
|||
The function returns two values: the taxon name with any † before it removed |
|||
and either '†' if it was present or the empty string if not. |
|||
=============================================================================]] |
=============================================================================]] |
||
function p. |
function p.main(frame) |
||
local name = frame.args[1] or '' |
|||
local linked = frame.args['linked'] == 'yes' |
|||
if mw.ustring.sub(taxonName,1,1) == '†' then |
|||
local abbreviated = frame.args['abbreviated'] == 'yes' |
|||
taxonName = mw.ustring.sub(taxonName,2,#taxonName) |
|||
local dab = frame.args['dab'] == 'yes' |
|||
dagger = '†' |
|||
return p.italicizeTaxonName(name, linked, abbreviated, dab) |
|||
else |
|||
end |
|||
if string.sub(taxonName,1,8) == '†' then |
|||
taxonName = string.sub(taxonName,9,#taxonName) |
|||
--[[========================================================================= |
|||
dagger = '†' |
|||
Utility local function to abbreviate an input string to its first character |
|||
else |
|||
followed by ".". |
|||
-- did the taxon name originally have {{extinct}} before it? |
|||
Both "×" and an HTML entity at the start of the string are skipped over in |
|||
if (string.sub(taxonName,1,5) == '<abbr') and mw.ustring.find(taxonName, '†') then |
|||
determining first character, as is an opening parenthesis and an opening ", |
|||
taxonName = string.gsub(taxonName, '^.*</abbr>', '', 1) |
|||
which cause a matching closing character to be included. |
|||
dagger = '†' |
|||
=============================================================================]] |
|||
function l.abbreviate(str) |
|||
local result = "" |
|||
local hasParentheses = false |
|||
local isQuoted = false |
|||
if mw.ustring.len(str) < 2 then |
|||
--single character strings are left unchanged |
|||
result = str |
|||
else |
|||
--skip over an opening parenthesis that could be present at the start of the string |
|||
if mw.ustring.sub(str,1,1) == "(" then |
|||
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 |
|||
--skip over a hybrid symbol that could be present at the start of the string |
|||
if mw.ustring.sub(str,1,1) == "×" then |
|||
result = "×" |
|||
str = mw.ustring.sub(str,2,mw.ustring.len(str)) |
|||
end |
|||
--skip over an HTML entity that could be present at the start of the string |
|||
if mw.ustring.sub(str,1,1) == "&" then |
|||
local i,dummy = mw.ustring.find(str,";",2,plain) |
|||
result = result .. mw.ustring.sub(str,1,i) |
|||
str = mw.ustring.sub(str,i+1,mw.ustring.len(str)) |
|||
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 .. ")" |
|||
elseif isQuoted then result = result .. '"' |
|||
end |
end |
||
end |
end |
||
end |
end |
||
return |
return result |
||
end |
end |
||
--[[========================================================================= |
--[[========================================================================= |
||
The function which does the italicization. Parameters: |
|||
name (string) – the taxon name to be processed |
|||
linked (boolean) – should a wikilink be generated? |
|||
1. Strip off any initial † present to mark the taxon as extinct. We outsource |
|||
abbreviated (boolean) – should the first parts of the taxon name be |
|||
to p.stripDagger() for this. |
|||
reduced to capital letters? |
|||
dab (boolean) – should any parenthesized part be treated as a disambiguation |
|||
2. Strip off any double quotation marks present to mark the taxon as invalid. |
|||
term and left unitalicized? |
|||
The double-quotation marks, too, should not be formatted. |
|||
3. Strip off any Candidatus or Ca. to mark the taxon as Candidatus. |
|||
The function returns four values: |
|||
* the taxon name with all of the three modifiers removed |
|||
* either '†' if it was present or the empty string if not |
|||
* either a single dquote if it was present in a pair or the empty string if not |
|||
* either italicized "Candidatus " or "Ca. " if it was present or the empty string if not |
|||
The function can error in case of an unpaired quotation mark. In that case, a |
|||
IfPreview._warning() is mixed into the first return. |
|||
=============================================================================]] |
=============================================================================]] |
||
function p. |
function p.italicizeTaxonName(name, linked, abbreviated, dab) |
||
name = mw.text.trim(name) |
|||
local name, dagger = p.stripDagger(taxonName) |
|||
-- if the name begins with '[', then assume formatting is present |
|||
local dquote = '' |
|||
if mw.ustring.sub(name,1,1) == '[' then return name end |
|||
return name, dagger, '', '' |
|||
-- otherwise begin by replacing any use of the HTML italic tags |
|||
--[[ |
|||
-- by Wikimedia markup; replace any entity alternatives to the hybrid symbol |
|||
if string.sub(name,1,1) == '"' then |
|||
-- by the symbol itself; prevent the hybrid symbol being treated as |
|||
name = string.sub(name,2) |
|||
-- a 'word' by converting a following space to the HTML entity |
|||
dquote = '"' |
|||
local italMarker = "''" |
|||
if string.sub(name,1,1) == '"' then |
|||
name = string.gsub(mw.text.trim(name), "</?i>", italMarker) |
|||
name = string.gsub(string.gsub(name, "×", "×"), "×", "×") |
|||
else |
|||
name = string.gsub(name, "</?span.->", "") -- remove any span markup |
|||
name = IfPreview._warning('Unmatched double quotation mark in taxon name: ' .. name) |
|||
name = string.gsub(name, "× ", "× ") |
|||
end |
|||
-- now italicize and abbreviate if required |
|||
end |
|||
local result = name |
|||
if name ~= '' then |
|||
if string.sub(name,1,2) == italMarker or string.sub(name,-2) == italMarker then |
|||
local candidatus = '' |
|||
-- do nothing if the name already has italic markers at the start or end |
|||
if string.sub(name,1,11) == 'Candidatus ' then |
|||
else |
|||
name = string.sub(name,12) |
|||
name = string.gsub(name, italMarker, "") -- first remove any internal italics |
|||
candidatus = "''Candidatus'' " |
|||
local words = mw.text.split(name, " ", true) |
|||
elseif string.sub(name,1,4) == 'Ca. ' then |
|||
if #words == 4 and cTerms3[words[3]] then |
|||
name = string.sub(name,5) |
|||
-- the third word of a four word name is a connecting term |
|||
candidatus = "''Ca.'' " |
|||
-- ensure the connecting term isn't italicized |
|||
end |
|||
words[3] = '<span style="font-style:normal;">' .. cTerms3[words[3]] .. '</span>' |
|||
if abbreviated then |
|||
return name, dagger, dquote, candidatus]] |
|||
words[1] = l.abbreviate(words[1]) |
|||
words[2] = l.abbreviate(words[2]) |
|||
end |
|||
result = words[1] .. " " .. words[2] .. " " .. words[3] .. " " .. words[4] |
|||
elseif #words == 3 and cTerms2[words[2]] then |
|||
-- the second word of a three word name is a connecting term |
|||
-- ensure the connecting term isn't italicized |
|||
words[2] = '<span style="font-style:normal;">' .. cTerms2[words[2]] .. '</span>' |
|||
if abbreviated then |
|||
words[1] = l.abbreviate(words[1]) |
|||
end |
|||
result = words[1] .. " " .. words[2] .. " " .. words[3] |
|||
elseif abbreviated then -- not a name as above; only deal with abbreviation |
|||
if #words > 1 then |
|||
result = l.abbreviate(words[1]) |
|||
for i = 2, #words-1, 1 do |
|||
result = result .. " " .. l.abbreviate(words[i]) |
|||
end |
|||
result = result .. " " .. words[#words] |
|||
end |
|||
else |
|||
result = name |
|||
end |
|||
-- deal with any hybrid symbol as it should not be italicized |
|||
result = string.gsub(result, "×", '<span style="font-style:normal;">×</span>') |
|||
-- deal with any parentheses as they should not be italicized |
|||
if dab then |
|||
result = string.gsub(string.gsub(result,"%(",'<span style="font-style:normal;">('),"%)",')</span>') |
|||
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 |
|||
result = "[[" .. name .. "|" .. italMarker .. result .. italMarker .. "]]" |
|||
else |
|||
result = italMarker .. "[[" .. name .. "]]" .. italMarker |
|||
end |
|||
else |
|||
result = italMarker .. result .. italMarker |
|||
end |
|||
end |
|||
end |
|||
return result |
|||
end |
end |
||
--[[========================================================================= |
--[[========================================================================= |
||
Utility function used by other modules to check if a connecting term is |
|||
The function returns a list of taxon names and authorities, appropriately |
|||
present in a name. The value of name is assumed to be plain text. |
|||
formatted. |
|||
Usage: |
|||
{{#invoke:TaxonList|main |
|||
|italic = yes - to italicize the taxon name |
|||
|linked = yes - to wikilink the taxon name |
|||
|bold = yes - to emboldent the taxon name |
|||
|incomplete = yes - to output "(incomplete)" at the end of the list |
|||
}} |
|||
The template that transcludes the invoking template must supply an indefinite |
|||
even number of arguments in the format |
|||
|Name1|Author1 |Name2|Author2| ... |NameN|AuthorN |
|||
=============================================================================]] |
=============================================================================]] |
||
function p. |
function p.hasCT(frame) |
||
return p.hasConnectingTerm(frame.args[1] or '') |
|||
local bold = frame.args['bold'] == 'yes' |
|||
local linked = frame.args['linked'] == 'yes' |
|||
if bold then linked = false end -- must not have bold and wikilinked |
|||
local abbreviated = frame.args['abbreviated'] == 'yes' |
|||
local incomplete = frame.args['incomplete'] == 'yes' |
|||
local taxonArgs = frame:getParent().args |
|||
local result = '' |
|||
-- iterate over unnamed variables |
|||
local taxonName |
|||
local dagger |
|||
local dquote |
|||
local candidatus |
|||
local first = true -- is this the first of a taxon name/author pair? |
|||
for param, value in pairs(taxonArgs) do |
|||
if tonumber(param) then |
|||
if first then |
|||
taxonName = mw.text.trim(value) |
|||
-- if necessary separate any initial modifier |
|||
taxonName, dagger, dquote, candidatus = p.parseName(taxonName) |
|||
if linked and not italic then |
|||
taxonName = '[[' .. taxonName .. ']]' |
|||
end |
|||
if italic and candidatus == '' then |
|||
taxonName = TaxonItalics.italicizeTaxonName(taxonName, linked, abbreviated) |
|||
end |
|||
if bold then |
|||
taxonName = '<b>' .. taxonName .. '</b>' |
|||
end |
|||
result = result .. '<li>' .. dagger .. dquote .. candidatus .. taxonName .. dquote |
|||
else |
|||
result = result .. ' <small>' .. value .. '</small></li>' |
|||
end |
|||
first = not first |
|||
end |
|||
end |
|||
if incomplete then |
|||
result = result .. '<small>(incomplete list)</small>' |
|||
end |
|||
return '<ul class="taxonlist">' .. result .. '</ul>' |
|||
end |
end |
||
function p.hasConnectingTerm(name) |
|||
local words = mw.text.split(name, " ", true) |
|||
return (#words == 4 and cTerms3[words[3]]) |
|||
or (#words == 3 and cTerms2[words[2]]) |
|||
end |
|||
return p |
return p |
Latest revision as of 10:40, 21 May 2025
![]() | This is the module sandbox page for Module:TaxonItalics (diff). See also the companion subpage for test cases (run). |
Module:TaxonItalics (talk · · hist · links · doc · subpages · sandbox · testcases)
![]() | This Lua module is used on approximately 603,000 pages, or roughly 1% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
Purpose
[edit]The module is primarily intended for use by the automated taxobox system. It 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
[edit]- {{#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
- {{#invoke:TaxonItalics|main|TAXON_NAME|dab=yes}} – italicizes a taxon name, treating any parenthesized part as a disambiguation term, and not italicizing it
The parameters can be combined. It can also be used via {{Taxon italics}}.
Examples
[edit]Just italicized
[edit]- 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
- × Beallara → × Beallara
- {{hybrid}}Beallara → ×Beallara
Linked
[edit]Using |linked=yes
- Populus sect. Aigeiros → Populus sect. Aigeiros
- Elaeagnus × submacrophylla → Elaeagnus × submacrophylla
Abbreviated
[edit]Using |abbreviated=yes
- Populus sect. Aigeiros → P. sect. Aigeiros
- Acer tataricum subsp. ginnala → A. t. subsp. ginnala
- [also linked] × Sorbaronia fallax → × S. fallax
- [also linked] Elaeagnus × submacrophylla → E. × submacrophylla
- Elaeagnus ×submacrophylla → E. ×submacrophylla
- Elaeagnus {{hybrid}} submacrophylla → E. × submacrophylla
Disambiguation terms
[edit]By default, a parenthesized part of a taxon name is assumed to be a subgenus name, and is italicized:
- Varanus (Hapturosaurus) → Varanus (Hapturosaurus)
- Caia (plant) → Caia (plant) – wrong
To treat a parenthesized part as a disambiguation term, use |dab=yes
- Caia (plant) → Caia (plant)
- (also linked) Caia (plant) → Caia (plant)
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.
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
"P. s. var. sylvestris").
* 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)
local cTerms3 = {
--subsp.
subspecies = "subsp.",
["subsp."] = "subsp.",
subsp = "subsp.",
["ssp."] = "subsp.",
ssp = "subsp.",
--var.
varietas = "var.",
["var."] = "var.",
var = "var.",
--subvar.
subvarietas = "subvar.",
["subvar."] = "subvar.",
subvar = "subvar.",
--f.
forma = "f.",
["f."] = "f.",
f = "f.",
--subf.
subforma = "subf.",
["subf."] = "subf.",
subf = "subf."
}
--connecting terms in two part names (e.g. Pinus sect. Pinus)
local cTerms2 = {
--subg.
subgenus = "subg.",
["subgen."] = "subg.",
["subg."] = "subg.",
subg = "subg.",
--supersect.
supersection = "supersect.",
["supersect."] = "supersect.",
supersect = "supersect.",
--sect.
section = "sect.",
["sect."] = "sect.",
sect = "sect.",
--subsect.
subsection = "subsect.",
["subsect."] = "subsect.",
subsect = "subsect.",
--ser.
series = "ser.",
["ser."] = "ser.",
ser = "ser.",
--subser.
subseries = "subser.",
["subser."] = "subser.",
subser = "subser.",
--cf.
cf = "cf.",
["cf."] = "cf.",
["c.f."] = "cf."
}
--[[=========================================================================
Main function to italicize a taxon name appropriately. For the purpose of the
parameters, see p.italicizeTaxonName().
=============================================================================]]
function p.main(frame)
local name = frame.args[1] or ''
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 and an opening ",
which cause a matching closing character to be included.
=============================================================================]]
function l.abbreviate(str)
local result = ""
local hasParentheses = false
local isQuoted = false
if mw.ustring.len(str) < 2 then
--single character strings are left unchanged
result = str
else
--skip over an opening parenthesis that could be present at the start of the string
if mw.ustring.sub(str,1,1) == "(" then
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
--skip over a hybrid symbol that could be present at the start of the string
if mw.ustring.sub(str,1,1) == "×" then
result = "×"
str = mw.ustring.sub(str,2,mw.ustring.len(str))
end
--skip over an HTML entity that could be present at the start of the string
if mw.ustring.sub(str,1,1) == "&" then
local i,dummy = mw.ustring.find(str,";",2,plain)
result = result .. mw.ustring.sub(str,1,i)
str = mw.ustring.sub(str,i+1,mw.ustring.len(str))
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 .. ")"
elseif isQuoted then result = result .. '"'
end
end
end
return result
end
--[[=========================================================================
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)
-- if the name begins with '[', then assume formatting is present
if mw.ustring.sub(name,1,1) == '[' then return name end
-- otherwise begin by replacing any use of the HTML 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, "×", "×"), "×", "×")
name = string.gsub(name, "</?span.->", "") -- remove any span markup
name = string.gsub(name, "× ", "× ")
-- now italicize and abbreviate if required
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
-- the third word of a four word name is a connecting term
-- ensure the connecting term isn't italicized
words[3] = '<span style="font-style:normal;">' .. cTerms3[words[3]] .. '</span>'
if abbreviated then
words[1] = l.abbreviate(words[1])
words[2] = l.abbreviate(words[2])
end
result = words[1] .. " " .. words[2] .. " " .. words[3] .. " " .. words[4]
elseif #words == 3 and cTerms2[words[2]] then
-- the second word of a three word name is a connecting term
-- ensure the connecting term isn't italicized
words[2] = '<span style="font-style:normal;">' .. cTerms2[words[2]] .. '</span>'
if abbreviated then
words[1] = l.abbreviate(words[1])
end
result = words[1] .. " " .. words[2] .. " " .. words[3]
elseif abbreviated then -- not a name as above; only deal with abbreviation
if #words > 1 then
result = l.abbreviate(words[1])
for i = 2, #words-1, 1 do
result = result .. " " .. l.abbreviate(words[i])
end
result = result .. " " .. words[#words]
end
else
result = name
end
-- deal with any hybrid symbol as it should not be italicized
result = string.gsub(result, "×", '<span style="font-style:normal;">×</span>')
-- deal with any parentheses as they should not be italicized
if dab then
result = string.gsub(string.gsub(result,"%(",'<span style="font-style:normal;">('),"%)",')</span>')
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
result = "[[" .. name .. "|" .. italMarker .. result .. italMarker .. "]]"
else
result = italMarker .. "[[" .. name .. "]]" .. italMarker
end
else
result = italMarker .. result .. italMarker
end
end
end
return result
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)
return (#words == 4 and cTerms3[words[3]])
or (#words == 3 and cTerms2[words[2]])
end
return p