Editing Module:Icon

Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
Line 1: Line 1:
local p = {}
local p = {}


local iconDataRaw = mw.loadJsonData("Data:ResourceLookup.json")
-- Load language codes
local iconData2 = mw.loadJsonData("Data:ResourceLookup2.json")
local lang_codes = mw.loadJsonData("Data:LangCodes.json")
local dictionary = mw.loadJsonData("Data:Dictionary")


local iconData = {}
-- Cache for compiled icon data, split by type (persists during page render)
local heroCacheBuilt = false
local abilityCacheBuilt = false
local itemCacheBuilt = false
local iconDataCache = {}


for k, v in pairs(iconDataRaw) do
local cachedLangCode, cachedLangData
    iconData[k] = v
end


for k, v in pairs(iconData2) do
-- Cache for expensive existence checks (persists during page render)
    iconData[k] = v
local existenceCache = {}
end


local lang_codes = mw.loadJsonData("Data:LangCodes.json")
-- Heroes/Items that don't have images uploaded yet
local lang_module = require("Module:Lang")
local missingImageOverrides = {
local table_data = require("Module:Icon/data")
    -- Unreleased Heroes
    ["boho"] = "Module-Icon missing.png",
    ["druid"] = "Module-Icon missing.png",
    ["fortuna"] = "Module-Icon missing.png",
    ["graf"] = "Module-Icon missing.png",
    ["gunslinger"] = "Module-Icon missing.png",
    ["skyrunner"] = "Module-Icon missing.png",
    ["swan"] = "Module-Icon missing.png",
    ["thumper"] = "Module-Icon missing.png",
    -- Add any others that show red links
}


local cachedLangCode
-- Build lookup table from bot-uploaded JSONs, only loading the relevant JSON per type
-- to reduce CPU cost on pages with many icon calls
local function buildIconData(iconType)
    -- Only load hero data if not already built
    if iconType == "hero" and not heroCacheBuilt then
        local heroData = mw.loadJsonData("Data:HeroData.json")


local linkOverrides = table_data.linkOverrides
        -- Process Heroes
local legacyItems = table_data.legacyItems
        for heroKey, hero in pairs(heroData) do
local missingImageOverrides = table_data.missingImageOverrides
            if type(hero) == "table" and hero.Name then
local aliases = table_data.aliases
                local name = hero.Name
local renames = table_data.renames
                local lowerName = name:lower()


local function getLangCode()
                -- Check for missing image override
    if cachedLangCode then
                local imageName = missingImageOverrides[lowerName] or (name .. ".png")
        return cachedLangCode
    end


    local title = mw.title.getCurrentTitle().fullText
                iconDataCache[lowerName] = {
    local subpageLang = title:match("/([^/]+)$")
                    name = name,
    local langCode = (subpageLang and lang_codes[subpageLang]) and subpageLang or "en"
                    key = heroKey,
                    link = name,
                    image = imageName,
                    type = "hero"
                }
            end
        end


    cachedLangCode = langCode
        heroCacheBuilt = true
    return langCode
end


-- Infer image path from name, with override support for missing images
    -- Only load ability data (and hero data for parent linking) if not already built
local function getImage(iconInfo, args)
    elseif iconType == "ability" and not abilityCacheBuilt then
    local lowerName = iconInfo.name:lower()
        local heroData = mw.loadJsonData("Data:HeroData.json")
        local abilityData = mw.loadJsonData("Data:AbilityData.json")


    if args.image then
        -- Track which hero owns which ability (for linking)
         return args.image
         local abilityToHero = {}
    end


    return missingImageOverrides[lowerName] or (iconInfo.name .. ".png")
        for heroKey, hero in pairs(heroData) do
end
            if type(hero) == "table" and hero.BoundAbilities then
                for slot, ability in pairs(hero.BoundAbilities) do
                    if ability.Key then
                        abilityToHero[ability.Key] = {
                            heroName = hero.Name,
                            abilityName = ability.Name
                        }
                    end
                end
            end
        end


-- Get the translated name
        -- Process Abilities
