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 | -- Load icon data and language codes | ||
local | local iconData = require("Module:Icon/data") | ||
local lang_codes = mw.loadJsonData("Data:LangCodes.json") | |||
local | -- Per-page cache | ||
local cachedLangCode, cachedLangData | |||
-- Get icon data by name | |||
iconData[ | 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 | end | ||
-- Get cached or loaded language code and JSON data | |||
local function getLangData() | |||
if cachedLangData then | |||
return cachedLangCode, cachedLangData | |||
local function | |||
if | |||
return cachedLangCode | |||
end | end | ||
| Line 36: | Line 27: | ||
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) | |||
local | if success and type(data) == "table" then | ||
langData = data | |||
end | |||
end | end | ||
end | end | ||
cachedLangCode = langCode | |||
cachedLangData = langData | |||
return langCode, langData | |||
end | end | ||
-- | -- Main render function | ||
function p.render(frame) | |||
local args = frame:getParent().args | |||
local name = args[1] or "" | |||
local customText = args.l1 or "" | |||
function p. | |||
local | |||
local | |||
local customText = | |||
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" | ||
local iconData, err = getIconData(name) | |||
if not iconData then | |||
return "Error: " .. err | |||
end | end | ||
local langCode = | local langCode, langData = getLangData() | ||
local displayName | -- Determine display name with clear priority | ||
if customText then | local displayName = "" | ||
if customText ~= "" then | |||
displayName = customText | displayName = customText | ||
elseif | elseif iconData.key and langData[iconData.key] then | ||
displayName = langData[iconData.key] | |||
displayName = | elseif iconData.name then | ||
displayName = iconData.name | |||
elseif iconOnly then | |||
displayName = "" | |||
else | else | ||
displayName = | displayName = name -- fallback to input name | ||
end | end | ||
local link = baseLink | -- 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 extra = (class:find("invert", 1, true) and " filter:invert(1);" or "") | |||
local style | local style | ||
if | 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;' .. extra .. '"' | ||
else | else | ||
style = ' | style = 'style="position:relative; bottom:2px;' .. extra .. '"' | ||
end | end | ||
local iconHtml = string.format('<span %s>[[File:%s|%s|link=]]</span>', style, iconData.image, size) | |||
local | |||
if iconOnly or displayName == "" then | if iconOnly or displayName == "" then | ||
| Line 222: | Line 94: | ||
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 | ||