Module:Game icon

From Final Fantasy XIV Online Wiki
Jump to navigation Jump to search
Documentation for Module:Game icon [view] [edit] [history] [purge] (How does this work?)

This module implements {{Game icon}}, the wiki's universal icon template. It handles SMW lookups and argument processing before shelling out to Module:Icon to perform the actual rendering.

local getArgs = require("Module:Arguments").getArgs
local Icon = require("Module:Icon")
local yesno = require("Module:Yesno")

local p = {}

function p.main(frame)
  local args = getArgs(frame)

  -- attempt to fetch SMW data
  local page = (mw.smw.ask({
    ("[[%s]]"):format(args[1]),
    "?=Pagename#",
    "?Has context",
    "?Has game icon#",
    "?Has canonical name",
    "?Has dye channels",
    "?Has game icon frame type",
  }) or {})[1] -- only first result; if query is invalid treat as no result

  -- render broken link for nonexistant pages
  if not page then
    return Icon.gameIconWithLabel({
      text = args[2] or args.alt or args[1],
      link = args[1],
    })
  end

  -- get the logical name of the thing being linked
  local canonicalName = page["Has canonical name"]
  if not canonicalName or canonicalName == "" then
    canonicalName = args[1]
  end

  -- compute final label text and alt text based on user value
  local textParam = args[2] or args.alt  -- 2 takes precedence over alt
  local text = canonicalName
  local alt = canonicalName
  if textParam == "x" then
    text = ""
    -- alt remains the canonical name for screen readers
  elseif textParam and textParam ~= "" then
    -- use provided label as both label and alt
    text = textParam
    alt = textParam
  end
  -- if no text param was given, canonical name is used for both

  -- yippee
  return Icon.gameIconWithLabel({
    -- SMW data
    icon = page["Has game icon"],
    frameType = page["Has game icon frame type"],
    type = page["Has context"],
    dyeCount = page["Has dye channels"] or 0,

    -- computed parameters
    text = text,
    alt = alt,

    -- other passthrough parameters
    link = args[1],
    size = args.size,
    hq = yesno(args.hq),
    qty = args.qty,
    allowWrap = yesno(args.wrap),
  })
end

return p