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]], | ||
moduleRandom = [[Module:Random]], | moduleRandom = [[Module:Random]], | ||
-- 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 aliases = mw.loadData( i18n.moduleAliases ) | local aliases = mw.loadData( i18n.moduleAliases ) | ||
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 | local img | ||
if mod then | if mod then | ||
img = i18n.legacyFilename:gsub( '%$1', name .. ' (' .. mod .. ')' ) | |||
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 | ||
formattedTitle = '' | |||
end | end | ||
| Line 144: | Line 160: | ||
} | } | ||
-- & is re-escaped because mw.html treats attributes | |||
-- as plain text, but MediaWiki doesn't | |||
local escapedTitle = ( plainTitle or '' ):gsub( '&', '&' ) | |||
local altText = img .. ': Inventory sprite for ' .. name .. ' in Minecraft as shown in-game' | |||
if link then | |||
altText = altText .. ' linking to ' .. link | |||
end | |||
if formattedTitle or plainTitle or link then | |||
altText = altText .. ' with description: ' .. ( formattedTitle or plainTitle or link ) | |||
if description then | |||
altText = altText .. ' ' .. description:gsub( '/', ' ' ) | |||
if | |||
end | end | ||
altText = altText:gsub( '&[0-9a-fk-or]', '' ) | |||
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 | 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. | if args.numstyle then | ||
number:cssText( args. | number:cssText( args.numstyle ) | ||
end | end | ||
if | if link then | ||
item:wikitext( ']]' ) | item:wikitext( ']]' ) | ||
end | end | ||
end | end | ||
| Line 282: | Line 288: | ||
local subframe | local subframe | ||
local expandedAliases | local expandedAliases | ||
local splitFrames = | 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() | ||