Difference between revisions of "Module:EFloras"

From Eat Every Plant
Jump to navigation Jump to search
m (1 revision: From PNW foraging book - part 3)
 
m (1 revision: Taxus)
 
(3 intermediate revisions by the same user not shown)
Line 7: Line 7:
 
local function getResource(floraID)
 
local function getResource(floraID)
 
data = data or mw.loadData("Module:eFloras/data")
 
data = data or mw.loadData("Module:eFloras/data")
return data.resources[floraID]
+
return data.resources[floraID] or data.resources[tonumber(floraID)]
 
end
 
end
  
Line 22: Line 22:
 
end
 
end
 
end
 
end
 +
end
 +
 +
function p._volumeName(floraID, volume, family)
 +
floraID = tonumber(floraID)
 +
 +
if not floraID then -- floraID is not a number.
 +
return
 +
end
 +
 +
data = data or mw.loadData("Module:eFloras/data")
 +
 +
if not volume then
 +
local familyToVolume = data.volumeTable[floraID]
 +
if not familyToVolume then
 +
return
 +
end
 +
 +
volume = tonumber(familyToVolume[family])
 +
 +
if not volume then
 +
return
 +
end
 +
end
 +
 +
local floraVolumeNames = data.volumeNames and data.volumeNames[floraID]
 +
if floraVolumeNames and volume then
 +
return floraVolumeNames[volume]
 +
end
 +
end
 +
 +
function p.volumeName(frame)
 +
if not (frame.args[1] and (frame.args[2] or frame.args[3] or frame.args.family)) then
 +
return
 +
end
 +
 +
local floraID = string.match(frame.args[1], "%d+")
 +
local volume = tonumber(frame.args[2])
 +
local family = frame.args[3] or frame.args.family
 +
 +
if not (floraID and (volume or family)) then
 +
return
 +
end
 +
 +
return p._volumeName(floraID, volume, family)
 
end
 
end
  
Line 76: Line 120:
 
local family = frame.args[2] or frame.args.family
 
local family = frame.args[2] or frame.args.family
 
data = data or mw.loadData("Module:eFloras/data")
 
data = data or mw.loadData("Module:eFloras/data")
local familyToVolume = data.volumeTable[floraID]
+
local familyToVolume = data.volumeTable[floraID] or data.volumeTable[tonumber(floraID)]
 
if familyToVolume == nil then
 
if familyToVolume == nil then
 
return ""
 
return ""
Line 94: Line 138:
 
function p.italicize(name)
 
function p.italicize(name)
 
local orig = name
 
local orig = name
name = string.gsub(name, "^%s*(.*)%s*$", "%1")
+
name = string.gsub(name, "^%s*(.-)%s*$", "%1")
 
 
 
local count
 
local count
Line 109: Line 153:
 
 
 
local rank
 
local rank
 +
local lowerName = name:lower()
 
if name == "" or name == nil then
 
if name == "" or name == nil then
 
return
 
return
elseif string.find(name, "%u%l+eae$") then
+
elseif string.find(name, "^%u%l+ae$") then
if string.find(name, "aceae$") then
+
if string.find(name, "eae$") then
rank = "family"
+
if string.find(name, "aceae$") then
elseif string.find(name, "oideae$") then
+
rank = "family"
rank = "subfamily"
+
elseif string.find(name, "oideae$") then
else
+
rank = "subfamily"
rank = "tribe"
+
else
 +
rank = "tribe"
 +
end
 +
elseif string.find(name, "inae$") then
 +
rank = "subtribe"
 
end
 
end
elseif string.find(name, "%u%l+inae$") then
+
elseif string.find(lowerName, "subsp.", 1, true) then
rank = "subtribe"
 
elseif string.find(name, "subsp.", nil, true) then
 
 
rank = "subspecies"
 
rank = "subspecies"
elseif string.find(name, "subg.", nil, true) then
+
elseif string.find(lowerName, "subg.", 1, true) then
 
rank = "subgenus"
 
rank = "subgenus"
elseif string.find(name, "var.", nil, true) then
+
elseif string.find(lowerName, "var.", 1, true) then
 
rank = "variety"
 
