From Arms of God Wiki

Revision as of 04:05, 10 June 2026 by Ta1ha (talk | contribs) (bot: Iconbox mode= named-arg fix (empty positional arg masked it))

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

-- Module:Iconbox — entity icon lookup (Arms of God).
-- Reads Data:Icons.json (lowercased display names + slugs -> icon/slug/name).
-- {{#invoke:Iconbox|render|<name-or-slug>}}            -> icon + linked name (default)
-- {{#invoke:Iconbox|render|<name-or-slug>|icononly}}   -> linked icon only
-- 'link' as 2nd arg is a no-op alias of the default; mode=icon == icononly.
-- Unknown entity or missing icon degrades to text-only (never a redlink file).
local p = {}

local function lookup(key)
  if not key or key == '' then return nil end
  local ok, data = pcall(mw.loadJsonData, 'Data:Icons.json')
  if not ok or type(data) ~= 'table' or type(data.by_key) ~= 'table' then
    return nil
  end
  return data.by_key[mw.ustring.lower(key)]
end

function p.render(frame)
  local key = (frame.args[1] or ''):gsub('^%s+', ''):gsub('%s+$', '')
  if key == '' then return '' end
  -- positional arg 2 arrives as '' (not nil) via the template's {{{2|}}},
  -- and '' is truthy in Lua — fall through to mode= only when 2 is blank.
  local mode = frame.args[2] or ''
  if mode:match('^%s*$') then mode = frame.args.mode or '' end
  mode = mw.ustring.lower((mode:gsub('^%s+', ''):gsub('%s+$', '')))
  -- default = icon + linked name; only an explicit icon-only flag drops the name
  local iconlonly = (mode == 'icononly' or mode == 'icon' or mode == 'noname')
  local withname = not iconlonly
  local size = frame.args.size or '24'
  local rec = lookup(key)
  if not rec then
    -- unknown key: plain link in named mode, nothing in icon-only mode
    if withname then return '[[' .. key .. ']]' end
    return ''
  end
  local slug = rec.slug or key
  local name = rec.name or key
  local icon = rec.icon or ''
  if icon == '' then
    if withname then return '[[' .. slug .. '|' .. name .. ']]' end
    return ''
  end
  local img = '[[File:' .. icon .. '|' .. size .. 'px|link=' .. slug
    .. '|alt=' .. name .. ']]'
  if withname then return img .. ' [[' .. slug .. '|' .. name .. ']]' end
  return img
end

return p