Module:Icon: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
LVL (talk | contribs)
Undo revision 36291 by LVL (talk)
Tag: Undo
Put the "nowrap" around the icon and text – not just icon, it doesn't do anything that way
Line 72: Line 72:
     -- Style logic
     -- Style logic
     local class = iconData.class or ""
     local class = iconData.class or ""
     local style = 'style="white-space:nowrap; position:relative; bottom:2px;"'
     local style = 'style="position:relative; bottom:2px;"'
     if class == "theme" then
     if class == "theme" then
         style = 'class="module-icon-ability" style="white-space:nowrap; position:relative; bottom:2px;"'
         style = 'class="module-icon-ability" style="position:relative; bottom:2px;"'
     end
     end


Line 85: Line 85:


     local textHtml = (noLink or link == "") and finalText or string.format('[[%s|%s]]', link, finalText)
     local textHtml = (noLink or link == "") and finalText or string.format('[[%s|%s]]', link, finalText)
     return iconHtml .. " " .. textHtml
     return '<span style="white-space:nowrap;">' .. iconHtml .. " " .. textHtml .. '</span>'
end
end


return p
return p

Revision as of 19:38, 6 October 2025

Main module for rendering game icons.

Note: This module is not intended for direct use. Use the templates below.

Data sources

The module reads from Data:ResourceLookup.json, a pre-indexed lookup table generated by Deadbot at deploy time. When the bot uploads new game files, hero, ability, and item icons update automatically.

An additional resource for other icons can be found at Data:ResourceLookup2.json, a manually maintained table; some entries may rely on a dictionary key.

See Module:Icon/data for manual overrides, legacy items, aliases, and renames used by this module.

Usage

Any of the following templates can be used. The module automatically detects the icon type:

Note: While all four templates work identically, it is recommended to use the appropriate one for clarity (e.g., use {{AbilityIcon|Siphon Life}} even though {{HeroIcon|Siphon Life}} will also work).

Error tracking

Icons that fail to render are added to Category:Module:Icon ERROR.


local p = {}

-- Load icon data and language codes
local iconData = require("Module:Icon/data")
local lang_codes = mw.loadJsonData("Data:LangCodes.json")

-- Per-page cache
local cachedLangCode, cachedLangData

-- Get icon data by name
local function getIconData(iconName)
    local entry = iconData[iconName:lower()]
    if entry then
        return entry, nil
    end
    return nil, string.format("[[:Category:Module:Icon ERROR|Icon not found]] ('%s'). [[Category:Module:Icon ERROR]]", iconName)
end

-- Get cached or loaded language code and JSON data
local function getLangData()
    if cachedLangData then
        return cachedLangCode, cachedLangData
    end

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

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

    cachedLangCode = langCode
    cachedLangData = langData

    return langCode, langData
end

-- Main render function
function p.render(frame)
    local args = frame:getParent().args
    local name = args[1] or ""
    local customText = args.l1 or ""
    local size = args.size or "20px"
    local noLink = args["no-link"] == "true"
    local iconOnly = args["icon-only"] == "true"

    local iconData, err = getIconData(name)
    if not iconData then
        return "Error: " .. err
    end

    local langCode, langData = getLangData()
    local hasOnlyImage = iconData.image and iconData.image ~= "" and not iconData.name and not iconData.key and (iconData.link == nil or iconData.link == "") and customText == ""

    -- Use translation if available
	local displayName = (iconData.name or (not hasOnlyImage and name)) or ""
	if iconData.key and langData[iconData.key] then
    	displayName = langData[iconData.key]
	end

    -- Localize link if on a translated subpage
    local baseLink = iconData.link or ""
    local link = (langCode ~= "en" and baseLink ~= "") and (baseLink .. "/" .. langCode) or baseLink

    -- Style logic
    local class = iconData.class or ""
    local style = 'style="position:relative; bottom:2px;"'
    if class == "theme" then
        style = 'class="module-icon-ability" style="position:relative; bottom:2px;"'
    end

    local finalText = (customText ~= "") and customText or displayName
    local iconHtml = string.format('<span %s>[[File:%s|%s|link=]]</span>', style, iconData.image, size)

	if iconOnly or displayName == "" then
    	return iconHtml
	end

    local textHtml = (noLink or link == "") and finalText or string.format('[[%s|%s]]', link, finalText)
    return '<span style="white-space:nowrap;">' .. iconHtml .. " " .. textHtml .. '</span>'
end

return p