From Arms of God Wiki

bot: render-time derivation refactor (phase 1)
bot: documentation pass — editor-facing docs (data descriptions, module comments, template usage, Help rewrite)
 
Line 1: Line 1:
-- Module:Iconbox — entity icon + linked name (Arms of God).
-- Module:Iconbox — an entity's small icon + linked name, anywhere.
-- RENDER-TIME DERIVATION: icons live on each entity's own source record
-- (rec.icon in Data:<Category>.json); there is no Data:Icons.json.
--
--
-- Brotato-style category-first signature (documented standard):
-- WHAT IT DOES
--  {{Iconbox|Weapon|Cutter}}            -> icon + linked name
--  Looks up an entity by category + name (or just name) and renders
--  {{Iconbox|Weapon|Cutter|icononly}}  -> linked icon glyph only
--  its icon and a link to its page. Icons come from each entity's own
-- Category accepts singular or plural (Weapon/Weapons, Tag/Tags, ...) and
--   record (rec.icon in Data:<Category>.json) — there is no separate
-- makes the lookup single-category + collision-safe (e.g. Upgrade vs
--   icon lookup table to maintain.
-- Achievement of the same name).
--
--
-- Bare-name fallback (cross-category Core.find scan; first category in
-- HOW TO INVOKE (normally via Template:Iconbox)
-- registry order wins):
--  Category-first form (preferred — collision-safe):
--   {{Iconbox|Cutter}}  /  {{Iconbox|Cutter|icononly}}
--    {{Iconbox|Weapon|Cutter}}            icon + linked name
--    {{Iconbox|Weapon|Cutter|icononly}}  linked icon glyph only
--    {{Iconbox|Blessing|Sanctified Halo|size=32}}
--  Category accepts singular or plural (Weapon/Weapons, Tag/Tags, ...).
--  Bare-name fallback (scans every category; first match in registry
--   order wins — beware same-named entities in two categories):
--     {{Iconbox|Cutter}}  /  {{Iconbox|Cutter|icononly}}
--
--
-- Unknown entity or missing icon degrades to a plain link (never a
-- SOURCE DATA IT READS (via Module:Core)
-- redlink file); icon-only mode degrades to nothing.
--  Any/all of the ten Data:<Category>.json pages, depending on the call.
--
-- EDITING NOTES
--  Unknown entity or missing icon degrades to a plain page link (never
--   a broken-file box); icon-only mode degrades to nothing.
local Core = require('Module:Core')
local Core = require('Module:Core')
local p = {}
local p = {}

Latest revision as of 16:04, 10 June 2026

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

-- Module:Iconbox — an entity's small icon + linked name, anywhere.
--
-- WHAT IT DOES
--   Looks up an entity by category + name (or just name) and renders
--   its icon and a link to its page. Icons come from each entity's own
--   record (rec.icon in Data:<Category>.json) — there is no separate
--   icon lookup table to maintain.
--
-- HOW TO INVOKE (normally via Template:Iconbox)
--   Category-first form (preferred — collision-safe):
--     {{Iconbox|Weapon|Cutter}}            icon + linked name
--     {{Iconbox|Weapon|Cutter|icononly}}   linked icon glyph only
--     {{Iconbox|Blessing|Sanctified Halo|size=32}}
--   Category accepts singular or plural (Weapon/Weapons, Tag/Tags, ...).
--   Bare-name fallback (scans every category; first match in registry
--   order wins — beware same-named entities in two categories):
--     {{Iconbox|Cutter}}  /  {{Iconbox|Cutter|icononly}}
--
-- SOURCE DATA IT READS (via Module:Core)
--   Any/all of the ten Data:<Category>.json pages, depending on the call.
--
-- EDITING NOTES
--   Unknown entity or missing icon degrades to a plain page link (never
--   a broken-file box); icon-only mode degrades to nothing.
local Core = require('Module:Core')
local p = {}

local function trim(s)
  return (tostring(s or ''):gsub('^%s+', ''):gsub('%s+$', ''))
end

local function findInCategory(cat, key)
  local k = mw.ustring.lower(key)
  local rec = Core.byId(cat)[key]
  if rec then return rec end
  for _, r in ipairs(Core.load(cat)) do
    if (r.name and mw.ustring.lower(r.name) == k)
      or (r.slug and mw.ustring.lower(r.slug) == k) then
      return r
    end
  end
  return nil
end

function p.render(frame)
  local a1 = trim(frame.args[1])
  if a1 == '' then return '' end
  local a2 = trim(frame.args[2])
  local a3 = trim(frame.args[3])

  local cat = Core.resolveCategory(a1)
  local key, mode, rec
  if cat and a2 ~= '' then
    -- category-first form: {{Iconbox|Weapon|Cutter|icononly?}}
    key, mode = a2, a3
    rec = findInCategory(cat, key)
  else
    -- bare-name form: {{Iconbox|Cutter|icononly?}}
    key, mode = a1, a2
    local _, found = Core.find(key)
    rec = found
  end
  if mode == '' then mode = trim(frame.args.mode) end
  mode = mw.ustring.lower(mode)
  local icononly = (mode == 'icononly' or mode == 'icon' or mode == 'noname')
  local size = trim(frame.args.size)
  if size == '' then size = '24' end

  if not rec then
    if icononly then return '' end
    return '[[' .. key .. ']]'
  end
  local slug = rec.slug or key
  local name = rec.name or key
  local icon = rec.icon or ''
  local tip = '<span class="wm-tip" data-tip-title="' .. slug .. '">'
  if icon == '' then
    if icononly then return '' end
    return tip .. '[[' .. slug .. '|' .. name .. ']]</span>'
  end
  local img = '[[File:' .. icon .. '|' .. size .. 'px|link=' .. slug
    .. '|alt=' .. name .. ']]'
  if icononly then return img end
  return tip .. img .. ' [[' .. slug .. '|' .. name .. ']]</span>'
end

return p