Module:Icons: Difference between revisions

Icon: Optimize, reduces runtime by ~30%
m (Added a missing potion override)
(Icon: Optimize, reduces runtime by ~30%)
Line 3: Line 3:


local Shared = require("Module:Shared")
local Shared = require("Module:Shared")
-- Locally index some functions for performance
local sgsub, sformat, fixPagename, formatnum, tostring, type = string.gsub, string.format, Shared.fixPagename, Shared.formatnum, tostring, type


local p = {}
local p = {}
Line 360: Line 363:
}
}
--Ambiguous overrides section
--Ambiguous overrides section
local ambiguousOverrides = {'Golbin', 'Chick', 'Chicken', 'Cyclops', 'Wizard'}
local ambiguousOverrides = {
['Golbin'] = true,
['Chick'] = true,
['Chicken'] = true,
['Cyclops'] = true,
['Wizard'] = true
}


function p.Icon(frame)
function p.Icon(frame)
Line 376: Line 385:
local img = args.img ~= nil and args.img ~= '' and args.img or link
local img = args.img ~= nil and args.img ~= '' and args.img or link


link = Shared.fixPagename(link)
link = fixPagename(link)
img = Shared.fixPagename(img)
img = fixPagename(img)
 
img = sgsub(img, '#', '')
img = sgsub(img, '/Training', '')
img = sgsub(img, '/Guide', '')
link = sgsub(link, '#', '')


img = string.gsub(img, '#', '')
img = string.gsub(img, '/Training', '')
img = string.gsub(img, '/Guide', '')
link = string.gsub(link, '#', '')
--MANUAL OVERRIDES
--MANUAL OVERRIDES
if imgOverrides[link] ~= nil and img == link then
local ovrImg = imgOverrides[link]
local ovr = imgOverrides[link]
local ovrTxt = txtOverrides[link]
if type(ovr) == 'string' then
local ovrLink = linkOverrides[link]
img = imgOverrides[link]
local ovrTypeExt = typeExtOverrides[iconType]
elseif type(ovr) == 'table' then
if ovrTypeExt ~= nil then
ovrTypeExt = ovrTypeExt[img]
end
local ovrExt = extOverrides[img]
local ovrType = typeOverrides[iconType]
local hasText = (text ~= nil and text ~= '')
if ovrImg ~= nil and img == link then
if type(ovrImg) == 'string' then
img = ovrImg
elseif type(ovrImg) == 'table' then
img = ovr[1]
img = ovr[1]
if ovr[2] ~= nil then iconType = ovr[2] end
if ovr[2] ~= nil then iconType = ovr[2] end
end
end
end
end
if (text == nil or text == '') and txtOverrides[link] ~= nil then
if not hasText and ovrTxt ~= nil then
text = txtOverrides[link]
text = ovrTxt
end
end
if linkOverrides[link] ~= nil then
if ovrLink ~= nil then
if (text == nil or text == '') then text = link end
if not hasText then text = link end
link = linkOverrides[link]
link = ovrLink
end
end
if typeExtOverrides[iconType] ~= nil and typeExtOverrides[iconType][img] ~= nil then
if ovrTypeExt ~= nil then
ext = typeExtOverrides[iconType][img]
ext = ovrTypeExt
elseif extOverrides[img] ~= nil then  
elseif ovrExt ~= nil then
ext = extOverrides[img]
ext = ovrExt
elseif iconType == 'mark' then
elseif iconType == 'mark' then
ext = 'png'
ext = 'png'
end
end
if typeOverrides[iconType] ~= nil then
if ovrType ~= nil then
iconType = typeOverrides[iconType]
iconType = ovrType
end
end
--There are a couple specific double overrides I want to include that don't fit in the above lists
-- There are a couple specific double overrides to be included that don't fit in the above lists
if Shared.contains(ambiguousOverrides, link) then
if ambiguousOverrides[link] then
if (text == nil or text == '') then text = link end
if not hasText then text = link end
link = link..' ('..(iconType == 'mark' and 'item' or iconType)..')'
link = link..' ('..(iconType == 'mark' and 'item' or iconType)..')'
end
end


local result = ''
local resultText = ''
if not noicon then
if not noicon then
--Creating the image...
-- Image size
result = '[[File:'..img
local txtSize = tostring(imgSize)
--Add in type if one is set
-- Extension and type if one is set
if iconType ~= nil and iconType ~= '' then result = result..'_('..iconType..')' end
local txtImg = (iconType ~= nil and iconType ~= '' and img .. '_(' .. iconType .. ')' or img) .. '.' .. ext
--Add in extension and image size
-- Include a link unless no link -and- no text was requested
result = result..'.'..ext..'|'..tostring(imgSize)..'x'..tostring(imgSize)..'px'
local txtLink = (nolink and notext and '' or '|link=' .. link)
--Include a link unless no link -and- no text was requested
resultText = '[[File:' .. txtImg .. '|' .. txtSize .. 'x' .. txtSize .. 'px' .. txtLink .. ']]'
if not (nolink and notext) then result = result..'|link='..link end
result = result..']]'
end
end
--Add Quantity to the front if requested
if qty ~= nil and qty ~= '' then result = Shared.formatnum(qty)..' '..result end


if not notext then
if not notext then
-- If the result is currently empty then there's no need to pad with whitespace
local padStr = (string.len(result) > 0 and ' ' or '')
if nolink then
if nolink then
if text ~= nil and text ~= '' then
resultText = resultText .. (noicon and '' or ' ') .. (hasText and text or link)
result = result..padStr..text
else
result = result..padStr..link
end
else
else
result = result..padStr..'[['..link
resultText = resultText .. (noicon and '' or ' ') .. '[[' .. link .. (hasText and text ~= link and '|' .. text or '') .. ']]'
if text ~= nil and text ~= '' and text ~= link then
result = result..'|'..text
end
result = result..']]'
end
end
end
end


if not noicon then
-- Add Quantity to the front if requested
result = '<span style="display:inline-block">'..result..'</span>'
--local resultQty = (qty ~= nil and qty ~= '' and formatnum(qty) .. ' ' or '')
if qty ~= nil and qty ~= '' then
resultText = formatnum(qty) .. ' ' .. resultText
end
end


if menu then
if menu then
result = '{| class="articletable" style="display:inline-block;vertical-align:middle;"\r\n|-\r\n|'..result
return '{| class="articletable" style="display:inline-block;vertical-align:middle;"\r\n|-\r\n|' .. resultText .. '\r\n|}'
result = result..'\r\n|}'
elseif not noicon then
return '<span style="display:inline-block">' .. resultText .. '</span>'
else
return resultText
end
end
return result
end
end


Line 515: Line 522:


function p._Currency(fileName, link, amt, maxAmt)
function p._Currency(fileName, link, amt, maxAmt)
local amtText = Shared.formatnum(amt)
local amtText = formatnum(amt)
if maxAmt ~= nil and maxAmt >= amt then
if maxAmt ~= nil and maxAmt >= amt then
amtText = amtText .. ' - ' .. Shared.formatnum(maxAmt)
amtText = amtText .. ' - ' .. formatnum(maxAmt)
end
end