local function getTranslatedName(iconInfo, langCode)
        for abilityKey, ability in pairs(abilityData) do
    if langCode == "en" then
            if type(ability) == "table" and ability.Name then
        return iconInfo.name
                local name = ability.Name
    end
                local lowerName = name:lower()
                local parentInfo = abilityToHero[abilityKey]


    -- 1. Direct key
                -- If we already have this ability name, prefer the one linked to a hero
    if iconInfo.key and iconInfo.key ~= "" then
                if iconDataCache[lowerName] and iconDataCache[lowerName].type == "ability" then
        local translated = lang_module.get_string(iconInfo.key, langCode, iconInfo.name
                    local existingHasParent = iconDataCache[lowerName].link ~= iconDataCache[lowerName].name
        )
                    local newHasParent = parentInfo ~= nil


        if type(translated) == "string" then
                    -- Skip if existing is linked to hero but this one isn't (prevents orphans from overwriting real ones)
            return (translated:gsub("<[^>]+>", ""))
                    if existingHasParent and not newHasParent then
                        -- Skip this orphan ability
                    else
                        -- Overwrite: new has parent and existing doesn't, or both/neither have parents
                        local link = parentInfo and parentInfo.heroName or name
                        iconDataCache[lowerName] = {
                            name = name,
                            key = abilityKey,
                            link = link,
                            image = name .. ".png",
                            type = "ability",
                            class = "theme"
                        }
                    end
                else
                    -- No existing entry, add normally
                    local link = parentInfo and parentInfo.heroName or name
                    iconDataCache[lowerName] = {
                        name = name,
                        key = abilityKey,
                        link = link,
                        image = name .. ".png",
                        type = "ability",
                        class = "theme"
                    }
                end
            end
         end
         end


         return iconInfo.name
         abilityCacheBuilt = true
     end
 
    -- Only load item data if not already built
     elseif iconType == "item" and not itemCacheBuilt then
        local itemData = mw.loadJsonData("Data:ItemData.json")


    -- 2. Dictionary lookup
        -- Process Items
    if iconInfo.key_dictionary and iconInfo.key_dictionary ~= "" then
        for itemKey, item in pairs(itemData) do
        local entry = dictionary[iconInfo.key_dictionary]
            if type(item) == "table" and item.Name then
                local name = item.Name
                local lowerName = name:lower()


        if type(entry) == "table" then
                iconDataCache[lowerName] = {
            return entry[langCode]
                    name = name,
                 or entry.en
                    key = itemKey,
                or iconInfo.name
                    link = name,
                    image = name .. ".png",
                    type = "item"
                 }
            end
         end
         end
    end


    -- No valid key > fallback
         itemCacheBuilt = true
    return iconInfo.name
end
 
-- Infer link from type and available fields
local function getLink(iconInfo, langCode)
    if iconInfo.type == "ability" then
        local hero = iconInfo.hero_name or iconInfo.name
       
        -- Get the translated name for the specific ability
        local abilityName = getTranslatedName(iconInfo, langCode)
       
        -- Format for URL: replace spaces with underscores
         local anchor = abilityName:gsub(" ", "_")
       
        return hero .. "#" .. anchor
     end
     end
    return linkOverrides[iconInfo.name] or iconInfo.name
end
end


-- Returns the canonical display name for a given user input
-- Get icon data by name and type (with alias support)
-- Used by templates to store the correct casing in data attributes
local function getIconData(iconName, iconType)
-- Normalizes casing so exact string matches work reliably in Lua modules
     buildIconData(iconType)
function p.getCanonicalName(frame)
     local lowerName = iconName:lower()
     local name = frame.args[1] or ""
     local lowerName = name:lower()


     if renames[lowerName] then
     -- Check main lookup
        lowerName = renames[lowerName]
     if iconDataCache[lowerName] then
     elseif aliases[lowerName] then
         return iconDataCache[lowerName], nil
         lowerName = aliases[lowerName]
     end
     end


     local iconInfo = iconData[lowerName] or legacyItems[lowerName]
    -- Check aliases (for edge cases like "doorman" -> "the doorman")
     local aliases = {
        ["doorman"] = "the doorman",
        ["mo and krill"] = "mo & krill",
        ["mo"] = "mo & krill",
        ["krill"] = "mo & krill",
        ["curse"] = "cursed relic",
        ["debuff remover"] = "dispel magic"
    }


    -- If unknown, return original input
     if aliases[lowerName] then
     if not iconInfo then
         return iconDataCache[aliases[lowerName]], nil
         return name
     end
     end


-- Quick hack: ability names are used by MediaWiki:Gadget-infobox-tooltip.js and expected to be localized
    return nil, string.format("[[:Category:Module:Icon ERROR|Icon not found]] ('%s'). [[Category:Module:Icon ERROR]]", iconName)
    local langCode = getLangCode()
end


     if iconInfo.type == "ability" then
-- Language handling
         return getTranslatedName(iconInfo, langCode)
local function getLangData()
     if cachedLangData then
         return cachedLangCode, cachedLangData
     end
     end


     return iconInfo.name
     local title = mw.title.getCurrentTitle().fullText
end
    local subpageLang = title:match("/([^/]+)$")
    local langCode = (subpageLang and lang_codes[subpageLang]) and subpageLang or "en"


local function render(name, args)
    local langData = {}
     local originalName = name
     if langCode ~= "en" then
    local lowerName = name:lower()
        local success, data = pcall(function()
     args = args or {}
            return mw.loadJsonData("Data:Lang " .. langCode .. ".json")
        end)
        if success and type(data) == "table" then
            langData = data
        end
     end


     if renames[lowerName] then
     cachedLangCode = langCode
        lowerName = renames[lowerName]
     cachedLangData = langData
     elseif aliases[lowerName] then
        lowerName = aliases[lowerName]
    end


     -- Lookup live data > legacy fallback
     return langCode, langData
    local iconInfo = iconData[lowerName] or legacyItems[lowerName]
