Difference between revisions of "Module:Img"

From Idlescape Wiki
Jump to navigation Jump to search
(Fixed the module breaking lines and and creating boxes)
(Update p.loadData() to also try require() if mw.loadData() fails.)
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
local findId = require("Module:FindId")
+
 
 +
local findId = require('Module:FindId')
  
 
local dataModuleNames = {
 
local dataModuleNames = {
item = "Module:Items/data",
+
    item = 'Module:Items/data',
enchantment = "Module:Enchantment/data",
+
    enchantment = 'Module:Enchantment/data',
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"
+
    other = 'Module:OtherImages/data',
 
}
 
}
  
 
local idListType = {
 
local idListType = {
[1] = {
+
    [1] = {
[1] = "item",
+
        [1] = 'item',
[2] = "itemImage"
+
        [2] = 'itemImage',
},
+
        [3] = 'itemIcon'
[2] = {
+
    },
[1] = "monster",
+
    [2] = {
[2] = "image"
+
        [1] = 'monster',
},
+
        [2] = 'image'
[3] = {
+
    },
[1] = "enchantment",
+
    [3] = {
[2] = "buffIcon"
+
        [1] = 'enchantment',
},
+
        [2] = 'buffIcon'
[4] = {
+
    },
[1] = "location",
+
    [4] = {
[2] = "locationImage"
+
        [1] = 'location',
},
+
        [2] = 'locationImage'
[5] = {
+
    },
[1] = "ability",
+
    [5] = {
[2] = "abilityImage"
+
        [1] = 'ability',
}
+
        [2] = 'abilityImage'
 +
    }
 
}
 
}
  
 
local loadedDataModules = {}
 
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
 +
}
  
 
--
 
--
Line 43: Line 58:
 
--
 
--
 
function p.loadData (dataType)
 
function p.loadData (dataType)
local moduleName = dataModuleNames[dataType]
+
    local moduleName = dataModuleNames[dataType]
if loadedDataModules[moduleName] == nil then
+
    if loadedDataModules[moduleName] == nil then
loadedDataModules[moduleName] = mw.loadData(moduleName)
+
        local status, module = pcall(mw.loadData, moduleName)
end
+
        if not status then
return loadedDataModules[moduleName]
+
            status, module = pcall(require, moduleName)
 +
        end
 +
        if status then
 +
            loadedDataModules[moduleName] = module
 +
        else
 +
            error("Failed to load module: " .. moduleName .. "\n" .. module)
 +
        end
 +
    end
 +
    return loadedDataModules[moduleName]
 
end
 
end
  
 
--
 
--
-- Helps capitalise the first letter of each word
+
-- Helps capitalize the first letter of each word
 
--
 
--
 
-- @param first {string}
 
-- @param first {string}
Line 58: Line 81:
 
--
 
--
 
local function tchelper(first, rest)
 
local function tchelper(first, rest)
     return first:upper()..rest:lower()
+
     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'):gsub(" A(n?) ", " a%1 ")
 +
    return s
 
end
 
end
  
Line 69: Line 103:
 
local function hyphenateName(name)
 
local function hyphenateName(name)
 
     local lName = name:lower()
 
     local lName = name:lower()
     return lName:gsub('^%s*(.-)%s*$', '%1'):gsub("%s+", "_")
+
     return lName:gsub('^%s*(.-)%s*$', '%1'):gsub('%s+', '_')
 
end
 
end
  
Line 79: Line 113:
 
--
 
--
 
local function fullUrl(url)
 
local function fullUrl(url)
local newUrl = url
+
    local newUrl = url
if url:sub(1,5) == "https" then
+
    if url:sub(1, 5) == 'https' then
return newUrl
+
        return newUrl
end
+
    end
  
if url:sub(1,1) ~= "/" then
+
    if url:sub(1, 1) ~= '/' then
newUrl = "/" .. newUrl
+
        newUrl = '/' .. newUrl
end
+
    end
  
newUrl = "https://www.play.idlescape.com" .. newUrl
+
    newUrl = 'https://www.play.idlescape.com' .. newUrl
return newUrl
+
    return newUrl
 
end
 
end
  
--  
+
--
 
-- Finds input's id and input's type
 
-- Finds input's id and input's type
 
--
 
--
Line 99: Line 133:
 
-- @return {string}
 
-- @return {string}
 
local function findInputId(name)
 
