Difference between revisions of "Module:Img"

From Idlescape Wiki
Jump to navigation Jump to search
(The module is slower than using a switch statement in a template. Will leave it uninvoked for now.)
 
m
 
(30 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
local origArgs = {}
+
local findId = require("Module:FindId")
local name
 
local width = 'auto'
 
local height = 'auto'
 
local add_word
 
local only_url
 
local no_link
 
local link
 
  
local data_module_names = {
+
local dataModuleNames = {
item = 'Module:Items/data',
+
item = "Module:Items/data",
--item = 'Module:Silent1/sandbox/data',
+
enchantment = "Module:Enchantment/data",
other = 'Module:OtherImages/data'
+
location = "Module:Location/data",
 +
monster = "Module:Monsters/data",
 +
ability = "Module:Abilities/data",
 +
other = "Module:OtherImages/data"
 
}
 
}
local loaded_data_modules = {}
 
  
function p.loadData (data_type)  
+
local idListType = {
local module_name = data_module_names[data_type]
+
[1] = {
if loaded_data_modules[module_name] == nil then
+
[1] = "item",
loaded_data_modules[module_name] = mw.loadData(module_name)
+
[2] = "itemImage",
 +
[3] = "itemIcon"
 +
},
 +
[2] = {
 +
[1] = "monster",
 +
[2] = "image"
 +
},
 +
[3] = {
 +
[1] = "enchantment",
 +
[2] = "buffIcon"
 +
},
 +
[4] = {
 +
[1] = "location",
 +
[2] = "locationImage"
 +
},
 +
[5] = {
 +
[1] = "ability",
 +
[2] = "abilityImage"
 +
}
 +
}
 +
 
 +
local loadedDataModules = {}
 +
 
 +
local alternativeNames = {
 +
["fishing_(enchantment)"] = 23,
 +
["cooking_(enchantment)"] = 24,
 +
["crafting_(enchantment)"] = 25,
 +
["runecrafting_(enchantment)"] = 27,
 +
["farming_(enchantment)"] = 39,
 +
fishing = 0,
 +
cooking = 0,
 +
crafting = 0,
 +
farming = 0,
 +
runecrafting = 0
 +
}
 +
 
 +
--
 +
-- Loads data modules
 +
--
 +
-- @param dataType {string}
 +
-- @return {data table}
 +
--
 +
function p.loadData (dataType)
 +
local moduleName = dataModuleNames[dataType]
 +
if loadedDataModules[moduleName] == nil then
 +
loadedDataModules[moduleName] = mw.loadData(moduleName)
 
end
 
end
+
return loadedDataModules[moduleName]
return loaded_data_modules[module_name]
 
 
end
 
end
  
--Search the item data for item name and return its id
+
--
function p.findItemID(name)
+
-- Helps capitalize the first letter of each word
local items = p.loadData('item')
+
--
local lname = string.lower(name)
+
-- @param first {string}
local id = nil
+
-- @param rest {string}
+
-- @return {strings}
for k,v in pairs(items) do
+
--
if lname == string.lower(v['name']) then  
+
local function tchelper(first, rest)
id = k
+
    return first:upper()..rest:lower()
return id
+
end
end
+
 
 +
--
 +
-- Capitalize the first letter of a word in a string
 +
--
 +
-- @param s {string}
 +
-- @return {string}
 +
--
 +
local function capitalize(s)
 +
s = s:gsub("(%a)([%w_']*)", tchelper):gsub(" Of "," of "):gsub(" The "," the "):gsub("Ii","II")
 +
return s
 +
end
 +
 
 +
--
 +
-- Hyphenates name
 +
--
 +
-- @param name {string}
 +
-- @return {string}
 +
--
 +
local function hyphenateName(name)
 +
    local lName = name:lower()
 +
    return lName:gsub('^%s*(.-)%s*$', '%1'):gsub("%s+", "_")
 +
end
 +
 
 +
--
 +
-- Generates a full url
 +
--
 +
-- @param url {string}
 +
-- @return {string}
 +
--
 +
local function fullUrl(url)
 +
local newUrl = url
 +
if url:sub(1,5) == "https" then
 +
return newUrl
 +
end
 +
 
 +
if url:sub(1,1) ~= "/" then
 +
newUrl = "/" .. newUrl
 
end
 
end
return id
+
 
 +
newUrl = "https://www.play.idlescape.com" .. newUrl
 +
return newUrl
 
end
 
end
  
--Find the image from the name
+
--  
function p.findImage(name)
+
-- Finds input's id and input's type
 +
--
 +
-- @param name {string}
 +
-- @return {number}
 +
-- @return {string}
 +
local function findInputId(name)
 
local id
 
local id
name = string.lower(name)
+
local inputType
local image = p.loadData('other')[name]
+
for index, value in ipairs(idListType) do
+
id = findId._findId({name, value[1]})
--If name is a number, pass in as an item ID
+
if type(id) == "number" then
if tonumber(name) then
+
inputType = value[1]
id = name
+
return id , inputType
else
+
end
id = p.findItemID(name)
 
 
end
 
end
+
return 0
--If id is valid
+
end
if id then
+
 
local item = p.loadData('item')[id]
+
--
local url = item['itemImage']
+
-- Finds arg's image url
local icon = item['itemIcon']
+
--
local realname = item['name']
+
-- @param id {number}
+
-- @param inputType {string}
if icon then url = icon end
+
-- @return {string}
image = {name = realname, image = url}
+
--
+
local function findImageUrl(id, inputType)
if not (image['name'] and image['image']) then
+
local imageUrl
image = nil
+
for index, value in ipairs(idListType) do
 +
if value[1] == inputType then
 +
if inputType == "item" then
 +
if p.loadData(inputType)[tostring(id)][value[3]] then
 +
return fullUrl(p.loadData(inputType)[tostring(id)][value[3]])
 +
end
 +
end
 +
imageUrl = fullUrl(p.loadData(inputType)[tostring(id)][value[2]])
 +
return imageUrl
 
end
 
end
 
end
 
end
+
return 0
return image
 
 
end
 
end
  
-- function p.findImage(name)
+
local function pageUrl(name)
-- local id
+
return "/p/" .. name:gsub('^%s*(.-)%s*$', '%1'):gsub("%s+", "_"):gsub("'", "%%27")
-- name = string.lower(name)
+
end
-- local image = p.loadData('other')[name]
 
 
-- if not image then
 
-- image = p.loadData('item')[name]
 
-- end
 
-- return image
 
-- end
 
  
--Create a string to output
+
--
local function _image()
+
-- Generates string with page link and <img> tag
local image = p.findImage(name)
+
--
 +
-- @param name {string}
 +
-- @param url {string}
 +
-- @param width {number}
 +
-- @param height {number}
 +
-- @param word {number}
 +
-- @return {string}
 +
--
 +
local function image(name, url, width, height, word)
 +
name = capitalize(name)
 
local s = ''
 
local s = ''
local rs = nil
+
local src = ' src="'.. url .. '"'
+
local alt = ' alt="' .. name .. '"'
if image then
+
width = ' width="' .. width .. 'px"'
local realname = image['name']
+
height = ' height="' .. height .. 'px"'
local url = image['image']
+
local img = '<img' .. src .. alt .. width .. height ..'>'
+
s = '[[' .. name .. '|' .. img
if url:sub(1,1) ~= '/' then url = '/' .. url end
+
if tonumber(word) == 1 then
url = 'https://idlescape.com' .. url
+
s = s .. '&nbsp;' .. name .. ']]'
s = url
+
s = '<span style="white-space: nowrap;">' .. s .. '</span>'
+
return s
if only_url == '1' then rs = s end
+
end
+
return s .. ']]'
s = 'src="' .. s .. '"'
+
end
s = s .. ' alt="' .. realname .. '"'
+
 
s = s .. ' width="' .. width .. '"'
+
--
s = s .. ' height="' .. height .. '"'
+
-- Finds otherimage
s = '<img ' .. s .. '>'
+
--
+
-- @param name {string}
if add_word == '1' then  
+
-- @return {string}
s = s .. ' ' .. realname
+
--
 +
local function findOtherImage(name)
 +
local h_name = hyphenateName(name)
 +
for key, value in pairs(p.loadData("other")) do
 +
if h_name == key then
 +
return value
 
end
 
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
 
end
if not rs then rs = s end  
+
return 0
return rs
+
end
 +
 
 +
--
 +
-- Generates string with page link and <img> tag
 +
--
 +
-- @param name {string}
 +
-- @param url {string}
 +
-- @param width {number}
 +
-- @param height {number}
 +
-- @param word {number}
 +
-- @return {string}
 +
--
 +
local function otherImage(name, url, width, height, word)
 +
local s = url
 +
s = s .. "\"" .. width .. "px\""
 +
s = s .. " height=\"" .. height .. "px\">"
 +
if tonumber(word) == 1 then
 +
s = s .. "&nbsp;".. capitalize(name)
 +
end
 +
s = s .. ']]'
 +
return s
 
end
 
end
  
function p.image(frame)
+
--
return p._image(frame:getParent())
+
-- Main img method accessed through #invoke
 +
--
 +
-- @param frame {table}
 +
-- @return {string}
 +
--
 +
function p.img(frame)
 +
return p._img(frame:getParent().args)
 
end
 
end
  
function p._image(frame)
+
--
origArgs = frame.args
+
-- img method to allow it to be called by other modules
name = origArgs[1]
+
--
arg = origArgs[2]
+
-- @param _args {table}
if not arg then  
+
-- @return {string}
width = 20  
+
--
else arg:match('%S')
+
function p._img(_args)
width = arg
+
local width = _args[2]
 +
local height = _args[3]
 +
local id
 +
local input_type
 +
local url
 +
local word = _args["word"]
 +
local other_image
 +
local s
 +
local error
 +
if not width then
 +
width = 20
 +
end
 +
if not height then
 +
height = width
 +
end
 +
if not _args[1] then
 +
return "Couldn't get name"
 +
end
 +
if alternativeNames[hyphenateName(_args[1])] then
 +
id = alternativeNames[hyphenateName(_args[1])]
 +
input_type = "enchantment"
 +
else
 +
id, input_type = findInputId(_args[1])
 +
end
 +
if id == 0 then
 +
other_image = findOtherImage(_args[1])
 +
end
 +
if other_image == 0 then
 +
error = "<span class=\"rt-commentedText tooltip tooltip-dotted\" title=\"Given name doesn't exist in any of the data modules, Check spelling or update data modules.\">".. _args[1] .."</span>"
 +
return error
 +
elseif other_image then
 +
s = otherImage(_args[1], other_image, width, height, word)
 +
return s
 +
end
 +
url = findImageUrl(id, input_type)
 +
if url == 0 then
 +
return "couldn't find image url."
 
end
 
end
arg = origArgs[3]
+
s = image(_args[1], url, width, height, word)
if arg and arg:match('%S') then height = arg end
+
return s
add_word = origArgs['word']
 
only_url = origArgs['url']
 
no_link = origArgs['nolink']
 
link = origArgs['link']
 
 
if not name then return '' end
 
return _image()
 
 
end
 
end
  
 
return p
 
return p

Latest revision as of 15:41, 21 June 2024


local p = {}
local findId = require("Module:FindId")

local dataModuleNames = {
	item = "Module:Items/data",
	enchantment = "Module:Enchantment/data",
	location = "Module:Location/data",
	monster = "Module:Monsters/data",
	ability = "Module:Abilities/data",
	other = "Module:OtherImages/data"
}

local idListType = {
	[1] = {
		[1] = "item",
		[2] = "itemImage",
		[3] = "itemIcon"
	},
	[2] = {
		[1] = "monster",
		[2] = "image"
	},
	[3] = {
		[1] = "enchantment",
		[2] = "buffIcon"
	},
	[4] = {
		[1] = "location",
		[2] = "locationImage"
	},
	[5] = {
		[1] = "ability",
		[2] = "abilityImage"
	}
}

local loadedDataModules = {}

local alternativeNames = {
	["fishing_(enchantment)"] = 23,
	["cooking_(enchantment)"] = 24,
	["crafting_(enchantment)"] = 25,
	["runecrafting_(enchantment)"] = 27,
	["farming_(enchantment)"] = 39,
	fishing = 0,
	cooking = 0,
	crafting = 0,
	farming = 0,
	runecrafting = 0
}

--
-- Loads data modules
--
-- @param dataType {string}
-- @return {data table}
--
function p.loadData (dataType)
	local moduleName = dataModuleNames[dataType]
	if loadedDataModules[moduleName] == nil then
		loadedDataModules[moduleName] = mw.loadData(moduleName)
	end
	return loadedDataModules[moduleName]
end

--
-- Helps capitalize the first letter of each word
--
-- @param first {string}
-- @param rest {string}
-- @return {strings}
--
local function tchelper(first, rest)
    return first:upper()..rest:lower()
end

--
-- Capitalize the first letter of a word in a string
--
-- @param s {string}
-- @return {string}
--
local function capitalize(s)
	s = s:gsub("(%a)([%w_']*)", tchelper):gsub(" Of "," of "):gsub(" The "," the "):gsub("Ii","II")
	return s
end

--
-- Hyphenates name
--
-- @param name {string}
-- @return {string}
--
local function hyphenateName(name)
    local lName = name:lower()
    return lName:gsub('^%s*(.-)%s*$', '%1'):gsub("%s+", "_")
end

--
-- Generates a full url
--
-- @param url {string}
-- @return {string}
--
local function fullUrl(url)
	local newUrl = url
	if url:sub(1,5) == "https" then
		return newUrl
	end

	if url:sub(1,1) ~= "/" then
		newUrl = "/" .. newUrl
	end

	newUrl = "https://www.play.idlescape.com" .. newUrl
	return newUrl
end

-- 
-- Finds input's id and input's type
--
-- @param name {string}
-- @return {number}
-- @return {string}
local function findInputId(name)
	local id
	local inputType
	for index, value in ipairs(idListType) do
		id = findId._findId({name, value[1]})
		if type(id) == "number" then
			inputType = value[1]
			return id , inputType
		end
	end
	return 0
end

--
-- Finds arg's image url
--
-- @param id {number}
-- @param inputType {string}
-- @return {string}
--
local function findImageUrl(id, inputType)
	local imageUrl
	for index, value in ipairs(idListType) do
		if value[1] == inputType then
			if inputType == "item" then
				if p.loadData(inputType)[tostring(id)][value[3]] then
					return fullUrl(p.loadData(inputType)[tostring(id)][value[3]])
				end
			end
			imageUrl = fullUrl(p.loadData(inputType)[tostring(id)][value[2]])
			return imageUrl
		end
	end
	return 0
end

local function pageUrl(name)
	return "/p/" .. name:gsub('^%s*(.-)%s*$', '%1'):gsub("%s+", "_"):gsub("'", "%%27")
end

--
-- Generates string with page link and <img> tag
--
-- @param name {string}
-- @param url {string}
-- @param width {number}
-- @param height {number}
-- @param word {number}
-- @return {string}
--
local function image(name, url, width, height, word)
	name = capitalize(name)
	local s = ''
	local src = ' src="'.. url .. '"'
	local alt = ' alt="' .. name .. '"'
	width = ' width="' .. width .. 'px"'
	height = ' height="' .. height .. 'px"'
	local img = '<img' .. src .. alt .. width .. height ..'>'
	s = '[[' .. name .. '|' .. img
	if tonumber(word) == 1 then
		s = s .. '&nbsp;' .. name .. ']]'
		s = '<span style="white-space: nowrap;">' .. s .. '</span>'
		return s
	end
	return s .. ']]'
end

--
-- Finds otherimage
--
-- @param name {string}
-- @return {string}
--
local function findOtherImage(name)
	local h_name = hyphenateName(name)
	for key, value in pairs(p.loadData("other")) do
		if h_name == key then
			return value
		end
	end
	return 0
end

--
-- Generates string with page link and <img> tag
--
-- @param name {string}
-- @param url {string}
-- @param width {number}
-- @param height {number}
-- @param word {number}
-- @return {string}
--
local function otherImage(name, url, width, height, word)
	local s = url
	s = s .. "\"" .. width .. "px\""
	s = s .. " height=\"" .. height .. "px\">"
	if tonumber(word) == 1 then
		s = s .. "&nbsp;".. capitalize(name)
	end
	s = s .. ']]'
	return s
end

--
-- Main img method accessed through #invoke
--
-- @param frame {table}
-- @return {string}
--
function p.img(frame)
	return p._img(frame:getParent().args)
end

--
-- img method to allow it to be called by other modules
--
-- @param _args {table}
-- @return {string}
--
function p._img(_args)
	local width = _args[2]
	local height = _args[3]
	local id
	local input_type
	local url
	local word = _args["word"]
	local other_image
	local s
	local error
	if not width then
		width = 20
	end
	if not height then
		height = width
	end
	if not _args[1] then
		return "Couldn't get name"
	end
	if alternativeNames[hyphenateName(_args[1])] then
		id = alternativeNames[hyphenateName(_args[1])]
		input_type = "enchantment"
	else
		id, input_type = findInputId(_args[1])
	end
	if id == 0 then
		other_image = findOtherImage(_args[1])
	end
	if other_image == 0 then
		error = "<span class=\"rt-commentedText tooltip tooltip-dotted\" title=\"Given name doesn't exist in any of the data modules, Check spelling or update data modules.\">".. _args[1] .."</span>"
		return error
	elseif other_image then
		s = otherImage(_args[1], other_image, width, height, word)
		return s
	end
	url = findImageUrl(id, input_type)
	if url == 0 then
		return "couldn't find image url."
	end
	s = image(_args[1], url, width, height, word)
	return s
end

return p