Module:Inventory slot: Difference between revisions

m Fixed bugs
Remove sprite support. Module:InvSprite has been uploaded as invicons and mod modules don't exist anymore.
Line 6: Line 6:
modLink = 'Mods/$1/$2',
modLink = 'Mods/$1/$2',
moduleAliases = [[Module:Inventory slot/Aliases]],
moduleAliases = [[Module:Inventory slot/Aliases]],
moduleInvData = [[Module:InvSprite]],
moduleModData = 'Module:InvSprite/Mods/$1',
moduleRandom = [[Module:Random]],
moduleRandom = [[Module:Random]],
moduleSprite = [[Module:Sprite]],
-- List of special prefixes which should be handled by
-- List of special prefixes which should be handled by
-- other modules (such as being moved outside links)
-- other modules (such as being moved outside links)
Line 25: Line 22:


local random = require( i18n.moduleRandom ).random
local random = require( i18n.moduleRandom ).random
local sprite = require( i18n.moduleSprite ).sprite
local aliases = mw.loadData( i18n.moduleAliases )
local aliases = mw.loadData( i18n.moduleAliases )
local ids = mw.loadData( i18n.moduleInvData ).ids
local modIds = {}
local pageName = mw.title.getCurrentTitle().text
local pageName = mw.title.getCurrentTitle().text
--[[Splits a given text into fragments separated by semicolons that are not
    inside square brackets. Written by AttemptToCallNil for the Russian wiki
--]]
local function splitOnUnenclosedSemicolons(text)
local semicolon, lbrace, rbrace = (";[]"):byte(1, 3)
local nesting = false
local splitStart = 1
local frameIndex = 1
local frames = {}
for index = 1, text:len() do
local byte = text:byte(index)
if byte == semicolon and not nesting then
frames[frameIndex] = text:sub(splitStart, index - 1)
frameIndex = frameIndex + 1
splitStart = index + 1
elseif byte == lbrace then
assert(not nesting, "Excessive square brackets found")
nesting = true
elseif byte == rbrace then
assert(nesting, "Unbalanced square brackets found")
nesting = false
end
end
assert(not nesting, "Unbalanced square brackets found")
frames[frameIndex] = text:sub(splitStart, text:len())
for index = 1, #frames do
frames[index] = (frames[index]:gsub("^%s+", ""):gsub("%s+$", "")) -- faster mw.text.trim
end
return frames
end


-- Performs a simple recursive clone of a table's values
-- Performs a simple recursive clone of a table's values
Line 76: Line 104:
local description = frame.text
local description = frame.text
local img, idData
local img
if mod then
if mod then
local modData = modIds[mod]
img = i18n.legacyFilename:gsub( '%$1', name .. ' (' .. mod .. ')' )
if not modData and mw.title.new( i18n.moduleModData:gsub( '%$1', mod ) ).exists then
modData = mw.loadData( i18n.moduleModData:gsub( '%$1', mod ) )
modIds[mod] = modData
end
if modData and modData[name] then
idData = modData[name]
else
img = i18n.legacyFilename:gsub( '%$1', name .. ' (' .. mod .. ')' )
end
elseif ids[name] then
idData = ids[name]
elseif name:match( '%.gif$' ) or name:match( '%.png$' ) then
elseif name:match( '%.gif$' ) or name:match( '%.png$' ) then
img = i18n.filename:gsub( '%$1', name )
img = i18n.filename:gsub( '%$1', name )
-- Remove file extension from name
-- Remove file extension from name
name = name:sub( 0, -5 )
name = name:sub( 0, -5 )
else
-- Fall back to an individual image if the sprite is lacking
img = i18n.filename:gsub( '%$1', name .. '.png' )
end
end
Line 132: Line 152:
end
end
elseif link then
elseif link then
if img then
formattedTitle = ''
formattedTitle = ''
else
plainTitle = ''
end
end
end
Line 144: Line 160:
}
}
if img then
-- & is re-escaped because mw.html treats attributes
-- & is re-escaped because mw.html treats attributes
-- as plain text, but MediaWiki doesn't
-- as plain text, but MediaWiki doesn't
local escapedTitle = ( plainTitle or '' ):gsub( '&', '&' )
local escapedTitle = ( plainTitle or '' ):gsub( '&', '&' )
local altText = img .. ': Inventory sprite for ' .. name .. ' in Minecraft as shown in-game'
item:addClass( 'invslot-item-image' )
if link then
:wikitext( '[[File:', img, '|32x32px|link=', link or '', '|', escapedTitle, ']]' )
altText = altText .. ' linking to ' .. link
else
end
local image
if formattedTitle or plainTitle or link then
if mod then
altText = altText .. ' with description: ' .. ( formattedTitle or plainTitle or link )
image = args.spritesheet or mod .. 'Sprite.png'
if description then
end
altText = altText .. ' ' .. description:gsub( '/', ' ' )
if link then
item:wikitext( '[[', link, '|' )
end
end
local image, spriteCat = sprite{
altText = altText:gsub( '&[0-9a-fk-or]', '' )
iddata = idData, title = plainTitle,
image = image, data = 'InvSprite',
nourl = args.nourl,
}
item:node( image )
category = spriteCat
end
end
item:addClass( 'invslot-item-image' )
:wikitext( '[[File:', img, '|32x32px|link=', link or '', '|', escapedTitle, '|alt=', altText, ']]' )
if num and num > 1 and num < 1000 then
if num and num > 1 and num < 1000 then
if img and link then
if link then
item:wikitext( '[[', link, '|' )
item:wikitext( '[[', link, '|' )
end
end
Line 176: Line 186:
:attr{ title = plainTitle }
:attr{ title = plainTitle }
:wikitext( num )
:wikitext( num )
if args.numStyle then
if args.numstyle then
number:cssText( args.numStyle )
number:cssText( args.numstyle )
end
end
if img and link then
if link then
item:wikitext( ']]' )
item:wikitext( ']]' )
end
end
end
if not img and link then
item:wikitext( ']]' )
end
end
Line 282: Line 288:
local subframe
local subframe
local expandedAliases
local expandedAliases
local splitFrames = mw.text.split( mw.text.trim( framesText ), '%s*;%s*' )
local splitFrames = splitOnUnenclosedSemicolons( framesText )
for i, frameText in ipairs( splitFrames ) do
for i, frameText in ipairs( splitFrames ) do
frameText = frameText:gsub( '^%s*{%s*', function()
frameText = frameText:gsub( '^%s*{%s*', function()