Jump to content

Module:eFloras

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Erutuon (talk | contribs) at 17:56, 11 July 2018 (separate out italicization part). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

require('Module:No globals')

local p = {}

local data -- Load [[Module:eFloras/data]] if needed and assign to this variable.

local function getResource(floraID)
	data = data or mw.loadData("Module:eFloras/data")
	return data.resources[floraID]
end

function p.resource(frame)
	local floraID = string.match(frame.args[1], "%d+")
	if floraID == nil then
		return "<span style=\"color: red;\">Please provide a resource number (<code>flora_id</code>). See the list of supported resource numbers at [[Module:eFloras/doc]]</span>"
	else
		local flora = getResource(floraID)
		if flora == nil then
			return "<span style=\"color: red;\">The resource number (<code>flora_id</code>) <code>" .. floraID .. "</code> is not recognized. See the list of supported resource numbers at [[Module:eFloras/doc]]</span>[[Category:Pages using eFloras template with unsupported parameter values]]"
		else
			return flora
		end
	end
end

function p.volume(frame)
	local floraID = string.match(frame.args[1], "%d+")
	local family = frame.args[2] or frame.args.family
	data = data or mw.loadData("Module:eFloras/data")
	local flora = data.volumeTable[floraID]
	if flora == nil then
		return ""
	else
		local volume = flora[family]
		if volume == "error" then
			return "19&ndash;21 [[Category:Pages using eFloras template with unsupported parameter values]]"
		elseif volume == nil then
			return ""
		else
			return volume
		end
	end
end

-- Italicize if name requires it.
function p.italicize(name)
	local orig = name
	name = string.gsub(name, "^%s*(.*)%s*$", "%1")
	
	local count
	name, count = string.gsub(name, "\'\'\'?", "")
	
	if count > 0 then
		-- A tracking method used on Wiktionary: [[wikt:Module:debug]].
		-- To see the results:
		-- [[Special:WhatLinksHere/Template:tracking/eFloras/italics or bolding]]
		local frame = mw.getCurrentFrame()
		pcall(frame.expandTemplate, frame, { title = 'tracking/eFloras/italics or bolding' })
		mw.log("Italics in parameter of {{eFloras}}:", orig)
	end
	
	local rank = ""
	if name == "" or name == nil then
		rank = ""
	elseif string.find(name, "aceae") then
		rank = "family"
	elseif string.find(name, "subsp.", nil, true) then
		rank = "subspecies"
	elseif string.find(name, "subg.", nil, true) then
		rank = "subgenus"
	elseif string.find(name, "var.", nil, true) then
		rank = "variety"
	elseif string.find(name, "sect.", nil, true) then
		rank = "section"
	elseif string.find(name, "%a%s%a") then
		rank = "species"
	elseif string.find(name, "%a") then
		rank = "genus"
	else
		error("Module:eFloras could not determine a taxonomic rank for the input that it received: " .. name)
	end
	
	mw.log(name, rank)
	
	if rank == "genus" or rank == "species" then
		return "<i>" .. name .. "</i>"
	elseif rank == "species" or rank == "variety" or rank == "subspecies" then
		local genus, species, lowerRank, lowerRankName = string.match(name, "(%a+)%s+(%a+)%s+(%a+%.)%s+(%a+)") -- Assumes a trinomial name.
		if genus == nil or species == nil or lowerRankName == nil or lowerRank == nil then
			error("The content being passed to the name function is not recognized")
		end
		return "<i>" .. genus .. " " .. species .. "</i> " .. lowerRank .. " <i>" .. lowerRankName .. "</i>"
	elseif rank == "section" or rank == "subgenus" then
		local genus, lowerRank, lowerRankName = string.match(name, "(%a+)%s+(%a+%.)%s+(%a+)")
		if genus == nil or lowerRankName == nil or lowerRank == nil then
			error("The content being passed to the name function is not recognized")
		end
		
		return "<i>" .. genus .. "</i> " .. lowerRank .. " <i>" .. lowerRankName .. "</i>"
	elseif rank == "family" then
		return name
	else
		return ""
	end
end

function p.name(frame)
	local name = frame.args[1]
	return p.italicize(name)
end 

p.get_volume = p.volume

return p