Difference between revisions of "Module:Img"

From Idlescape Wiki
Jump to navigation Jump to search
m
(Fix do not capitalize ' A ' or ' An ')
 
(33 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
local p = {}
 
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 = {
+
local findId = require('Module:FindId')
item = 'Module:Items/data',
+
 
monster = 'Module:Monsters/data',
+
local dataModuleNames = {
location = 'Module:Location/data',
+
    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 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',
end
+
        [3] = 'itemIcon'
+
    },
return loaded_data_modules[module_name]
+
    [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
 
end
  
--Search the item data for item name and return its id
+
--
local function findIdByName(name, type)
+
-- Capitalize the first letter of a word in a string
local items = p.loadData(type)
+
--
local lname = string.lower(name)
+
-- @param s {string}
local id = nil
+
-- @return {string}
+
--
for k,v in pairs(items) do
+
local function capitalize(s)
if lname == string.lower(v.name) then
+
    s = s:gsub('(%a)([%w_\']*)', tchelper):gsub(' Of ',' of '):gsub(' The ',' the '):gsub('Ii','II'):gsub(" A(n?) ", " a%1 ")
id = k
+
    return s
return id
 
end
 
end
 
return id
 
 
end
 
end
  
--Find the image from the name
+
--
function p.findImage(name)
+
-- Hyphenates name
local id
+
--
name = string.lower(name)
+
-- @param name {string}
local image = p.loadData('item')[name]
+
-- @return {string}
+
--
--If name is a number, pass in as an item ID
+
local function hyphenateName(name)
if tonumber(name) then
+
    local lName = name:lower()
id = name
+
    return lName:gsub('^%s*(.-)%s*$', '%1'):gsub('%s+', '_')
else
 
id = p.findItemID(name)
 
end
 
 
--If id is valid
 
if 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}
 
 
if not (image.name and image.image) then
 
image = nil
 
end
 
end
 
 
return image
 
 
end
 
end
  
--Find the image from the name
+
--
local function findImage(name, origSource)
+
-- Generates a full url
local id = nil
+
--
local source = origSource
+
-- @param url {string}
if source == '' then
+
-- @return {string}
for k,v in pairs(data_module_names) do
+
--
id = findIdByName(name, k)
+
local function fullUrl(url)
if id ~= nil then
+
    local newUrl = url
source = k
+
    if url:sub(1, 5) == 'https' then
break
+
        return newUrl
end
+
    end
end
+
 
else
+
    if url:sub(1, 1) ~= '/' then
id = findIdByName(name, source)
+
        newUrl = '/' .. newUrl
end
+
    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
  
local sourceData = p.loadData(source)[id]
+
--
realname = sourceData.name
+
-- Generates string with page link and <img> tag
if source == 'item' then
+
--
local item = sourceData
+
-- @param name {string}
if item.itemIcon then
+
-- @param url {string}
return item.itemIcon
+
-- @param width {number}
else  
+
-- @param height {number}
return item.itemImage
+
-- @param word {bool}
end
+
-- @param alt {string}
elseif source == 'monster' then
+
-- @param nolink {bool}
return sourceData.image
+
-- @return {string}
elseif source == 'location' then
+
--
return sourceData.locationImage
+
local function image(name, url, width, height, word, alt, nolink)
end
+
    name = capitalize(name)
return ''
+
    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
 
end
  
--Create a string to output
+
--
local function formatImage(url)
+
-- Finds otherimage
local s = ''
+
--
local rs = nil
+
-- @param name {string}
+
-- @return {string}
if url then
+
--
+
local function findOtherImageUrl(name)
if url:sub(1,1) ~= '/' then url = '/' .. url end
+
    local h_name = hyphenateName(name)
url = 'https://idlescape.com' .. url
+
    for key, value in pairs(p.loadData('other')) do
s = url
+
        if h_name == key then
+
            return value
if only_url == '1' then rs = s end
+
        end
+
    end
s = 'src="' .. s .. '"'
+
    return 0
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
 
end
  
function p.image(frame)
+
--
return p._image(frame:getParent().args)
+
-- 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(args)
+
--
local name = args[1]
+
-- img method to allow it to be called by other modules
width = 20
+
--
height = 20
+
-- @param _args {table}
add_word = false
+
-- @return {string}
realname = ''
+
--
local source = ''
+
function p._img(_args)
if args[2] then
+
    local name = _args[1]
width = args[2]
+
    local width = _args[2]
end
+
    local height = _args[3]
if args[3] then
+
    local id
height = args[3]
+
    local input_type
end
+
    local url
if args[4] then
+
    local word = _args['word'] == '1'
add_word = args[4]
+
    local alt
end
+
    local nolink = _args['nolink'] == '1'
if args[5] then
+
    local s
source = args[5]
+
    local error
end
+
    if not width then
+
        width = 20
if not name then return '' end
+
    end
local image = findImage(name, source)
+
    if not height then
return formatImage(image)
+
        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
 
end
  
 
return p
 
return p

Latest revision as of 19:00, 30 April 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
        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'):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