rank = "variety"
elseif string.find(name, "sect.", nil, true) then
+
elseif string.find(lowerName, "sect.", 1, true) then
 
rank = "section"
 
rank = "section"
 
elseif string.find(name, "^%a+%s[%a-]+$") or string.find(name, "^%a+%s×%s[%a-]+$") then
 
elseif string.find(name, "^%a+%s[%a-]+$") or string.find(name, "^%a+%s×%s[%a-]+$") then
Line 134: Line 181:
 
and not string.find(name, ".%u") then -- Uppercase letters can only appear at beginning of taxonomic name.
 
and not string.find(name, ".%u") then -- Uppercase letters can only appear at beginning of taxonomic name.
 
rank = "genus"
 
rank = "genus"
else
 
mw.log("Module:eFloras could not determine a taxonomic rank for the input that it received: " .. name)
 
 
end
 
end
 
 
 
if not rank then
 
if not rank then
 +
mw.log("Module:eFloras could not determine a taxonomic rank for the input that it received: " .. name)
 
return orig
 
return orig
 
end
 
end
Line 147: Line 193:
 
name = "<i>" .. name .. "</i>"
 
name = "<i>" .. name .. "</i>"
 
local hybrid = "×"
 
local hybrid = "×"
local abbreviations = {
+
["subsp."] = true, ["ssp."] = true, ["var."] = true, ["f."] = true,
+
if name:find(".", 1, true) then
["sect."] = true, ["subsect."] = true, ["subg."] = true,
+
local abbreviations = {
}
+
["subsp."] = true, ["ssp."] = true, ["var."] = true, ["f."] = true,
local unrecognized
+
["sect."] = true, ["subsect."] = true, ["subg."] = true,
name = name:gsub(
+
}
"%s+(%S+%.)%s+",
+
function (abbreviation)
+
local unrecognized
if abbreviations[abbreviation] then
+
name = name:gsub(
return "</i> " .. abbreviation .. " <i>"
+
"%s+(%S+%.)%s+",
else
+
function (abbreviation)
unrecognized = unrecognized or {}
+
mw.log(name, abbreviation, abbreviation:lower(), abbreviations[abbreviation:lower()])
table.insert(unrecognized, abbreviation)
+
if abbreviations[abbreviation:lower()] then
end
+
return "</i> " .. abbreviation .. " <i>"
end)
+
else
 +
unrecognized = unrecognized or {}
 +
table.insert(unrecognized, abbreviation)
 +
end
 +
end)
 +
 +
if unrecognized then
 +
mw.log(string.format("The abbreviation%s %s %s not recognized.",
 +
unrecognized[2] and "s" or "",
 +
table.concat(
 +
unrecognized,
 +
", "),
 +
unrecognized[2] and "are" or "is"))
 +
return orig
 +
end
 +
end
 
 
 
name = name:gsub("%s+" .. hybrid .. "%s+", "</i> " .. hybrid .. " <i>")
 
name = name:gsub("%s+" .. hybrid .. "%s+", "</i> " .. hybrid .. " <i>")
 
if unrecognized then
 
mw.log(string.format("The abbreviation%s %s %s not recognized.",
 
unrecognized[2] and "s" or "",
 
table.concat(
 
unrecognized,
 
", "),
 
unrecognized[2] and "are" or "is"))
 
return orig
 
end
 
 
end -- Else do not modify name.
 
end -- Else do not modify name.
 
 

Latest revision as of 14:11, 11 September 2018

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] or data.resources[tonumber(floraID)] end

function p.resource(frame) local floraID = string.match(frame.args[1], "%d+") if floraID == nil then return "Please provide a resource number (flora_id). See the list of supported resource numbers at Module:eFloras/doc" else local familyToVolume = getResource(floraID) if familyToVolume == nil then return "The resource number (flora_id) " .. floraID .. " is not recognized. See the list of supported resource numbers at Module:eFloras/doc" else return familyToVolume end end end

function p._volumeName(floraID, volume, family) floraID = tonumber(floraID)

if not floraID then -- floraID is not a number. return end

data = data or mw.loadData("Module:eFloras/data")

