Module:SpecializationLink: Difference between revisions

From Against the Storm Official Wiki
m (the file part too :()
(Updated with Foxes specs)
Line 28: Line 28:
local SPEC_ENGI = "Engineering"
local SPEC_ENGI = "Engineering"
local SPEC_FARM = "Farming"
local SPEC_FARM = "Farming"
local SPEC_FORE = "Forest"
local SPEC_MEAT = "Meat production"
local SPEC_MEAT = "Meat production"
local SPEC_RAIN = "Rainwater"
local SPEC_WARM = "Warmth"
local SPEC_WARM = "Warmth"
local SPEC_WOOD = "Wood"
local SPEC_WOOD = "Wood"
Line 78: Line 80:
[SPEC_ENGI] = { iconfile="Icon Spec Gear 64x64.png" },
[SPEC_ENGI] = { iconfile="Icon Spec Gear 64x64.png" },
[SPEC_FARM] = { iconfile="Icon_Spec_Farm_64x64.png" },
[SPEC_FARM] = { iconfile="Icon_Spec_Farm_64x64.png" },
[SPEC_FORE] = { iconfile="Icon Spec Forest 64x64.png" },
[SPEC_MEAT] = { iconfile="Icon_Spec_Meat_64x64.png" },
[SPEC_MEAT] = { iconfile="Icon_Spec_Meat_64x64.png" },
[SPEC_RAIN] = { iconfile="Icon Spec RainWater 64x64.png" },
[SPEC_WARM] = { iconfile="Icon_Spec_Fire_64x64.png" },
[SPEC_WARM] = { iconfile="Icon_Spec_Fire_64x64.png" },
[SPEC_WOOD] = { iconfile="Icon_Spec_Wood_64x64.png" }
[SPEC_WOOD] = { iconfile="Icon_Spec_Wood_64x64.png" }

Revision as of 02:23, 7 December 2023

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

-------------------------------------------------------------------------------
-- This module renders the {{Specialization _link}} template.
-- https://hoodedhorse.com/wiki/Against_the_Storm/Template:Specialization_link
--
-- This template #invokes SpecializationLink.renderLink(frame), below.
--
-- The template requires an argument, the name of the specialization to which to
-- link. Optionally, the template accepts a second argument to change the size 
-- of the icon that accompanies the text hyperlink.
--
-- Using the arguments, this module creates an icon and the name of the 
-- specialization, and wraps both in a link to the specialization's wiki page. 
-- If no icon size is specified, the icon will default to a size appropriate for
-- display in-line with other text.
--
-- The icons are sized consistently by using existing wiki templates, {{ImgS}}, 
-- {{ImgM}}, etc. The sizes of icons are not stored or known by this module.
-- @module SpecializationLink
local SpecializationLink = {}

--
-- Constants
--
-- specialization names
local SPEC_ALCH = "Alchemy"
local SPEC_BREW = "Brewing"
local SPEC_CLOT = "Cloth"
local SPEC_ENGI = "Engineering"
local SPEC_FARM = "Farming"
local SPEC_FORE = "Forest"
local SPEC_MEAT = "Meat production"
local SPEC_RAIN = "Rainwater"
local SPEC_WARM = "Warmth"
local SPEC_WOOD = "Wood"

-- names of the templates used to size the icons consistently
local TEMPLATE_IMGSMALL = "ImgS"
local TEMPLATE_IMGMED = "ImgM"
local TEMPLATE_IMGLARGE = "ImgL"
local TEMPLATE_IMGHUGE = "ImgH"

-- string options for icon size arguments
local S_SML = "small"
local S_MED = "med"
local S_LRG = "large"
local S_HUG = "huge"

--
-- Main rendering function
--

-------------------------------------------------------------------------------
-- Renders an icon the name of a specialization linked to a wiki page 
-- corresponding to the provided specialization.
-- 
-- Uses MediaWiki markup to display the name of a wiki page and an icon that has 
-- been uploaded to the wiki.
-- @param frame  a table describing the MediaWiki frame; frame['args'] is a 
-- table containg the template arguments; frame['args']['spec'] is the first
-- argument, assigned to the key 'spec'; frame['args']['iconsize'] is the 
-- second argument, assigned to the key 'iconsize'
-- @return a string containing the wiki markup for an icon and an internal wiki 
-- page link (or an error if a problem occured)
function SpecializationLink.renderLink(frame)
	
	-- extract the arguments we care about from the frame
	local argSpecName = frame.args.spec
	local argIconsize = frame.args.iconsize
	
	-- validate that there's a resource name to use
	if not argSpecName or "" == argSpecName then
		return "SpecializationLink error: no resource given"
	end
	
	-- simple lookup for a small number of specializations with little extra data
	local specLookup = {
		[SPEC_ALCH] = { iconfile="Icon Spec Alchemy 64x64.png" },
		[SPEC_BREW] = { iconfile="Icon Spec Brewing 64x64.png" },
		[SPEC_CLOT] = { iconfile="Icon Spec Cloth 64x64.png" },
		[SPEC_ENGI] = { iconfile="Icon Spec Gear 64x64.png" },
		[SPEC_FARM] = { iconfile="Icon_Spec_Farm_64x64.png" },
		[SPEC_FORE] = { iconfile="Icon Spec Forest 64x64.png" },
		[SPEC_MEAT] = { iconfile="Icon_Spec_Meat_64x64.png" },
		[SPEC_RAIN] = { iconfile="Icon Spec RainWater 64x64.png" },
		[SPEC_WARM] = { iconfile="Icon_Spec_Fire_64x64.png" },
		[SPEC_WOOD] = { iconfile="Icon_Spec_Wood_64x64.png" }
	}
	
	-- make sure the argument was a valid name
	local specInfo = specLookup[argSpecName]
	if not specInfo then
		return "SpecializationLink error: no specialization " .. argSpecName .. " found."
	end
	
	-- the wiki markup for the internal link to the specializations's wiki page
	local linkPart = "[[" .. argSpecName .. " Specialization|" .. argSpecName .. "]]"
	
	-- store the requested size for use in a moment
	local size = ""
	
	-- Check the requested size against the allowed options. Expand the 
	-- corresponding template.
	if S_MED == argIconsize then
		size = frame:expandTemplate{title=TEMPLATE_IMGMED}
	elseif S_LRG == argIconsize then
		size = frame:expandTemplate{title=TEMPLATE_IMGLARGE}
	elseif S_HUG == argIconsize then
		size = frame:expandTemplate{title=TEMPLATE_IMGHUGE}
	else
		-- default to small size if the argument wasn't valid
		size = frame:expandTemplate{title=TEMPLATE_IMGSMALL}
	end
	
	-- If the icon filename doesn't exist, we can still continue by substituting 
	-- a question mark icon instead.
	local icon = specInfo.iconfile
	
	-- combine the size and filename to form the iconPart
	local iconPart = "[[File:" .. icon .. "|" .. size .. "|link=" .. argSpecName .. " Specialization|alt=" .. argSpecName .. " Specialization|" .. argSpecName .. " Specialization]]"
	
	-- combine the file part with the link part
	return iconPart .. " " .. linkPart
end



-- return when required into another module
return SpecializationLink