Module:UtilsAnimalTables: Difference between revisions

From Eco - English Wiki
[unchecked revision][unchecked revision]
(Add various utility functions to use when building tables of animals from Module:AnimalData)
 
m (Fix image creation.)
Line 58: Line 58:
function p.buildResourceItemIcon(imageFileName, link, backgroundColor)
function p.buildResourceItemIcon(imageFileName, link, backgroundColor)
local iconBg = string.format("icon%s", backgroundColor)
local iconBg = string.format("icon%s", backgroundColor)
local file = "" --string.format("[[File:%s|frameless|class=iconRecipe %s|link=%s]]", Utils.checkImage(imageFileName, false), iconBg, link)
local file = string.format("[[File:%s|frameless|class=iconRecipe %s|link=%s]]", Utils.checkImage(imageFileName, false), iconBg, link)
return string.format("<div class=iconContainerSmall><div class=\"iconStack\">%s</div><div class=\"iconBorder borderBlue\" style=\"position:absolute;\"></div></div>", file)
return string.format("<div class=iconContainerSmall><div class=\"iconStack\">%s</div><div class=\"iconBorder borderBlue\" style=\"position:absolute;\"></div></div>", file)
end
end

Revision as of 17:35, 22 February 2022

Various utility functions to use when building tables of animals from Module:AnimalData. Primarily intended for use in Module:Table Animals and Module:Table Fish.

Usage

Add the following line of code at the top of your file.

local AnimalUtils = require("Module:UtilsAnimalTables")

-- You may then call functions from this module in your script. For example:
local rangeText = AnimalUtils.toCountRange(2, 5, true)

local p = {}
local Utils = require("Module:Utils")
local HTMLUtils = require("Module:UtilsHTML")

--- Format the input integers with <code>toRangeString</code> only if <code>1 < min</code> and optionally adds parentheses around the range.
-- @param #number min Minimum value (left side)
-- @param #number min Maximum value (right side)
-- @param #bool parentheses Add parentheses around the range?
-- @return #string "<code>min</code>—<code>max</code>"
-- @return #string " (<code>min</code>—<code>max</code>)"
-- @return #string "" (empty string) if <code>min == 1</code>
-- @author User:Demian
-- @see toRangeString
function p.toCountRange(min, max, parentheses)
	local range = Utils.toRangeString(min, max, min, "%d")

	if "1" == range then
		return ""
	else
		if parentheses then
			return string.format(" (%s)", range)
		else
			return range
		end
	end
end

--- Format <code>height</code> into an integer, if it is greater than <code>0.0</code>.
-- @param #number height Value to format
-- @return #string "<code>height</code>" as an integer as a string.
-- @return #string "—" if <code>height == 0.0 or height == nil</code>.
-- @return #string "∞" if <code>height < 0.0</code>.
-- @author User:Demian
function p.formatClimbHeight(height)
	local heightNum = tonumber(Utils.valueOrDefault(height, 0.0))

	if 0.0 < heightNum then
		return string.format("%d", heightNum)
	elseif 0.0 == heightNum then
		-- nil or 0.0
		return "—"
	else
		-- TODO: Support i18n.
		return HTMLUtils.tagAbbr("&infin;", "Unlimited")
	end
end

--- Create HTML code for a small image (recipe size) of the specified file with a blue border.
--
-- Heavily simplified from <code>Utils.build_icon</code>.
-- @param #string imageFileName Name of the image file to display
-- @param #string link Wikitext for a link to some page
-- @param #string backgroundColor One of: <code>"Green"</code>, <code>"Blue"</code>
-- @return #string HTML code wrapped around wikitext to display an image with a link.
-- @author User:Demian
-- @see formatWikilink
-- @see Util.build_icon
function p.buildResourceItemIcon(imageFileName, link, backgroundColor)
	local iconBg = string.format("icon%s", backgroundColor)
	local file = string.format("[[File:%s|frameless|class=iconRecipe %s|link=%s]]", Utils.checkImage(imageFileName, false), iconBg, link)
	return string.format("<div class=iconContainerSmall><div class=\"iconStack\">%s</div><div class=\"iconBorder borderBlue\" style=\"position:absolute;\"></div></div>", file)
end

--- Sort items in the string list of foods, if any.
-- @param #string foodCSV String to split
-- @param #string character Character to split string with
-- @return #string <code>foodCSV</code> with items in alphabetical order.
-- @return #string "—" if <code>foodCSV</code> is <code>nil</code>.
-- @return #string <code>foodCSV</code> unchanged if it contains no commas.
-- @author User:Demian
function p.formatFoodSources(foodCSV)
	if nil == foodCSV then
		return "—"
	elseif string.find(foodCSV, ",") then
		return Utils.sortListString(foodCSV, ",", ", ")
	else
		-- Skip all sorting if there is only one item in foods.
		return foodCSV
	end
end

--- Create HTML code to display an item harvested from an animal with an image of the item on the left and the name (with link) on the right side.
--
-- Will additionally display the number of items harvested as a range, but only if <code>1 < resourceMin</code>.
-- @param #string itemLink Wikitext link to the resource item
-- @param #number resourceMin Minimum number of items that harvesting will yield
-- @param #number resourceMax Maximum number of items that harvesting will yield
-- @return #string HTML code for a table displaying the harvested item.
-- @author User:Demian
function p.formatResourceItem(itemLink, resourceMin, resourceMax)
	local itemName = string.sub(itemLink, 3, -3)
	local actualPageName = Utils.getDirectPageName(itemName, "item")
	local countString = p.toCountRange(resourceMin, resourceMax, true)
	local linkHTML = string.format("%s%s", Utils.formatWikilink(actualPageName, itemName, true), countString)

	-- Raw meat seems to be the only exception to the harvest items. It is the only "food" item.
	local iconHTML = p.buildResourceItemIcon(itemName, Utils.formatWikilink(actualPageName, actualPageName, false), "Raw Meat" == itemName and "Green" or "Blue")

	return HTMLUtils.twoCellTable(iconHTML, linkHTML)
end

return p