Difference between revisions of "Module:Silent1/sandbox"

From Idlescape Wiki
Jump to navigation Jump to search
m
m
Line 10: Line 10:
  
 
local data_module_names = {
 
local data_module_names = {
item = 'Module:Silent1/sandbox/data'
+
item = 'Module:Items/data',
 +
other = 'Module:OtherImages/data'
 
}
 
}
 
local loaded_data_modules = {}
 
local loaded_data_modules = {}
Line 21: Line 22:
 
 
 
return loaded_data_modules[module_name]
 
return loaded_data_modules[module_name]
 +
end
 +
 +
--Search the item data for item name and return its id
 +
function p.findItemID(name)
 +
local items = p.loadData('item')
 +
local lname = string.lower(name)
 +
local id = nil
 +
 +
for k,v in pairs(items) do
 +
if lname == string.lower(v['name']) then
 +
id = k
 +
return id
 +
end
 +
end
 +
return id
 +
end
 +
 +
function p.findImage(name)
 +
name = string.lower(name)
 +
local id = p.findItemID(name)
 +
local image = p.loadData('other')[name]
 +
 +
--If no entry found in other and item id isn't invalid
 +
if not image and id then
 +
local item = p.loadData('item')[id]
 +
local url = item['itemImage']
 +
local icon = item['itemIcon']
 +
local realname = item['name']
 +
 +
if icon then url = icon end
 +
image = {name = realname, image = url}
 +
end
 +
 +
return image
 
end
 
end
  
 
local function _image()
 
local function _image()
name = string.lower(name)
+
local image = p.findImage(name)
local item = p.loadData('item')[name]
 
 
local s = ''
 
local s = ''
 
local rs = nil
 
local rs = nil
if item then  
+
local url = item['image']
+
if image then  
local rname = item['name']
+
local realname = item['name']
 +
local url = item['itemImage']
 
 
 
if url:sub(1,1) ~= '/' then url = '/' .. url end
 
if url:sub(1,1) ~= '/' then url = '/' .. url end
Line 37: Line 72:
 
 
 
if only_url == '1' then rs = s end
 
if only_url == '1' then rs = s end
 +
 
s = 'src="' .. s .. '"'
 
s = 'src="' .. s .. '"'
s = s .. ' alt="' .. rname .. '"'
+
s = s .. ' alt="' .. realname .. '"'
 
s = s .. ' width="' .. width .. '"'
 
s = s .. ' width="' .. width .. '"'
 
s = s .. ' height="' .. height .. '"'
 
s = s .. ' height="' .. height .. '"'
Line 44: Line 80:
 
 
 
if add_word == '1' then  
 
if add_word == '1' then  
s = s .. ' ' .. rname
+
s = s .. ' ' .. realname
 
end
 
end
 +
 
if no_link == '1' then rs = s end
 
if no_link == '1' then rs = s end
 
 
if not (link and link:match('%S')) then link = rname end
+
if not (link and link:match('%S')) then link = realname end
 
s = '[[' .. link .. '|' .. s .. ']]'
 
s = '[[' .. link .. '|' .. s .. ']]'
 
else
 
else

Revision as of 15:55, 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 link

local data_module_names = {
	item = 'Module:Items/data',
	other = 'Module:OtherImages/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

--Search the item data for item name and return its id
function p.findItemID(name)
	local items = p.loadData('item')
	local lname = string.lower(name)
	local id = nil
	
	for k,v in pairs(items) do
		if lname == string.lower(v['name']) then 
			id = k
			return id
		end
	end
	return id
end

function p.findImage(name)
	name = string.lower(name)
	local id = p.findItemID(name)
	local image = p.loadData('other')[name]

	--If no entry found in other and item id isn't invalid
	if not image and id then
		local item = p.loadData('item')[id]
		local url = item['itemImage']
		local icon = item['itemIcon']
		local realname = item['name']
		
		if icon then url = icon end
		image = {name = realname, image = url}
	end
	
	return image
end

local function _image()
	local image = p.findImage(name)
	local s = ''
	local rs = nil
	
	if image then 
		local realname = item['name']
		local url = item['itemImage']
		
		if url:sub(1,1) ~= '/' then url = '/' .. url end
		url = 'https://idlescape.com' .. url
		s = url
		
		if only_url == '1' then rs = s end
		
		s = 'src="' .. s .. '"'
		s = s .. ' alt="' .. realname .. '"'
		s = s .. ' width="' .. width .. '"'
		s = s .. ' height="' .. height .. '"'
		s = '<img ' .. s .. '>'
		
		if add_word == '1' then 
			s = s .. ' ' .. realname
		end
		
		if no_link == '1' then rs = s end
		
		if not (link and link:match('%S')) then link = realname end
		s = '[[' .. link .. '|' .. s .. ']]'
	else
		s = '[[' .. name .. ']]' .. '{{?}}'
	end
	if not 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 not arg then 
		width = 20 
	else arg:match('%S')
		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 not name then return '' end
	return _image()
end

return p