Difference between revisions of "Module:Silent1/sandbox"

From Idlescape Wiki
Jump to navigation Jump to search
m
m
Line 1: Line 1:
 
local p = {}
 
local p = {}
 
local origArgs = {}
 
local origArgs = {}
 +
local name
 +
local width = 'auto'
 +
local height = 'auto'
 +
local add_word
 +
local only_url
 +
local no_link
 +
local alt_link
  
 
local data_module_names = {
 
local data_module_names = {
Line 30: Line 37:
 
end
 
end
  
function p.getURL(id)
+
local function _image()
return 'https://idlescape.com' .. p.loadData('item')[tostring(id)]['itemImage']
+
local id = p.findItemID(name)
 +
local item = p.loadData('item')[id]
 +
local url = 'https://idlescape.com' .. item['itemImage']
 +
local rname = item['name']
 +
local s = url
 +
 +
if only_url then return s end
 +
s = 'src="' .. s .. '"'
 +
s = s .. ' alt="' .. rname .. '"'
 +
s = s .. ' width="' .. width .. '"'
 +
s = s .. ' height="' .. height .. '"'
 +
s = '<img ' .. s .. '>'
 +
 +
if add_word then
 +
s = s .. ' ' .. rname
 +
end
 +
if no_link then return s end
 +
 +
if link then link = rname end
 +
s = '[[' .. link .. '|' .. s .. ']]'
 +
 +
return s
 
end
 
end
  
Line 42: Line 70:
 
origArgs = frame
 
origArgs = frame
 
end
 
end
 +
name = origArgs[1]
 +
if origArgs[2] then width = origArgs[2] end
 +
if origArgs[3] then height = origArgs[3] end
 +
add_word = origArgs['word']
 +
only_url = origArgs['url']
 +
no_link = origArgs['nolink']
 +
link = origArgs['link']
 
 
local id = p.findItemID(origArgs[1])
+
return _image()
return p.getURL(id)
 
 
end
 
end
 +
  
 
return p
 
return p

Revision as of 11:48, 14 February 2022


local p = {}
local origArgs = {}
local name
local width = 'auto'
local height = 'auto'
local add_word
local only_url
local no_link
local alt_link

local data_module_names = {
	item = 'Module:Silent1/sandbox/data'
}
local loaded_data_modules = {}

function p.loadData (data_type) 
	local module_name = data_module_names[data_type]
	if loaded_data_modules[module_name] == nil then
		loaded_data_modules[module_name] = mw.loadData(module_name)
	end
	
	return loaded_data_modules[module_name]
end

function p.findItemID(name)
	local items = p.loadData('item')
	local lname = string.lower(name)
	local id = 0
	
	for k,v in pairs(items) do
		if lname == string.lower(v['name']) then 
			id = k
			return id
		end
	end
	return id
end

local function _image()
	local id = p.findItemID(name)
	local item = p.loadData('item')[id]
	local url = 'https://idlescape.com' .. item['itemImage']
	local rname = item['name']
	local s = url
	
	if only_url then return s end
	s = 'src="' .. s .. '"'
	s = s .. ' alt="' .. rname .. '"'
	s = s .. ' width="' .. width .. '"'
	s = s .. ' height="' .. height .. '"'
	s = '<img ' .. s .. '>'
	
	if add_word then 
		s = s .. ' ' .. rname
	end
	if no_link then return s end
	
	if link then link = rname end
	s = '[[' .. link .. '|' .. s .. ']]'
	
	return s
end

-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
function p.image(frame)
	if frame == mw.getCurrentFrame() then
		origArgs = frame:getParent().args
	else
		origArgs = frame
	end
	name = origArgs[1]
	if origArgs[2] then width = origArgs[2] end
	if origArgs[3] then height = origArgs[3] end
	add_word = origArgs['word']
	only_url = origArgs['url']
	no_link = origArgs['nolink']
	link = origArgs['link']
	
	return _image()
end


return p