end


    -- Throw error if not found
-- Main render function
    if not iconInfo then
function p.render(frame)
        return string.format("[[:Category:Module:Icon ERROR|Icon not found]] ('%s'). [[Category:Module:Icon ERROR]]", name)
    local args = frame:getParent().args
     end
    local name = args[1] or ""
    local customText = args.l1 or ""
     local iconType = args.type or "item"


     local customText = args.l1 ~= "" and args.l1 or nil
     -- Set Defaults
    local customLink = args.link ~= "" and args.link or nil
     local size = args.size or "20px"
     local size = args.size or "20px"
     local noLink = args["no-link"] == "true"
     local noLink = args["no-link"] == "true"
    local noFile = args["no-file"] == "true"
     local iconOnly = args["icon-only"] == "true"
     local iconOnly = args["icon-only"] == "true"


    -- Scan unnamed parameters
     for k, v in pairs(args) do
     for k, v in pairs(args) do
         if type(k) == "number" and k > 1 then
         if type(k) == "number" and k > 1 then
             local val = mw.text.trim(v)
             local val = mw.text.trim(v)
             if val == "icon-only" then
             if val == "icon-only" then
                 iconOnly = true
                 iconOnly = true
Line 170: Line 226:
     end
     end


     local langCode = getLangCode()
    -- Get icon data automatically from JSON, using type to limit which JSON is loaded
    local iconData, err = getIconData(name, iconType)
    if not iconData then
        return "Error: " .. err
    end
 
     local langCode, langData = getLangData()


     local displayName
    -- Determine display name
     if customText then
     local displayName = ""
     if customText ~= "" then
         displayName = customText
         displayName = customText
     elseif renames[name:lower()] then
     elseif iconData.key and langData[iconData.key] then
        -- Preserve original if renamed
         displayName = langData[iconData.key]
         displayName = originalName
     else
     else
         displayName = getTranslatedName(iconInfo, langCode)
         displayName = iconData.name
    end
 
    local baseLink = customLink or getLink(iconInfo, langCode)
    local fragment = ""
 
    -- Split the fragment (#Abilities) from the base link
    if baseLink:find("#") then
        local parts = mw.text.split(baseLink, "#", true)
        baseLink = parts[1]
        fragment = "#" .. (parts[2] or "")
     end
     end


    -- Localize link if on translated subpage
    local baseLink = iconData.link or ""
     local link = baseLink
     local link = baseLink


     if not customLink and langCode ~= "en" and baseLink ~= "" then
     if langCode ~= "en" and baseLink ~= "" then
         link = baseLink .. "/" .. langCode
         link = baseLink .. "/" .. langCode
     end
     end


     -- Reattach fragment at the end
     -- Style logic (only abilities get theme class by default)
     link = link .. fragment
     local class = iconData.class or ""
 
    -- Add CSS class for abilities
     local style
     local style
     if iconInfo.type == "ability" then
     if class:find("theme", 1, true) then
         style = 'class="module-icon-ability" style="position:relative; bottom:2px;"'
         style = 'class="module-icon-ability" style="position:relative; bottom:2px;"'
     else
     else
         style = 'class="c-icon" style="position:relative; bottom:2px;"'
         style = 'style="position:relative; bottom:2px;"'
     end
     end


    -- Generate icon HTML
     local imageLink = noLink and "" or link
     local imageLink = noLink and "" or link
     local iconHtml = ""
     local iconHtml = string.format('<span %s>[[File:%s|%s|link=%s]]</span>', style, iconData.image, size, imageLink)
 
    if not noFile then
        local image = getImage(iconInfo, args)
        iconHtml = string.format('<span %s>[[File:%s|%s|link=%s]]</span>', style, image, size, imageLink)
    end


     if iconOnly or displayName == "" then
     if iconOnly or displayName == "" then
Line 222: Line 270:


     local textHtml = (noLink or link == "") and displayName or string.format('[[%s|%s]]', link, displayName)
     local textHtml = (noLink or link == "") and displayName or string.format('[[%s|%s]]', link, displayName)
 
     return '<span style="white-space:nowrap;">' .. iconHtml .. " " .. textHtml .. '</span>'
     if iconHtml ~= "" then
        return '<span style="white-space:nowrap;">' .. iconHtml .. " " .. textHtml .. '</span>'
    else
        return textHtml
    end
end
 
-- Template entrypoint
function p.render(frame)
    local args = frame:getParent().args
    local name = args[1] or ""
 
    return render(name, args)
end
 
-- Module entrypoint
function p._render(name, args)
    return render(name, args)
end
end


return p
return p
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

Pages included on this page: