Module:Icon

local p = {}

local function cargo_find(table, link) if table == nil then return nil end local result = mw.ext.cargo.query(table, "_pageName,name", {       limit = 1,        where = string.format("_pageName='%s'", link:gsub("'", "''"))    }) return result[1] end

local function data_find(dataname, link) if dataname == nil then return nil end return mw.loadData(dataname)[link] end

local function entity_icon_html(image, link, name, size) local image = image:gsub("+", "_") local imglink = link if imglink == nil then imglink = 'File:' .. image end local textlink = '' if link ~= nil and link ~= '' and name ~= nil then textlink = string.format(' %s', link, name) end if size == nil then size = 50 end local bg = string.format('', size, size, imglink) local icon = string.format('', image, size, size, imglink) return tostring(        mw.html.create('span')        :css('display', 'inline-block')        :css('text-align', 'center')        :css('width', tostring(size)..'px')            :tag('div'):css('position', 'relative'):wikitext(bg)            :tag('div')                :css('position', 'absolute')                :css('left', '0px')                :css('top', '0px')                :wikitext(icon):allDone) .. textlink end

function p.entity_icon(frame) local a = frame.args return entity_icon_html(a.image, a.link, a.name, a.size) end

local function item_icon_html(image, link, name, size) local image = image:gsub("+", "_") local icon = string.format('', image, size, size, link) return tostring(mw.html.create('span')           :css('display', 'inline-block')            :css('text-align', 'center')            :css('width', tostring(size)..'px')                :wikitext(icon):allDone) .. string.format(' %s', link, name) end

-- Given page name (link), returns 3 values: --  1. The kind of page --  2. The updated link with disamibiguation remove (e.g. "Dagger (Equipment)" -> Dagger) --  3. The entity name (e.g. "Mighty (A Class)" -> Mighty) -- -- Example: "Mighty (A Class) (Guardian)" -> Guardian, "Might (A Class)", Mighty -- local function page_info(link) if link == "Normal Energy" or link == "Super Energy" then return "Item", link, link end local pagetypes = { {suf=" (Guardian)", data="Module:Guardian/data", table="guardians", kind="Guardian", image="Guardian_%s_icon.png"}, {suf=" (Equipment)", data="Module:Equipment/data", table="equipment", kind="Equipment", image="Equipment_%s_icon.png"}, {suf=" (Companion)", data="Module:Companion/data", table=nil, kind="Companion", image="Companion_%s_icon.png"}, {suf=" (Item)", data="Module:Item/data", table=nil, kind="Item", image="Item_%s.png"}, {suf=" (Character)", data="Module:Character/data", table="characters", kind="Character", image="Character_%s_icon.png"}, }   -- pages with the type in the title (e.g. "Kappa (Equipment)") for _, pt in ipairs(pagetypes) do       if link:sub(-pt.suf:len) == pt.suf then local key = link:sub(1, -pt.suf:len-1) local e = data_find(pt.data, key) if e ~= nil and e.name ~= nil then return pt.kind, key, e.name end local e = cargo_find(pt.table, link) if e ~= nil and e.name ~= nil then return pt.kind, key, e.name end return pt.kind, key, key end end -- search data for page type for _, pt in ipairs(pagetypes) do       local e = data_find(pt.data, link) if e ~= nil then if e.name ~= nil then return pt.kind, link, e.name end return pt.kind, link, link end end -- search cargo tables for page type for _, pt in ipairs(pagetypes) do      local e = cargo_find(pt.table, link) if e ~= nil then if e.name ~= nil then return pt.kind, link, e.name end return pt.kind, link, link end end -- search for icon file (uses expensive function) for _, pt in ipairs(pagetypes) do       local t = mw.title.makeTitle("File", string.format(pt.image, link)) if t ~= nil and t.exists then return pt.kind, link, link end end return "Unknown", link, link end

function p.icon(frame) local link = frame.args[1] local kind, key, name = page_info(link) if kind == "Guardian" then local image = string.format("Guardian_%s_icon.png", key) for _, suf in ipairs{" DNA", " RNA"} do           if link:sub(-suf:len) == suf then link = link:sub(1, -suf:len-1) end end return entity_icon_html(image, link, name) end if kind == "Equipment" then local image = string.format("Equipment_%s_icon.png", key) return entity_icon_html(image, link, name) end if kind == "Companion" then local image = string.format("Companion_%s_icon.png", key) return entity_icon_html(image, link, name) end if kind == "Character" then local image = string.format("Character_%s_icon.png", key) return entity_icon_html(image, link, name) end if kind == "Item" then local image = string.format("Item_%s.png", key) return item_icon_html(image, link, name, 20) end return string.format('%s', link, link) end

-- Run this before saving function p._test local testcases = { {kind="Guardian", link="Samatha", key="Samatha", name="Samatha"}, {kind="Guardian", link="Samatha DNA", key="Samatha DNA", name="Samatha DNA"}, {kind="Guardian", link="Samatha RNA", key="Samatha RNA", name="Samatha RNA"}, {kind="Guardian", link="Mighty (A Class)", key="Mighty (A Class)", name="Mighty"}, {kind="Equipment", link="Dagger", key="Dagger", name="Dagger"}, {kind="Equipment", link="Kappa (Equipment)", key="Kappa", name="Kappa"}, {kind="Equipment", link="Bahamut's Claw", key="Bahamut's Claw", name="Bahamut's Claw"}, {kind="Equipment", link="Zapper (A Class)", key="Zapper (A Class)", name="Zapper"}, {kind="Companion", link="Bluebird", key="Bluebird", name="Bluebird"}, {kind="Companion", link="+2% Fire Attack", key="+2% Fire Attack", name="+2% Fire Attack"}, {kind="Character", link="Sarah", key="Sarah", name="Sarah"}, {kind="Item", link="Fores Seed", key="Fores Seed", name="Fores Seed"}, {kind="Item", link="Field Time Controller", key="Field Time Controller", name="[F] Time Controller"}, {kind="Item", link="Normal Energy", key="Normal Energy", name="Normal Energy"}, {kind="Item", link="Super Energy", key="Super Energy", name="Super Energy"}, }   for _, tc in ipairs(testcases) do        local kind, key, name = page_info(tc.link) if tc.kind ~= kind or tc.key ~= key or tc.name ~= name then mw.log(string.format("failed: link=%s, kind=%s, key=%s, name=%s", tc.link, kind, key, name)) end local html = p.icon{args={tc.link}} if string.len(html) < 10 then mw.log(string.format("icon(%s) is %q", tc.link, html)) end end end

return p