Difference between revisions of "Module:Silent1/sandbox"

From Idlescape Wiki
Jump to navigation Jump to search
m
m
Line 26: Line 26:
 
local items = p.loadData('item')
 
local items = p.loadData('item')
 
local lname = string.lower(name)
 
local lname = string.lower(name)
local id = 0
+
local id = '0'
 
 
 
for k,v in pairs(items) do
 
for k,v in pairs(items) do
Line 39: Line 39:
 
local function _image()
 
local function _image()
 
local id = p.findItemID(name)
 
local id = p.findItemID(name)
local item = p.loadData('item')[id]
+
local s = ''
local url = 'https://idlescape.com' .. item['itemImage']
+
local rs = nil
local rname = item['name']
+
if id ~= '0' then
local s = url
+
local item = p.loadData('item')[id]
+
local url = 'https://idlescape.com' .. item['itemImage']
if only_url == '1' then return s end
+
local rname = item['name']
s = 'src="' .. s .. '"'
+
s = s .. ' alt="' .. rname .. '"'
+
s = url
s = s .. ' width="' .. width .. '"'
+
s = s .. ' height="' .. height .. '"'
+
if only_url == '1' then rs = s end
s = '<img ' .. s .. '>'
+
s = 'src="' .. s .. '"'
+
s = s .. ' alt="' .. rname .. '"'
if add_word == '1' then  
+
s = s .. ' width="' .. width .. '"'
s = s .. ' ' .. rname
+
s = s .. ' height="' .. height .. '"'
 +
s = '<img ' .. s .. '>'
 +
 +
if add_word == '1' then  
 +
s = s .. ' ' .. rname
 +
end
 +
if no_link == '1' then rs = s end
 +
 +
if not (link and link:match('%S')) then link = rname end
 +
s = '[[' .. link .. '|' .. s .. ']]'
 +
else
 +
s = '[[' .. name .. ']]' .. '{{{?}}}'
 
end
 
end
if no_link == '1' then return s end
+
if rs then rs = s end  
+
return rs
if not (link and link:match('%S')) then link = rname end
 
s = '[[' .. link .. '|' .. s .. ']]'
 
 
return s
 
 
end
 
end
  
Line 79: Line 86:
 
no_link = origArgs['nolink']
 
no_link = origArgs['nolink']
 
link = origArgs['link']
 
link = origArgs['link']
+
if name then return '' end
 
return _image()
 
return _image()
 
end
 
end

Revision as of 12:26, 14 February 2022

Documentation for this module may be created at Module:Silent1/sandbox/doc

local p = {}
local origArgs = {}
local name
local width = '20'
local height = 'auto'
local add_word
local only_url
local no_link
local 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 s = ''
	local rs = nil
	if id ~= '0' then 
		local item = p.loadData('item')[id]
		local url = 'https://idlescape.com' .. item['itemImage']
		local rname = item['name']
		
		s = url
		
		if only_url == '1' then rs = s end
		s = 'src="' .. s .. '"'
		s = s .. ' alt="' .. rname .. '"'
		s = s .. ' width="' .. width .. '"'
		s = s .. ' height="' .. height .. '"'
		s = '<img ' .. s .. '>'
		
		if add_word == '1' then 
			s = s .. ' ' .. rname
		end
		if no_link == '1' then rs = s end
		
		if not (link and link:match('%S')) then link = rname end
		s = '[[' .. link .. '|' .. s .. ']]'
	else
		s = '[[' .. name .. ']]' .. '{{{?}}}'
	end
	if rs then rs = s end 
	return rs
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]
	arg = origArgs[2]
	if arg and arg:match('%S') then width = arg end
	arg = origArgs[3]
	if arg and arg:match('%S') then height = arg end
	add_word = origArgs['word']
	only_url = origArgs['url']
	no_link = origArgs['nolink']
	link = origArgs['link']
	if name then return '' end
	return _image()
end


return p