Module:Sandbox/MonkeysHK/5

-- Initially taken from: https://minecraft.gamepedia.com/Module:Inventory_slot -- and: https://hypixel-skyblock.fandom.com/wiki/Module:Inventory_slot

local p = {}

local getArgs = require('Module:Arguments').getArgs

local string = require("Module:String") local table = require("Module:Table") local yesno = require("Module:Yesno") local random = require("Module:Random")

local aliasesCache = require('Module:Cache').slotAliasesCache

local pageName = mw.title.getCurrentTitle.text local availableFrameParameters = { 'title', 'count', 'count2', 'text', 'link', 'image', 'image_id', 'class', 'goto', 'style' } local availableInputParameters = { 'item', 'title', 'count', 'count2', 'text', 'link', 'image', 'image_id', 'class', 'goto', 'style' } -- unused?

--	Structure for main slot creation: --	p.slot --	├── p.parseFrameText --	│	└── p.combineFrames --	│	└── p.makeFrame --	└── p.makeItem

--Merges a list, or inserts a string	or table into a table -- local function mergeList( parentTable, content ) if content[1] then -- Merge list into table for _, v in ipairs( content ) do			parentTable[#parentTable + 1] = v		end else -- Add strings or tables to table parentTable[#parentTable + 1] = content end end

-- Main entry point function p.slot( f ) local args = getArgs(f) if not args.parsed then args[1] = string.trim( args[1] and (args[1]:gsub("(%s)%s+", "%1")) or '' ) end -- Prepare List of "Frames" local frames if args.parsed then frames = args[1] elseif args[1] ~= '' then local randomise = args.class == 'invslot-large' and 'never' or nil frames = p.parseFrameText( args[1], randomise, false ) end -- Now, Prepare Top-level Slot Element local body = mw.html.create( 'span' ):addClass( 'invslot noselect' ):css{ ['vertical-align'] = args.align } local animated = frames and #frames > 1 local imgClass = args.imgclass local numStyle = args.numstyle if animated then body:addClass( 'animated' ) end if args.class then body:addClass( args.class ) end if args.style then body:cssText( args.style ) end if not frames then return tostring(body:tag('span'):addClass('invslot-item'):done) end -- Create Slot Item for Each "Frame" local activeFrame = frames.randomise == true and random.number{ #frames } or 1 for index, frame in ipairs( frames ) do		local item item = p.makeItem( frame, index, args ) body:node( item ) if index == activeFrame and animated then item:addClass( 'animated-active' ) elseif animated then -- CUSTOM: "hidden" class is needed to hide it on mobile (since wikia doesn't allow CSS / JS on mobile) item:addClass('hidden') end end return tostring( body ) end

--Creates the HTML for a single Slot Item -- function p.makeItem( frame, i, args ) -- Initialize Element local item = mw.html.create( 'span' ):addClass( 'invslot-item' ) if args.imgclass then item:addClass( args.imgclass ) end if frame.image_id then item:attr('data-iid', frame.image_id) end if frame.item == '' then return item end local category local title = string.trim( args.title or frame.title or '' ) local name = frame.item or '' local n_1 = tonumber(frame.count) or 1 local n_2 = tonumber(frame.count2) local n_str, n_fs, n_r local image = args.image or frame.image or nil local description = args.text or frame.text or '' local forceNum = yesno(args.forcenum) local img -- Handle Image if frame.image_id then -- has CSS handled image ID: don't display image img = nil elseif image and string.anyMatched(image, '%.gif$', '%.webp$', '%.png', '%.apng', '%.jpg$', '%.jpeg$') then img = image else img = (image or name) .. '.png' end -- Handle Link local link = args.link or frame.link or '' if link == '' then link = name == 'none' and nil or name elseif ((not args.link) or (args.link or ''):lower == 'none') and link:lower == 'none' then link = nil end -- (Handle Link Redirects) local rtName = mw.title.makeTitle(0, link or ) and mw.title.makeTitle(0, link or ).redirectTarget or false rtName = rtName and rtName.text or false if link == pageName or rtName == pageName or link == 'none' then link = nil end -- Handle Numbers if not (n_1 and n_2 and n_1 ~= n_2) then -- No second number n_2 = nil end if (n_1 or 0) >= 10000 then n_1 = string._formatShortNum(n_1):lower end if (n_2 or 0) >= 10000 then n_2 = string._formatShortNum(n_2):lower end if forceNum or ((not n_2) and n_1 and n_1 ~= 1) or (n_1 and n_2) then -- Entering condition for displaying numbers (else, n_str undefined) local function len(n) return n and tostring(n):len or 0 end if n_1 and n_2 then n_1 = tostring(n_1) .. '-'		end local splitLine = n_2 and (len(n_1) + len(n_2) > 4) -- Deciding font-size (n_fs) and right (n_r) values if (len(n_1) > 7) or (len(n_2) > 7) then n_fs = 5.4; n_r = 1.2 elseif (len(n_1) > 6) or (len(n_2) > 6) then n_fs = 6.1; n_r = 1.3 elseif (len(n_1) > 5) or (len(n_2) > 5) then n_fs = 7.2; n_r = 1.6 elseif (len(n_1) > 3) or (len(n_2) > 3) or splitLine then n_fs = 9; n_r = 1 elseif n_2 or splitLine then n_fs = 12; n_r = 0 else n_fs = 15; n_r = 0 end -- Stringify Numbers n_str = n_2 and (n_1 .. (splitLine and ' ' or '') .. n_2) or n_1 end -- Handle Title local formattedTitle, plainTitle if title == '' then plainTitle = name formattedTitle = name elseif title:lower ~= 'none' then formattedTitle = title plainTitle = title local formatPattern = '&[0-9a-fk-or]' if plainTitle:match( formatPattern ) then plainTitle = plainTitle:gsub( formatPattern, '' ) end if plainTitle == '' then plainTitle = name end elseif link then if img then formattedTitle = '' else plainTitle = '' end end -- Handle Description if description:lower == 'none' then description = '' end -- Now, Prepare Element -- Insert Title/Text item:attr{ ['data-minetip-title'] = formattedTitle and formattedTitle:gsub('"', '&quot;') or nil,		['data-minetip-text'] = description and description:gsub('"', '&quot;') or nil, }	-- Insert Image if img then -- & is re-escaped because mw.html treats attributes -- as plain text, but MediaWiki doesn't		local escapedTitle = ( plainTitle or '' ):gsub( '&', '&#38;' ) item:addClass( 'invslot-item-image' ) :wikitext( '' ) end -- Insert Numbers if n_str then local stacksizeElm = item:tag( 'span' ) :addClass( 'invslot-stacksize' ) :attr{ title = plainTitle } stacksizeElm:css('font-size', n_fs .. 'px'):css('right', (n_r - 2) .. 'px') if args.numStyle then stacksizeElm:cssText( args.numStyle ) end stacksizeElm:wikitext( n_str ) end return item end

--Parses the frame text into a table of frames,	expanding aliases, and	deciding if the slot can be randomised -- function p.parseFrameText( framesText, randomise ) local frames = { randomise = randomise } local expandedAliases framesText = framesText:gsub( '\\;', '%%%%SEMICOLON%%%%' );-- gsub here allows us to escape ; character local splitFrames = string.split( string.trim( framesText ), '%s*;%s*' ) for _, frameText in ipairs( splitFrames ) do		frameText = frameText:gsub('%%%%SEMICOLON%%%%', ';') -- undo-escape now that semicolon regex check is done -- Now, Parse Frame Text local frame = p.makeFrame( frameText ) local newFrame = frame local id = frame.item -- Now, Find Frame Alias and Combine With Frame local alias = aliasesCache:get(id) if alias then newFrame = p.combineFrames( alias, frame ) end -- Randomise starting frame for "Any *" aliases, as long as the alias is the only frame if frames.randomise == nil and (frame.item:match( '^Any' ) or frame.item:match( '^%?' )) then frames.randomise = true elseif frames.randomise ~= 'never' then frames.randomise = false end mergeList( frames, newFrame ) end return frames end

--Returns a new table with the parent frame values	added to alias frames (parent frame overrides alias frames) -- function p.combineFrames( aliasFrames, parentFrame ) -- If alias is just a name, return the parent frame with the new name if type( aliasFrames ) == 'string' then local expandedFrame = mw.clone( parentFrame ) expandedFrame.item = aliasFrames return { expandedFrame } end -- Single frame alias, put in list if aliasFrames.item then aliasFrames = { aliasFrames } end -- Prepare new table, combine frames local expandedFrames = {} for i, aliasFrame in ipairs( aliasFrames ) do		local expandedFrame if type( aliasFrame ) == 'string' then expandedFrame = { name = aliasFrame } else expandedFrame = table.deepCopy( aliasFrame, true ) end for _, param in ipairs(availableFrameParameters) do			expandedFrame[param] = parentFrame[param] or expandedFrame[param] end expandedFrames[i] = expandedFrame end return expandedFrames end

--Parses frame text into a table	key{value} - function p.makeFrame( frameText ) local frameTextUntouched = frameText -- Simple frame with no parts if type(frameText) ~= 'string' then error(type(frameText)) end -- [ESC] allow escaping special characters - we'll convert normal character after we parse frameText = frameText:gsub( '\\\\', '%%%%BACKSLASH%%%%' ):gsub( '\\%{', '%%%%LEFTBRACE%%%%' ):gsub( '\\%}', '%%%%RIGHTBRACE%%%%' ) local frame = {} for key, value in mw.ustring.gmatch(frameText, '([^%p%s]){(.-)}') do		frame[key] = value:gsub('%%%%BACKSLASH%%%%','\\'):gsub('%%%%LEFTBRACE%%%%','{'):gsub('%%%%RIGHTBRACE%%%%','}') end if not frame.item then frame.item = frameTextUntouched end if frame.count then -- Handle m-n syntax local one, two = frameText:match( '%s*(%d+)%s*[%-%–]%s*(%d+)%s*' ) if one and two then frame.count = one frame.count2 = two end end return frame end

- -- Other Features -

p.getAlias = p.combineFrames function p.expandAlias( parentFrame, alias ) return p.combineFrames( alias, parentFrame ) end

-- function p.stringifyFrame( frame ) -- 	if not frame.item then -- 		return '' -- 	end -- 	return string.format( -- 		'[%s]%s,%s[%s]', -- 		frame.title or , -- 		frame.item, -- 		frame.count or , -- 		frame.text or '' -- 	) -- end

-- function p.stringifyFrames( frames ) -- 	for i, frame in ipairs( frames ) do -- 		frames[i] = p.stringifyFrame( frame ) -- 	end -- 	return table.concat( frames, ';' ) -- end

return p