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")
local iconData = mw.loadJsonData("Data:ResourceLookup.json")
local iconData2 = mw.loadJsonData("Data:ResourceLookup2.json")
local lang_codes = mw.loadJsonData("Data:LangCodes.json")
local dictionary = mw.loadJsonData("Data:Dictionary")
local lang_module = require('Module:Lang')


local iconData = {}
local cachedLangCode


for k, v in pairs(iconDataRaw) do
local linkOverrides = {
     iconData[k] = v
    ["Bullet Lifesteal"] = "Bullet Lifesteal (item)",
end
     ["Spirit Lifesteal"] = "Spirit Lifesteal (item)"
}


for k, v in pairs(iconData2) do
local legacyItems = {
     iconData[k] = v
    ["ammo scavenger"] = { name = "Ammo Scavenger", type = "item" },
end
     ["enduring spirit"] = { name = "Enduring Spirit", type = "item" },
    ["bullet armor"]    = { name = "Bullet Armor", type = "item" },
    ["spirit armor"]    = { name = "Spirit Armor", type = "item" },
    ["soul explosion"]  = { name = "Soul Explosion", type = "item" },
    ["soul rebirth"]    = { name = "Soul Rebirth", type = "item" },
}


local lang_codes = mw.loadJsonData("Data:LangCodes.json")
local missingImageOverrides = {
local lang_module = require("Module:Lang")
    ["boho"] = "Module-Icon missing.png",
local table_data = require("Module:Icon/data")
    ["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",
    ["bullet armor"] = "Bullet Armor (item).png",
    ["spirit armor"] = "Spirit Armor (item).png",
}


local cachedLangCode
local aliases = {
 
    ["doorman"] = "the doorman",
local linkOverrides = table_data.linkOverrides
    ["mo and krill"] = "mo & krill",
local legacyItems = table_data.legacyItems
    ["mo"] = "mo & krill",
local missingImageOverrides = table_data.missingImageOverrides
    ["krill"] = "mo & krill",
local aliases = table_data.aliases
    ["curse"] = "cursed relic",
local renames = table_data.renames
    ["debuff remover"] = "dispel magic",
    ["defend and fight!"] = "plot armor",
    ["conjure dragon"] = "bookwyrm",
    ["backstabber"] = "stalker",
    ["flying strike"] = "flying slash",
    ["tornado"] = "dust devil",
    ["kudzu bomb"] = "entangling thorns",
    ["watcher's covenant"] = "kudzu connection"
}


local function getLangCode()
local function getLangCode()
Line 41: Line 64:


-- Infer image path from name, with override support for missing images
-- Infer image path from name, with override support for missing images
local function getImage(iconInfo, args)
local function getImage(iconInfo)
     local lowerName = iconInfo.name:lower()
     local lowerName = iconInfo.name:lower()
    return missingImageOverrides[lowerName] or (iconInfo.name .. ".png")
end


     if args.image then
-- Infer link from type and available fields
         return args.image
local function getLink(iconInfo)
     if iconInfo.type == "ability" then
        local hero = iconInfo.hero_name or iconInfo.name
         return hero .. "#Abilities"
     end
     end
 
     return linkOverrides[iconInfo.name] or iconInfo.name
     return missingImageOverrides[lowerName] or (iconInfo.name .. ".png")
end
end


-- Get the translated name
local function getTranslatedName(iconInfo, langCode)
local function getTranslatedName(iconInfo, langCode)
     if langCode == "en" then
     if langCode == "en" or not iconInfo.key then
        return iconInfo.name
    end
 
    -- 1. Direct key
    if iconInfo.key and iconInfo.key ~= "" then
        local translated = lang_module.get_string(iconInfo.key, langCode, iconInfo.name
        )
 
        if type(translated) == "string" then
            return (translated:gsub("<[^>]+>", ""))
        end
 
         return iconInfo.name
         return iconInfo.name
     end
     end
 
      
     -- 2. Dictionary lookup
     local translatedName = lang_module.get_string(iconInfo.key, langCode, iconInfo.name)
     if iconInfo.key_dictionary and iconInfo.key_dictionary ~= "" then
   
        local entry = dictionary[iconInfo.key_dictionary]
    if type(translatedName) == "string" then
 
        translatedName = translatedName:gsub("<[^>]+>", "")
        if type(entry) == "table" then
            return entry[langCode]
                or entry.en
                or iconInfo.name
        end
     end
     end
 
      
    -- No valid key > fallback
     return translatedName or iconInfo.name
    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
 
     return linkOverrides[iconInfo.name] or iconInfo.name
end
end


-- Returns the canonical display name for a given user input
-- Returns the canonical display name for a given user input.
-- Used by templates to store the correct casing in data attributes
-- Used by templates to store the correct casing in data attributes
-- Normalizes casing so exact string matches work reliably in Lua modules
-- so that Lua modules which do exact string matching receive the right value.
function p.getCanonicalName(frame)
function p.getCanonicalName(frame)
     local name = frame.args[1] or ""
     local name = frame.args[1] or ""
     local lowerName = name:lower()
     local lowerName = name:lower()


     if renames[lowerName] then
     if aliases[lowerName] then
        lowerName = renames[lowerName]
    elseif aliases[lowerName] then
         lowerName = aliases[lowerName]
         lowerName = aliases[lowerName]
     end
     end


     local iconInfo = iconData[lowerName] or legacyItems[lowerName]
     local iconInfo = iconData[lowerName]
 
    -- If unknown, return original input
     if not iconInfo then
     if not iconInfo then
         return name
         iconInfo = legacyItems[lowerName]
     end
     end


-- Quick hack: ability names are used by MediaWiki:Gadget-infobox-tooltip.js and expected to be localized
     if not iconInfo then
    local langCode = getLangCode()
         return name -- fall back to original input if not found
 
     if iconInfo.type == "ability" then
         return getTranslatedName(iconInfo, langCode)
     end
     end


Line 132: Line 116:


local function render(name, args)
local function render(name, args)
    local originalName = name
     local lowerName = name:lower()
     local lowerName = name:lower()
     args = args or {}
     args = args or {}


     if renames[lowerName] then
     if aliases[lowerName] then
        lowerName = renames[lowerName]
    elseif aliases[lowerName] then
         lowerName = aliases[lowerName]
         lowerName = aliases[lowerName]
     end
     end


     -- Lookup live data > legacy fallback
     -- 1. Check live game data
     local iconInfo = iconData[lowerName] or legacyItems[lowerName]
     local iconInfo = iconData[lowerName]
   
    -- 2. If not found, check legacy/unused items
    if not iconInfo then
        iconInfo = legacyItems[lowerName]
    end


     -- Throw error if not found
     -- 3. If still not found, throw error
     if not iconInfo then
     if not iconInfo then
         return string.format("[[:Category:Module:Icon ERROR|Icon not found]] ('%s'). [[Category:Module:Icon ERROR]]", name)
         return string.format("[[:Category:Module:Icon ERROR|Icon not found]] ('%s'). [[Category:Module:Icon ERROR]]", name)
     end
     end


     local customText = args.l1 ~= "" and args.l1 or nil
     local customText = args.l1 or name
    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"


Line 160: Line 144:
         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
             elseif val == "no-link" then noLink = true
             elseif val == "no-link" then
             elseif val:match("^%d+px$") then size = val end
                noLink = true
             elseif val:match("^%d+px$") then
                size = val
            end
         end
         end
     end
     end
Line 172: Line 152:
     local langCode = getLangCode()
     local langCode = getLangCode()


     local displayName
     local displayName = customText
     if customText then
     if displayName == name then
        displayName = customText
    elseif renames[name:lower()] then
        -- Preserve original if renamed
        displayName = originalName
    else
         displayName = getTranslatedName(iconInfo, langCode)
         displayName = getTranslatedName(iconInfo, langCode)
     end
     end


     local baseLink = customLink or getLink(iconInfo, langCode)
     local baseLink = getLink(iconInfo)
     local fragment = ""
     local fragment = ""


Line 193: Line 168:


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


     -- Reattach fragment at the end
     -- Re-attach the fragment at the very end
     link = link .. fragment
     link = link .. fragment


     -- Add CSS class for abilities
     -- Abilities get the module-icon-ability class for light/dark theme filter support
     local style
     local style
     if iconInfo.type == "ability" then
     if iconInfo.type == "ability" 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


    local image = getImage(iconInfo)
     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, 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 192:


     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
end


Line 240: Line 205:
-- Module entrypoint
-- Module entrypoint
function p._render(name, args)
function p._render(name, args)
    return 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: