Difference between revisions of "Module:Silent1/sandbox"
Jump to navigation
Jump to search
m |
Tag: Undo |
||
(33 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
− | |||
local origArgs = {} | local origArgs = {} | ||
− | local | + | local name |
− | local | + | local width = 'auto' |
− | local | + | local height = 'auto' |
− | + | local add_word | |
− | local | + | local only_url |
− | local | + | local no_link |
− | local | + | local link |
− | local | ||
− | local | + | local data_module_names = { |
− | + | --item = 'Module:Items/data', | |
− | + | item = 'Module:Silent1/sandbox/data', | |
− | + | other = 'Module:OtherImages/data' | |
− | + | } | |
− | + | local loaded_data_modules = {} | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | function p.loadData (data_type) | |
− | + | local module_name = data_module_names[data_type] | |
− | + | if loaded_data_modules[module_name] == nil then | |
− | + | loaded_data_modules[module_name] = mw.loadData(module_name) | |
− | local | ||
− | |||
− | |||
− | |||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | return loaded_data_modules[module_name] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | -- | + | --Search the item data for item name and return its id |
− | + | function p.findItemID(name) | |
− | + | local items = p.loadData('item') | |
− | + | local lname = string.lower(name) | |
− | + | local id = nil | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | for k,v in pairs(items) do | |
− | + | if lname == string.lower(v['name']) then | |
− | + | id = k | |
− | + | return id | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | for k, | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
+ | return id | ||
end | end | ||
− | -- | + | --Find the image from the name |
− | local | + | -- function p.findImage(name) |
− | + | -- local id | |
+ | -- name = string.lower(name) | ||
+ | -- local image = p.loadData('other')[name] | ||
− | + | -- --If name is a number, pass in as an item ID | |
− | + | -- if tonumber(name) then | |
− | + | -- id = name | |
− | + | -- 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 | |
− | |||
− | |||
− | local | + | function p.findImage(name) |
− | + | local id | |
− | + | name = string.lower(name) | |
− | + | local image = p.loadData('other')[name] | |
− | |||
− | + | if not image then | |
− | if | + | image = p.loadData('item')[name] |
− | |||
− | |||
end | end | ||
− | + | return image | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | return | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | -- | + | --Create a string to output |
− | + | local function _image() | |
− | local function | + | local image = p.findImage(name) |
− | + | local s = '' | |
− | + | local rs = nil | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | if image then | |
− | + | local realname = image['name'] | |
− | + | local url = image['image'] | |
− | + | ||
− | + | if url:sub(1,1) ~= '/' then url = '/' .. url end | |
− | + | url = 'https://idlescape.com' .. url | |
− | + | s = url | |
− | + | ||
− | + | if only_url == '1' then rs = s end | |
− | + | ||
− | + | s = 'src="' .. s .. '"' | |
− | + | s = s .. ' alt="' .. realname .. '"' | |
− | + | s = s .. ' width="' .. width .. '"' | |
− | + | s = s .. ' height="' .. height .. '"' | |
− | + | s = '<img ' .. s .. '>' | |
− | + | ||
− | + | if add_word == '1' then | |
− | + | s = s .. ' ' .. realname | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
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 rs | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | function p.image(frame) | |
− | + | return p._image(frame:getParent()) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
end | end | ||
− | + | function p._image(frame) | |
− | + | origArgs = frame.args | |
− | function p. | + | name = origArgs[1] |
− | + | arg = origArgs[2] | |
− | + | if not arg then | |
− | + | width = 20 | |
− | + | else arg:match('%S') | |
+ | width = arg | ||
end | end | ||
+ | arg = origArgs[3] | ||
+ | if arg and arg:match('%S') then height = arg end | ||
+ | add_word = origArgs['word'] | ||
+ | only_url = origArgs['url'] | ||
+ | no_link = origArgs['nolink'] | ||
+ | link = origArgs['link'] | ||
− | + | if not name then return '' end | |
− | + | return _image() | |
− | return | ||
end | end | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
return p | return p |
Latest revision as of 19:07, 14 February 2022
Documentation for this module may be created at Module:Silent1/sandbox/doc
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 = { --item = 'Module:Items/data', item = 'Module:Silent1/sandbox/data', other = 'Module:OtherImages/data' } local loaded_data_modules = {} function p.loadData (data_type) local module_name = data_module_names[data_type] if loaded_data_modules[module_name] == nil then loaded_data_modules[module_name] = mw.loadData(module_name) end return loaded_data_modules[module_name] end --Search the item data for item name and return its id function p.findItemID(name) local items = p.loadData('item') local lname = string.lower(name) local id = nil for k,v in pairs(items) do if lname == string.lower(v['name']) then id = k return id end end return id end --Find the image from the name -- function p.findImage(name) -- local id -- name = string.lower(name) -- local image = p.loadData('other')[name] -- --If name is a number, pass in as an item ID -- if tonumber(name) then -- id = name -- 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 function p.findImage(name) local id name = string.lower(name) 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() local image = p.findImage(name) local s = '' local rs = nil if image then local realname = image['name'] local url = image['image'] if url:sub(1,1) ~= '/' then url = '/' .. url end url = 'https://idlescape.com' .. url s = url if only_url == '1' then rs = s end s = 'src="' .. s .. '"' 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 function p.image(frame) return p._image(frame:getParent()) end function p._image(frame) origArgs = frame.args name = origArgs[1] arg = origArgs[2] if not arg then width = 20 else arg:match('%S') width = arg end arg = origArgs[3] if arg and arg:match('%S') then height = arg end add_word = origArgs['word'] only_url = origArgs['url'] no_link = origArgs['nolink'] link = origArgs['link'] if not name then return '' end return _image() end return p