if not volume then local familyToVolume = data.volumeTable[floraID] if not familyToVolume then return end

volume = tonumber(familyToVolume[family])

if not volume then return end end

local floraVolumeNames = data.volumeNames and data.volumeNames[floraID] if floraVolumeNames and volume then return floraVolumeNames[volume] end end

function p.volumeName(frame) if not (frame.args[1] and (frame.args[2] or frame.args[3] or frame.args.family)) then return end

local floraID = string.match(frame.args[1], "%d+") local volume = tonumber(frame.args[2]) local family = frame.args[3] or frame.args.family

if not (floraID and (volume or family)) then return end

return p._volumeName(floraID, volume, family) end

function p._volumeDate(floraID, volume, family) floraID = tonumber(floraID)

if not floraID then -- floraID is not a number. return end

data = data or mw.loadData("Module:eFloras/data")

if not volume then local familyToVolume = data.volumeTable[floraID] if not familyToVolume then return end

volume = tonumber(familyToVolume[family])

if not volume then return end end

local floraVolumeDates = data.volumeDates and data.volumeDates[floraID] if floraVolumeDates then if volume and floraVolumeDates[volume] then return floraVolumeDates[volume] else return floraVolumeDates.default end end end

function p.volumeDate(frame) if not (frame.args[1] and (frame.args[2] or frame.args[3] or frame.args.family)) then return end

local floraID = string.match(frame.args[1], "%d+") local volume = tonumber(frame.args[2]) local family = frame.args[3] or frame.args.family

if not (floraID and (volume or family)) then return end

return p._volumeDate(floraID, volume, family) 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 familyToVolume = data.volumeTable[floraID] or data.volumeTable[tonumber(floraID)] if familyToVolume == nil then return "" else local volume = familyToVolume[family] if volume == "error" then return "19–21" 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 input to the italicize function in Module:eFloras:", orig) end

local rank local lowerName = name:lower() if name == "" or name == nil then return elseif string.find(name, "^%u%l+ae$") then if string.find(name, "eae$") then if string.find(name, "aceae$") then rank = "family" elseif string.find(name, "oideae$") then rank = "subfamily" else rank = "tribe" end elseif string.find(name, "inae$") then rank = "subtribe" end elseif string.find(lowerName, "subsp.", 1, true) then rank = "subspecies" elseif string.find(lowerName, "subg.", 1, true) then rank = "subgenus" elseif string.find(lowerName, "var.", 1, true) then rank = "variety" elseif string.find(lowerName, "sect.", 1, true) then rank = "section" elseif string.find(name, "^%a+%s[%a-]+$") or string.find(name, "^%a+%s×%s[%a-]+$") then rank = "species" elseif string.find(name, "^%u%a+$") -- No one-letter genera, probably. and not string.find(name, ".%u") then -- Uppercase letters can only appear at beginning of taxonomic name. rank = "genus" end

if not rank then mw.log("Module:eFloras could not determine a taxonomic rank for the input that it received: " .. name) return orig end

if rank == "genus" or rank == "subgenus" or rank == "species" or rank == "subspecies" or rank == "variety" or rank == "section" then

name = "" .. name .. "" local hybrid = "×"

if name:find(".", 1, true) then local abbreviations = { ["subsp."] = true, ["ssp."] = true, ["var."] = true, ["f."] = true, ["sect."] = true, ["subsect."] = true, ["subg."] = true, }

local unrecognized name = name:gsub( "%s+(%S+%.)%s+", function (abbreviation) mw.log(name, abbreviation, abbreviation:lower(), abbreviations[abbreviation:lower()]) if abbreviations[abbreviation:lower()] then return " " .. abbreviation .. " " else unrecognized = unrecognized or {} table.insert(unrecognized, abbreviation) end end)

if unrecognized then mw.log(string.format("The abbreviation%s %s %s not recognized.", unrecognized[2] and "s" or "", table.concat( unrecognized, ", "), unrecognized[2] and "are" or "is")) return orig end end

name = name:gsub("%s+" .. hybrid .. "%s+", " " .. hybrid .. " ") end -- Else do not modify name.

return name end

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

p.get_volume = p.volume

return p