Difference between revisions of "Module:Img"
Jump to navigation
Jump to search
m |
|||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
| − | local | + | local findId = require("Module:FindId") |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | local | + | local dataModuleNames = { |
| − | item = | + | item = "Module:Items/data", |
| − | + | enchantment = "Module:Enchantment/data", | |
| − | location = | + | location = "Module:Location/data", |
| + | monster = "Module:Items/data" | ||
} | } | ||
| − | |||
| − | function p.loadData ( | + | local idListType = { |
| − | local | + | [1] = { |
| − | if | + | [1] = "item", |
| − | + | [2] = "itemImage" | |
| + | }, | ||
| + | [2] = { | ||
| + | [1] = "monster", | ||
| + | [2] = "image" | ||
| + | }, | ||
| + | [3] = { | ||
| + | [1] = "enchantment", | ||
| + | [2] = "buffIcon" | ||
| + | }, | ||
| + | [4] = { | ||
| + | [1] = "location", | ||
| + | [2] = "locationImage" | ||
| + | } | ||
| + | } | ||
| + | |||
| + | 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 | end | ||
| − | + | return loadedDataModules[moduleName] | |
| − | return | ||
end | end | ||
| − | -- | + | -- |
| − | local function | + | -- 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 | end | ||
| − | -- | + | -- |
| − | + | -- Generates a full url | |
| − | + | -- | |
| − | + | -- @param url {string} | |
| − | local | + | -- @return {string} |
| − | + | -- | |
| − | + | local function fullUrl(url) | |
| − | if | + | local newUrl = url |
| − | + | if url:sub(1,5) == "https" then | |
| − | + | return newUrl | |
| − | |||
end | end | ||
| − | + | ||
| − | + | if url:sub(1,1) ~= "/" then | |
| − | if | + | newUrl = "/" .. newUrl |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
| − | + | ||
| − | return | + | newUrl = "https://www.play.idlescape.com" .. newUrl |
| + | return newUrl | ||
end | end | ||
| − | -- | + | -- |
| − | local function | + | -- Finds input's id and input's type |
| − | local id | + | -- |
| − | local | + | -- @param name {string} |
| − | + | -- @return {number} | |
| − | + | -- @return {string} | |
| − | + | local function findInputId(name) | |
| − | + | local id | |
| − | + | local inputType | |
| − | + | for index, value in ipairs(idListType) do | |
| − | + | id = findId(name, value[1]) | |
| + | if type(id) == "number" then | ||
| + | inputType = value[1] | ||
| + | return id , inputType | ||
end | end | ||
| − | |||
| − | |||
end | end | ||
| + | return 0 | ||
| + | end | ||
| − | + | -- | |
| − | + | -- Finds arg's image url | |
| − | if | + | -- |
| − | + | -- @param id {number} | |
| − | + | -- @param inputType {string} | |
| − | return | + | -- @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[1])[tostring(id)][value[2]]) | ||
| + | return imageUrl | ||
end | end | ||
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
| − | return | + | return 0 |
end | 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 .. "\"" | |
| − | + | s = s .. " height=\"" .. height .. "\"" | |
| − | + | s = '<img ' .. s .. '>' | |
| − | + | if word == '1' then | |
| − | + | s = s .. " " .. name | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
end | end | ||
| − | + | s = '[[' .. name:gsub("(%a)([%w_']*)", tchelper) .. '|' .. s .. ']]' | |
| − | return | + | return s |
end | end | ||
| − | function p. | + | -- |
| − | return p. | + | -- Main img method accessed through #invoke |
| + | -- | ||
| + | -- @param frame {table} | ||
| + | -- @return {string} | ||
| + | -- | ||
| + | function p.img(frame) | ||
| + | return p._img(frame:getParent()) | ||
end | end | ||
| − | function p. | + | -- |
| − | local | + | -- img method to allow it to be called by other modules |
| − | + | -- | |
| − | + | -- @param _args {table} | |
| − | + | -- @return {string} | |
| − | + | -- | |
| − | local | + | function p._img(_args) |
| − | if | + | local width = _args[2] |
| − | width = | + | local height = _args[3] |
| + | local id | ||
| + | local inputType | ||
| + | local url | ||
| + | local s | ||
| + | if not width then | ||
| + | width = 20 | ||
end | end | ||
| − | if | + | if not height then |
| − | height = | + | height = 20 |
end | end | ||
| − | + | id, inputType = findInputId(_args[1]) | |
| − | + | if not id then | |
| + | return "couldn't find id. Check spelling or update id lists" | ||
end | end | ||
| − | if | + | url = findImageUrl(id, inputType) |
| − | + | if not url then | |
| + | return "couldn't find image url." | ||
end | end | ||
| − | + | s = image(_args[1], url, width, height, _args["word"]) | |
| − | + | return s | |
| − | |||
| − | return | ||
end | end | ||
return p | return p | ||
Revision as of 12:35, 1 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:Items/data"
}
local idListType = {
[1] = {
[1] = "item",
[2] = "itemImage"
},
[2] = {
[1] = "monster",
[2] = "image"
},
[3] = {
[1] = "enchantment",
[2] = "buffIcon"
},
[4] = {
[1] = "location",
[2] = "locationImage"
}
}
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
--
-- 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(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[1])[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 .. "\""
s = s .. " height=\"" .. height .. "\""
s = '<img ' .. s .. '>'
if word == '1' then
s = s .. " " .. name
end
s = '[[' .. name:gsub("(%a)([%w_']*)", tchelper) .. '|' .. s .. ']]'
return s
end
--
-- Main img method accessed through #invoke
--
-- @param frame {table}
-- @return {string}
--
function p.img(frame)
return p._img(frame:getParent())
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 s
if not width then
width = 20
end
if not height then
height = 20
end
id, inputType = findInputId(_args[1])
if not id then
return "couldn't find id. Check spelling or update id lists"
end
url = findImageUrl(id, inputType)
if not url then
return "couldn't find image url."
end
s = image(_args[1], url, width, height, _args["word"])
return s
end
return p