Jump to content

Module:TaxonItalics

From Simple English Wikipedia, the free encyclopedia
Revision as of 12:28, 10 February 2018 by en>Peter coxhead

Documentation for this module may be created at Module:TaxonItalics/doc

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.",
	["cf."] = "cf.",
	cf = "cf.",
	["c.f."] = "cf."
	}

--[[=========================================================================
Italicize a taxon name appropriately.
* If the name is already italicized, do nothing.
* If the name is not made up of four words or the name is made up of four
  words and the third word is not a botanical connecting term, add italic
  markup to the outside of the name.
* If the name is made up of four words and the third word is a botanical
  connecting term, add italic markup so as not to italicize the connecting
  term.
=============================================================================]]
function p.italicizeTaxonName(frame)
	local name = frame.args[1] or ''
	-- replace any use of the HTML italic tags by Wikimedia markup
	name = string.gsub(string.gsub(name, "<i>", "''"), "</i>", "''")
	local result = name
	local italMarker = "''"
	if name ~= '' then
		if string.sub(name, 1, 2) == "''" then
			-- do nothing if the name already has italic markers at the start
		else
			local words = mw.text.split(name, " ", true)
			if #words == 4 then
				if string.find(name, "''", 1, true) then
					-- add outside italic markup to four word names already containing internal italic markup
					result = italMarker .. name .. italMarker
				else
					-- 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 = italMarker .. words[1] .. " " .. words[2] .. italMarker .. " " .. cTerms3[words[3]] .. " " .. italMarker .. words[4] .. italMarker
					else
						-- third word is not a connecting term, so add outside italic markup
						result = italMarker .. name .. italMarker
					end
				end
			elseif #words == 3 then
				if string.find(name, "''", 1, true) then
					-- add outside italic markup to three word names already containing internal italic markup
					result = italMarker .. name .. italMarker
				else
					-- 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 = italMarker .. words[1] .. " " .. cTerms2[words[2]] .. " " .. italMarker .. words[3] .. italMarker
					else
						-- second word is not a connecting term, so add outside italic markup
						result = italMarker .. name .. italMarker
					end
				end
			else
				-- add outside italic markup to names not made up of four words
				result = italMarker .. name .. italMarker
			end
		end
	end
	return result
end

return p