Anonymous

Module:Icons: Difference between revisions

From Melvor Idle
Icon: Modify link logic to prevent linking to File pages when nolink is specified
(Add extension overrides for v1.0.2)
(Icon: Modify link logic to prevent linking to File pages when nolink is specified)
(10 intermediate revisions by 2 users not shown)
Line 3: Line 3:


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


local p = {}
local p = {}
Line 270: Line 273:
["Stonewall Shield"] = 'png',
["Stonewall Shield"] = 'png',
["Impossible Longbow"] = 'png',
["Impossible Longbow"] = 'png',
["Throwing Dragon Sword"] = 'png'
["Throwing Dragon Sword"] = 'png',
["Golbin Crate"] = 'png',
["Bone"] = 'png'
}
}


Line 303: Line 308:
['Bane, Instrument of Fear'] = 'Bane',
['Bane, Instrument of Fear'] = 'Bane',
['Mastery Token Astrology'] = 'Mastery Token (Astrology)',
['Mastery Token Astrology'] = 'Mastery Token (Astrology)',
['Event Token - Holiday 2021'] = 'Christmas Present (Yellow)'
['Event Token - Holiday 2021'] = 'Christmas Present (Yellow)',
["I Can't See Helmet"] = {'Golbin', 'monster'},
-- Overrides that allow generic potion pages to be linked to easily
['Melee Accuracy Potion'] = {'Melee Accuracy Potion I', 'item'},
['Melee Evasion Potion'] = {'Melee Evasion Potion I', 'item'},
['Ranged Assistance Potion'] = {'Ranged Assistance Potion I', 'item'},
['Hinder Potion'] = {'Hinder Potion I', 'item'},
['Magic Assistance Potion'] = {'Magic Assistance Potion I', 'item'},
['Regeneration Potion'] = {'Regeneration Potion I', 'item'},
['Famished Potion'] = {'Famished Potion I', 'item'},
['Ranged Strength Potion'] = {'Ranged Strength Potion I', 'item'},
['Lucky Herb Potion'] = {'Lucky Herb Potion I', 'item'},
['Divine Potion'] = {'Divine Potion I', 'item'},
['Melee Strength Potion'] = {'Melee Strength Potion I', 'item'},
['Magic Damage Potion'] = {'Magic Damage Potion I', 'item'},
['Lethal Toxins Potion'] = {'Lethal Toxins Potion I', 'item'},
['Diamond Luck Potion'] = {'Diamond Luck Potion I', 'item'},
['Damage Reduction Potion'] = {'Damage Reduction Potion I', 'item'},
['Bird Nest Potion'] = {'Bird Nest Potion I', 'item'},
['Controlled Heat Potion'] = {'Controlled Heat Potion I', 'item'},
['Generous Cook Potion'] = {'Generous Cook Potion I', 'item'},
['Fishermans Potion'] = {'Fishermans Potion I', 'item'},
['Skilled Fletching Potion'] = {'Skilled Fletching Potion I', 'item'},
['Gentle Hands Potion'] = {'Gentle Hands Potion I', 'item'},
['Secret Stardust Potion'] = {'Secret Stardust Potion I', 'item'},
['Crafting Potion'] = {'Crafting Potion I', 'item'},
['Perfect Swing Potion'] = {'Perfect Swing Potion I', 'item'},
['Necromancer Potion'] = {'Necromancer Potion I', 'item'},
['Performance Enhancing Potion'] = {'Performance Enhancing Potion I', 'item'},
['Elemental Potion'] = {'Elemental Potion I', 'item'},
['Herblore Potion'] = {'Herblore Potion I', 'item'},
['Generous Harvest Potion'] = {'Generous Harvest Potion I', 'item'},
['Seeing Gold Potion'] = {'Seeing Gold Potion I', 'item'}
}
}
--When calling for an icon for Key, the link goes to Value
--When calling for an icon for Key, the link goes to Value
Line 327: Line 364:
}
}
--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 343: Line 386:
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 hasText = (text ~= nil and text ~= '')
elseif type(ovr) == 'table' then
if ovrImg ~= nil and img == link then
img = ovr[1]
if type(ovrImg) == 'string' then
if ovr[2] ~= nil then iconType = ovr[2] end
img = ovrImg
elseif type(ovrImg) == 'table' then
img = ovrImg[1]
if ovrImg[2] ~= nil then iconType = ovrImg[2] end
end
end
end
end
if (text == nil or text == '') and txtOverrides[link] ~= nil then
-- Type & extension overrides must be after adjustments have been made
text = txtOverrides[link]
-- for any image overrides
local ovrTypeExt = typeExtOverrides[iconType]
if ovrTypeExt ~= nil then
ovrTypeExt = ovrTypeExt[img]
end
end
if linkOverrides[link] ~= nil then
local ovrExt = extOverrides[img]
if (text == nil or text == '') then text = link end
local ovrType = typeOverrides[iconType]
link = linkOverrides[link]
if not hasText and ovrTxt ~= nil then
text = ovrTxt
hasText = true
end
if ovrLink ~= nil then
if not hasText then
text = link
hasText = true
end
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
hasText = true
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 = '|link=' .. (nolink and notext and '' or 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 482: Line 532:


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