Editing Module:IconGive feedback
Jump to navigation
Jump to search
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 | local iconData = mw.loadJsonData("Data:ResourceLookup.json") | ||
local | local lang_codes = mw.loadJsonData("Data:LangCodes.json") | ||
local | local cachedLangCode, cachedLangData | ||
local linkOverrides = { | |||
["Bullet Lifesteal"] = "Bullet Lifesteal (item)", | |||
["Spirit Lifesteal"] = "Spirit Lifesteal (item)" | |||
} | |||
local legacyItems = { | |||
["ammo scavenger"] = { name = "Ammo Scavenger", type = "item" }, | |||
["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 | local missingImageOverrides = { | ||
["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", | |||
["bullet armor"] = "Bullet Armor (item).png", | |||
["spirit armor"] = "Spirit Armor (item).png", | |||
} | |||
local | local aliases = { | ||
["doorman"] = "the doorman", | |||
["mo and krill"] = "mo & krill", | |||
["mo"] = "mo & krill", | |||
["krill"] = "mo & krill", | |||
["curse"] = "cursed relic", | |||
["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 getLangData() | |||
if cachedLangData then | |||
return cachedLangCode, cachedLangData | |||
local function | |||
if | |||
return cachedLangCode | |||
end | end | ||
| Line 35: | Line 57: | ||
local subpageLang = title:match("/([^/]+)$") | local subpageLang = title:match("/([^/]+)$") | ||
local langCode = (subpageLang and lang_codes[subpageLang]) and subpageLang or "en" | 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 | cachedLangCode = langCode | ||
return langCode | cachedLangData = langData | ||
return langCode, langData | |||
end | end | ||
-- 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 | local function getImage(iconInfo) | ||
local lowerName = iconInfo.name:lower() | local lowerName = iconInfo.name:lower() | ||
return missingImageOverrides[lowerName] or (iconInfo.name .. ".png") | return missingImageOverrides[lowerName] or (iconInfo.name .. ".png") | ||
end | end | ||
-- Infer link from type and available fields | -- Infer link from type and available fields | ||
local function getLink(iconInfo | local function getLink(iconInfo) | ||
if iconInfo.type == "ability" then | if iconInfo.type == "ability" then | ||
local hero = iconInfo.hero_name or iconInfo.name | local hero = iconInfo.hero_name or iconInfo.name | ||
return hero .. "#Abilities" | |||
return hero .. "#" | |||
end | end | ||
return linkOverrides[iconInfo.name] or iconInfo.name | 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 | ||
-- | -- 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 | if aliases[lowerName] then | ||
lowerName = aliases[lowerName] | lowerName = aliases[lowerName] | ||
end | end | ||
local iconInfo = iconData[lowerName] | local iconInfo = iconData[lowerName] | ||
if not iconInfo then | if not iconInfo then | ||
iconInfo = legacyItems[lowerName] | |||
end | end | ||
if not iconInfo then | |||
return name -- fall back to original input if not found | |||
if iconInfo | |||
return | |||
end | end | ||
| Line 131: | Line 111: | ||
end | end | ||
function p.render(frame) | |||
local | local args = frame:getParent().args | ||
local name = args[1] or "" | |||
local lowerName = name:lower() | local lowerName = name:lower() | ||
if | if aliases[lowerName] then | ||
lowerName = aliases[lowerName] | lowerName = aliases[lowerName] | ||
end | end | ||
-- | -- 1. Check live game data | ||
local iconInfo = iconData[lowerName] | local iconInfo = iconData[lowerName] | ||
-- 2. If not found, check legacy/unused items | |||
if not iconInfo then | |||
iconInfo = legacyItems[lowerName] | |||
end | |||
-- | -- 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 = | local customText = args.l1 or "" | ||
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 iconOnly = args["icon-only"] == "true" | local iconOnly = args["icon-only"] == "true" | ||
| Line 160: | Line 141: | ||
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 | ||
elseif val == "no-link" then noLink = true | |||
elseif val == "no-link" then | elseif val:match("^%d+px$") then size = val end | ||
elseif val:match("^%d+px$") then | |||
end | end | ||
end | end | ||
local langCode = | local langCode, langData = getLangData() | ||
local displayName | local displayName = customText | ||
if | if displayName == "" then | ||
if iconInfo.key and langData[iconInfo.key] then | |||
displayName = langData[iconInfo.key] | |||
else | |||
displayName = iconInfo.name | |||
end | |||
end | end | ||
local baseLink = | local baseLink = getLink(iconInfo) | ||
local fragment = "" | local fragment = "" | ||
| Line 193: | Line 169: | ||
local link = baseLink | local link = baseLink | ||
if langCode ~= "en" and baseLink ~= "" then | |||
if | |||
link = baseLink .. "/" .. langCode | link = baseLink .. "/" .. langCode | ||
end | end | ||
-- | -- Re-attach the fragment at the very end | ||
link = link .. fragment | link = link .. fragment | ||
-- | -- 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 = ' | 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 | local iconHtml = string.format('<span %s>[[File:%s|%s|link=%s]]</span>', style, image, size, imageLink) | ||
if iconOnly or displayName == "" then | if iconOnly or displayName == "" then | ||
| Line 222: | Line 193: | ||
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>' | |||
end | end | ||
return p | return p | ||