Jump to content

Module:TaxonItalics

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Peter coxhead (talk | contribs) at 10:01, 8 February 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

--[[=========================================================================
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
				-- add outside italic markup to names not made up of four words
				result = italMarker .. name .. italMarker
			elseif 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 words[3] == "subsp." or words[3] == "var." or words[3] == "subvar." or words[3] == "f." or words[3] == "subf." then
					-- de-italicize the connecting term by adding internal italic markup
					result = italMarker .. words[1] .. " " .. words[2] .. italMarker .. " " .. 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
		end
	end
	return result
end

return p