Module:ItemLink

From Secrets of Grindea Wiki
Jump to navigation Jump to search

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

local p = {}

local func = require('Module:Func')

local is_empty = func.is_empty
local nil_if_empty = func.nil_if_empty

--[[

Module for Template:ItemLink

ItemLinks consist of three elements: image, label and article (link). These can be
supplied using the "image", "label" and "article" parameters respectively

--]]

function p.link(frame)
  -- Option parameters
  local id         = nil_if_empty(frame.args["id"])
  local wh         = nil_if_empty(frame.args["wh"])
  local nolink     = not is_empty(frame.args["nolink"])
  local softErrors = not is_empty(frame.args["softErrors"])
  
  -- Core parameters
  local article = nil_if_empty(frame.args["article"])
  local label   = nil_if_empty(frame.args["label"])
  local image   = nil_if_empty(frame.args["image"])

  -- If ID is specified, then replace any nil values with its data
  if id ~= nil then
    local itemModule = require("Module:Item")
    if softErrors then
      image   = image or itemModule.data({["args"] = {id, "sprite", ["softErrors"] = true}})
      label   = label or itemModule.data({["args"] = {id, "name", ["softErrors"] = true}})
      article = article or itemModule.data({["args"] = {id, "name", ["softErrors"] = true}})
    else
      local name = itemModule.data({["args"] = {id, "name"}})

      image   = image or itemModule.data({["args"] = {id, "sprite"}})
      label   = label or name
      article = article or itemModule.data({["args"] = {id, "article"}}) or name
    end
    
    -- While using "softErrors" only - signifies fail
    if softErrors and (image == "Error" or label == "Error") then
      image  = "Placeholder.png"
      label  = "Unknown Item"
      nolink = true
    end
  end

  -- If label is left unspecified, use article as value if provided
  label = label or article

  -- Finally, if info is still missing, use placeholder values (usually means an error happened)
  label = label or "Unknown"
  image = image or "Placeholder.png"
  wh    = wh or "25"

  if nolink or article == nil then
    article = ''
  end

  local textString
  if article ~= '' then
    textString = "[[" .. article .. "|" .. label .. "]]"
  else
    textString = label
  end

  -- Set image 
  return "[[File:" .. image .. "|class=game-sprite|link=" .. article .. "|" .. wh .. "px" .. "]] " .. textString
end

return p