From Arms of God Wiki

bot: operator iteration 2026-06-10
 
bot: Iconbox default now icon + linked name; icononly flag added (operator iteration 2026-06-10)
Line 1: Line 1:
-- Module:Iconbox — entity icon lookup (Arms of God).
-- Module:Iconbox — entity icon lookup (Arms of God).
-- Reads Data:Icons.json (lowercased display names + slugs -> icon/slug/name).
-- Reads Data:Icons.json (lowercased display names + slugs -> icon/slug/name).
-- {{#invoke:Iconbox|render|<name-or-slug>}}       -> linked icon only
-- {{#invoke:Iconbox|render|<name-or-slug>}}           -> icon + linked name (default)
-- {{#invoke:Iconbox|render|<name-or-slug>|link}}  -> icon + linked name
-- {{#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).
-- Unknown entity or missing icon degrades to text-only (never a redlink file).
local p = {}
local p = {}
Line 18: Line 19:
   local key = (frame.args[1] or ''):gsub('^%s+', ''):gsub('%s+$', '')
   local key = (frame.args[1] or ''):gsub('^%s+', ''):gsub('%s+$', '')
   if key == '' then return '' end
   if key == '' then return '' end
   local withname = (frame.args[2] or frame.args.text or '') ~= ''
   local mode = (frame.args[2] or frame.args.mode or '')
  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 size = frame.args.size or '24'
   local rec = lookup(key)
   local rec = lookup(key)
   if not rec then
   if not rec then
     -- unknown key: plain link in text mode, nothing in icon-only mode
     -- unknown key: plain link in named mode, nothing in icon-only mode
     if withname then return '[[' .. key .. ']]' end
     if withname then return '[[' .. key .. ']]' end
     return ''
     return ''

Revision as of 04:03, 10 June 2026

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
  local mode = (frame.args[2] or frame.args.mode or '')
  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