From Arms of God Wiki

bot: Iconbox mode= named-arg fix (empty positional arg masked it)
bot: documentation pass — editor-facing docs (data descriptions, module comments, template usage, Help rewrite)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:Iconbox — entity icon lookup (Arms of God).
-- Module:Iconbox — an entity's small icon + linked name, anywhere.
-- Reads Data:Icons.json (lowercased display names + slugs -> icon/slug/name).
--
-- {{#invoke:Iconbox|render|<name-or-slug>}}            -> icon + linked name (default)
-- WHAT IT DOES
-- {{#invoke:Iconbox|render|<name-or-slug>|icononly}}   -> linked icon only
--  Looks up an entity by category + name (or just name) and renders
-- 'link' as 2nd arg is a no-op alias of the default; mode=icon == icononly.
--  its icon and a link to its page. Icons come from each entity's own
-- Unknown entity or missing icon degrades to text-only (never a redlink file).
--   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 p = {}


local function lookup(key)
local function trim(s)
   if not key or key == '' then return nil end
   return (tostring(s or ''):gsub('^%s+', ''):gsub('%s+$', ''))
   local ok, data = pcall(mw.loadJsonData, 'Data:Icons.json')
end
  if not ok or type(data) ~= 'table' or type(data.by_key) ~= 'table' then
 
     return nil
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
   end
   return data.by_key[mw.ustring.lower(key)]
   return nil
end
end


function p.render(frame)
function p.render(frame)
   local key = (frame.args[1] or ''):gsub('^%s+', ''):gsub('%s+$', '')
   local a1 = trim(frame.args[1])
   if key == '' then return '' end
   if a1 == '' then return '' end
   -- positional arg 2 arrives as '' (not nil) via the template's {{{2|}}},
   local a2 = trim(frame.args[2])
   -- and '' is truthy in Lua — fall through to mode= only when 2 is blank.
  local a3 = trim(frame.args[3])
  local mode = frame.args[2] or ''
 
   if mode:match('^%s*$') then mode = frame.args.mode or '' end
  local cat = Core.resolveCategory(a1)
   mode = mw.ustring.lower((mode:gsub('^%s+', ''):gsub('%s+$', '')))
  local key, mode, rec
  -- default = icon + linked name; only an explicit icon-only flag drops the name
  if cat and a2 ~= '' then
   local iconlonly = (mode == 'icononly' or mode == 'icon' or mode == 'noname')
    -- category-first form: {{Iconbox|Weapon|Cutter|icononly?}}
  local withname = not iconlonly
    key, mode = a2, a3
   local size = frame.args.size or '24'
    rec = findInCategory(cat, key)
  local rec = lookup(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 not rec then
     -- unknown key: plain link in named mode, nothing in icon-only mode
     if icononly then return '' end
     if withname then return '[[' .. key .. ']]' end
     return '[[' .. key .. ']]'
    return ''
   end
   end
   local slug = rec.slug or key
   local slug = rec.slug or key
   local name = rec.name or key
   local name = rec.name or key
   local icon = rec.icon or ''
   local icon = rec.icon or ''
  local tip = '<span class="wm-tip" data-tip-title="' .. slug .. '">'
   if icon == '' then
   if icon == '' then
     if withname then return '[[' .. slug .. '|' .. name .. ']]' end
     if icononly then return '' end
    return ''
    return tip .. '[[' .. slug .. '|' .. name .. ']]</span>'
   end
   end
   local img = '[[File:' .. icon .. '|' .. size .. 'px|link=' .. slug
   local img = '[[File:' .. icon .. '|' .. size .. 'px|link=' .. slug
     .. '|alt=' .. name .. ']]'
     .. '|alt=' .. name .. ']]'
   if withname then return img .. ' [[' .. slug .. '|' .. name .. ']]' end
   if icononly then return img end
  return img
  return tip .. img .. ' [[' .. slug .. '|' .. name .. ']]</span>'
end
end


return p
return 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