Difference between revisions of "Module:Img"

From Idlescape Wiki
Jump to navigation Jump to search
(Fixed module not working templates, Added abilities)
(Added misc images)
Line 7: Line 7:
 
location = "Module:Location/data",
 
location = "Module:Location/data",
 
monster = "Module:Monsters/data",
 
monster = "Module:Monsters/data",
ability = "Module:Abilities/data"
+
ability = "Module:Abilities/data",
 +
other = "Module:OtherImages/data"
 
}
 
}
  
Line 58: Line 59:
 
local function tchelper(first, rest)
 
local function tchelper(first, rest)
 
     return first:upper()..rest:lower()
 
     return first:upper()..rest:lower()
 +
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
 
end
  
Line 142: Line 154:
 
end
 
end
 
s = '[[' .. name:gsub("(%a)([%w_']*)", tchelper) .. '|' .. s .. ']]'
 
s = '[[' .. name:gsub("(%a)([%w_']*)", tchelper) .. '|' .. s .. ']]'
 +
return s
 +
end
 +
 +
--
 +
-- Finds otherimage
 +
--
 +
-- @param name {string}
 +
-- @return {string}
 +
--
 +
local function findOtherImage(name)
 +
local hName = hyphenateName(name)
 +
for key, value in pairs(p.loadData("other")) do
 +
if hName == 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\""
 +
if type(height) == "number" then
 +
s = s .. " height=\"" .. height .. "px\""
 +
else
 +
s = s .. " height=\"" .. height .. "\""
 +
end
 +
if tonumber(word) == 1 then
 +
s = s .. "&nbsp;" .. name
 +
end
 +
s = s .. ']]'
 
return s
 
return s
 
end
 
end
Line 167: Line 220:
 
local inputType
 
local inputType
 
local url
 
local url
local word
+
local word = _args["word"]
 +
local otherimage
 
local s
 
local s
 
if not width then
 
if not width then
Line 179: Line 233:
 
end
 
end
 
id, inputType = findInputId(_args[1])
 
id, inputType = findInputId(_args[1])
if not id then
+
if id == 0 then
return "couldn't find id. Check spelling or update id lists"
+
otherimage = findOtherImage(_args[1])
 +
end
 +
if otherimage == 0 then
 +
return "Given name doesn't exist in any of the data modules, Check spelling or update data modules"
 +
elseif otherimage then
 +
s = otherImage(_args[1], url, width, height, word)
 +
return s
 
end
 
end
 
url = findImageUrl(id, inputType)
 
url = findImageUrl(id, inputType)
if not url then
+
if url == 0 then
 
return "couldn't find image url."
 
return "couldn't find image url."
 
end
 
end
word = _args["word"]
 
 
s = image(_args[1], url, width, height, word)
 
s = image(_args[1], url, width, height, word)
 
return s
 
return s

Revision as of 08:47, 2 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"
	},
	[2] = {
		[1] = "monster",
		[2] = "image"
	},
	[3] = {
		[1] = "enchantment",
		[2] = "buffIcon"
	},
	[4] = {
		[1] = "location",
		[2] = "locationImage"
	},
	[5] = {
		[1] = "ability",
		[2] = "abilityImage"
	}
}

local loadedDataModules = {}

--
-- 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 capitalise 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

--
-- 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
			imageUrl = fullUrl(p.loadData(inputType)[tostring(id)][value[2]])
			return imageUrl
		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 image(name, url, width, height, word)
	local s = url
	s = "src=\"".. s .. "\""
	s = s .. " alt=\"" .. name .. "\""
	s = s .. " width=\"" .. width .. "px\""
	if type(height) == "number" then
		s = s .. " height=\"" .. height .. "px\""
	else
		s = s .. " height=\"" .. height .. "\""
	end
	s = '<img ' .. s .. '>'
	if tonumber(word) == 1 then
		s = s .. "&nbsp;" .. name
	end
	s = '[[' .. name:gsub("(%a)([%w_']*)", tchelper) .. '|' .. s .. ']]'
	return s
end

--
-- Finds otherimage
--
-- @param name {string}
-- @return {string}
--
local function findOtherImage(name)
	local hName = hyphenateName(name)
	for key, value in pairs(p.loadData("other")) do
		if hName == 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\""
	if type(height) == "number" then
		s = s .. " height=\"" .. height .. "px\""
	else
		s = s .. " height=\"" .. height .. "\""
	end
	if tonumber(word) == 1 then
		s = s .. "&nbsp;" .. 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 inputType
	local url
	local word = _args["word"]
	local otherimage
	local s
	if not width then
		width = 20
	end
	if not height then
		height = "auto"
	end
	if not _args[1] then
		return "Couldn't get name"
	end
	id, inputType = findInputId(_args[1])
	if id == 0 then
		otherimage = findOtherImage(_args[1])
	end
	if otherimage == 0 then
		return "Given name doesn't exist in any of the data modules, Check spelling or update data modules"
	elseif otherimage then
		s = otherImage(_args[1], url, width, height, word)
		return s
	end
	url = findImageUrl(id, inputType)
	if url == 0 then
		return "couldn't find image url."
	end
	s = image(_args[1], url, width, height, word)
	return s
end

return p