local function findInputId(name)
local id
+
    local id
local inputType
+
    local inputType
for index, value in ipairs(idListType) do
+
    for index, value in ipairs(idListType) do
id = findId._findId({name, value[1]})
+
        id = findId._findId({name, value[1]})
if type(id) == "number" then
+
        if type(id) == 'number' then
inputType = value[1]
+
            inputType = value[1]
return id , inputType
+
            return id , inputType
end
+
        end
end
+
    end
return 0
+
    return 0
 
end
 
end
  
Line 119: Line 153:
 
--
 
--
 
local function findImageUrl(id, inputType)
 
local function findImageUrl(id, inputType)
local imageUrl
+
    local imageUrl
for index, value in ipairs(idListType) do
+
    for index, value in ipairs(idListType) do
if value[1] == inputType then
+
        if value[1] == inputType then
imageUrl = fullUrl(p.loadData(inputType)[tostring(id)][value[2]])
+
            if inputType == 'item' then
return imageUrl
+
                if p.loadData(inputType)[tostring(id)][value[3]] then
end
+
                    return fullUrl(p.loadData(inputType)[tostring(id)][value[3]])
end
+
                end
return 0
+
            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
 
end
  
Line 136: Line 179:
 
-- @param width {number}
 
-- @param width {number}
 
-- @param height {number}
 
-- @param height {number}
-- @param word {number}
+
-- @param word {bool}
 +
-- @param alt {string}
 +
-- @param nolink {bool}
 
-- @return {string}
 
-- @return {string}
 
--
 
--
local function image(name, url, width, height, word)
+
local function image(name, url, width, height, word, alt, nolink)
local s = url
+
    name = capitalize(name)
s = "src=\"".. s .. "\""
+
    local s = ''
s = s .. " alt=\"" .. name:gsub("(%a)([%w_']*)", tchelper):gsub( " Of "," of "):gsub(" The "," the ") .. "\""
+
    url = ' src=\''.. url .. '\''
s = s .. " width=\"" .. width .. "px\""
+
    alt = ' alt=\''.. alt .. '\''
s = s .. " height=\"" .. height .. "px\""
+
    width = ' width=\'' .. width .. 'px\''
s = '<img ' .. s .. '>'
+
    height = ' height=\'' .. height .. 'px\''
if tonumber(word) == 1 then
+
    local img = '<img' .. url .. alt .. width .. height ..'>'
s = s .. "&nbsp;" .. name:gsub("(%a)([%w_']*)", tchelper):gsub( " Of "," of "):gsub(" The "," the ")
+
    if word then
end
+
        s = img .. ' ' .. name
s = '[[' .. name:gsub("(%a)([%w_']*)", tchelper):gsub( " Of "," of "):gsub(" The "," the ") .. '|' .. s .. ']]'
+
    else
return s
+
        s = img
 +
    end
 +
    if not nolink then
 +
        s = '[[' .. name .. '|' .. s .. ']]'
 +
    end
 +
    if word then
 +
        s = '<span style=\'white-space: nowrap;\'>' .. s .. '</span>'
 +
    end
 +
    return s
 
end
 
end
  
Line 159: Line 212:
 
-- @return {string}
 
-- @return {string}
 
--
 
--
local function findOtherImage(name)
+
local function findOtherImageUrl(name)
local hName = hyphenateName(name)
+
    local h_name = hyphenateName(name)
for key, value in pairs(p.loadData("other")) do
+
    for key, value in pairs(p.loadData('other')) do
if hName == key then
+
        if h_name == key then
return value
+
            return value
end
+
        end
end
+
    end
return 0
+
    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;" .. name:gsub("(%a)([%w_']*)", tchelper):gsub( " Of "," of "):gsub(" The "," the ")
 
end
 
s = s .. ']]'
 
return s
 
 
end
 
end
  
Line 197: Line 229:
 
--
 
--
 
function p.img(frame)
 
function p.img(frame)
return p._img(frame:getParent().args)
+
    return p._img(frame:getParent().args)
 
end
 
end
  
Line 207: Line 239:
 
--
 
--
 
function p._img(_args)
 
function p._img(_args)
local width = _args[2]
+
    local name = _args[1]
local height = _args[3]
+
    local width = _args[2]
local id
+
    local height = _args[3]
local inputType
+
    local id
local url
+
    local input_type
local word = _args["word"]
+
    local url
local otherimage
+
    local word = _args['word'] == '1'
local s
+
    local alt
if not width then
+
    local nolink = _args['nolink'] == '1'
width = 20
+
    local s
end
+
    local error
if not height then
+
    if not width then
height = width
+
        width = 20
end
+
    end
if not _args[1] then
+
    if not height then
return "Couldn't get name"
+
        height = width
end
+
    end
id, inputType = findInputId(_args[1])
+
    if not name then
if id == 0 then
+
        return 'Couldn\'t get name'
otherimage = findOtherImage(_args[1])
+
    end
end
+
    if alternativeNames[hyphenateName(name)] then
if otherimage == 0 then
+
        id = alternativeNames[hyphenateName(name)]
return "Given name doesn't exist in any of the data modules, Check spelling or update data modules"
+
        input_type = 'enchantment'
elseif otherimage then
+
    else
s = otherImage(_args[1], otherimage, width, height, word)
+
        id, input_type = findInputId(name)
return s
+
    end
end
+
    if id == 0 then
url = findImageUrl(id, inputType)
+
        url = findOtherImageUrl(name)
if url == 0 then
+
    end
return "couldn't find image url."
+
    if url == 0 then
end
+
        error = '<span class=\'rt-commentedText tooltip tooltip-dotted\' title=\'Given name does not exist in any of the data modules. Please check spelling or update data modules.\'>' .. name .. '</span>'
s = image(_args[1], url, width, height, word)
+
        return error
return s
+
    end
 +
    if not url then
 +
        url = findImageUrl(id, input_type)
 +
    end
 +
    if url == 0 then
 +
        return 'couldn\'t find image url.'
 +
    end
 +
    if not _args['alt'] then
 +
        alt = name
 +
    else
 +
        alt = _args['alt']
 +
    end
 +
    s = image(name, url, width, height, word, alt, nolink)
 +
    return s
 
end
 
end
  
 
return p
 
return p

Revision as of 20:29, 6 May 2025


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
        local status, module = pcall(mw.loadData, moduleName)
        if not status then
            status, module = pcall(require, moduleName)
        end
        if status then
            loadedDataModules[moduleName] = module
        else
            error("Failed to load module: " .. moduleName .. "\n" .. module)
        end
    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'):gsub(" A(n?) ", " a%1 ")
    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 {bool}
-- @param alt {string}
-- @param nolink {bool}
-- @return {string}
--
local function image(name, url, width, height, word, alt, nolink)
    name = capitalize(name)
    local s = ''
    url = ' src=\''.. url .. '\''
    alt = ' alt=\''.. alt .. '\''
    width = ' width=\'' .. width .. 'px\''
    height = ' height=\'' .. height .. 'px\''
    local img = '<img' .. url .. alt .. width .. height ..'>'
    if word then
        s = img .. ' ' .. name
    else
        s = img
    end
    if not nolink then
        s = '[[' .. name .. '|' .. s .. ']]'
    end
    if word then
        s = '<span style=\'white-space: nowrap;\'>' .. s .. '</span>'
    end
    return s
end

--
-- Finds otherimage
--
-- @param name {string}
-- @return {string}
--
local function findOtherImageUrl(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

--
-- 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 name = _args[1]
    local width = _args[2]
    local height = _args[3]
    local id
    local input_type
    local url
    local word = _args['word'] == '1'
    local alt
    local nolink = _args['nolink'] == '1'
    local s
    local error
    if not width then
        width = 20
    end
    if not height then
        height = width
    end
    if not name then
        return 'Couldn\'t get name'
    end
    if alternativeNames[hyphenateName(name)] then
        id = alternativeNames[hyphenateName(name)]
        input_type = 'enchantment'
    else
        id, input_type = findInputId(name)
    end
    if id == 0 then
        url = findOtherImageUrl(name)
    end
    if url == 0 then
        error = '<span class=\'rt-commentedText tooltip tooltip-dotted\' title=\'Given name does not exist in any of the data modules. Please check spelling or update data modules.\'>' .. name .. '</span>'
        return error
    end
    if not url then
        url = findImageUrl(id, input_type)
    end
    if url == 0 then
        return 'couldn\'t find image url.'
    end
    if not _args['alt'] then
        alt = name
    else
        alt = _args['alt']
    end
    s = image(name, url, width, height, word, alt, nolink)
    return s
end

return p