We are actively looking for staff to help us build the wiki. If you are interested please join our Discord server and apply.

Module:InfoboxModuleTest: Difference between revisions

From Moonbounce Wiki
Jump to navigation Jump to search
(Hopefully add images and Nothing detection)
m (Testing)
Line 6: Line 6:
   local items = mw.text.split(foundIn, ',')
   local items = mw.text.split(foundIn, ',')
   local result = ""
   local result = ""
  local nothingImage = "X.png"


   -- if there are no items, add "Nothing" as a placeholder
   -- if there are no items, add "Nothing" as a placeholder
Line 17: Line 15:
     item = mw.text.trim(item)
     item = mw.text.trim(item)


     -- generate an image name from the item name by replacing spaces with underscores, then appending ".png"
     -- generate an image name from the item name by replacing spaces with underscores
     local imageName = string.gsub(item, " ", "_")
     local imageName = string.gsub(item, " ", "_")
    imageName = imageName .. ".png"
      
      
     if item == "Nothing" then
     if item == "Nothing" then
       imageName = nothingImage
       imageName = "X"
     end
     end


     if not mw.ustring.match(imageName, "File:") then
     -- construct the new format
      imageName = "File:" .. imageName
    result = result .. '[[File:' .. imageName .. '.png|24px|link=]]  ' .. item
    end


     -- if the item is surrounded by [[ ]], remove them and mark it as a link
     -- add a separator if not the last item
     if string.sub(item, 1, 2) == "[[" and string.sub(item, -2) == "]]" then
     if i < #items then
      item = string.sub(item, 3, -3)
       result = result .. ' '
      result = result .. '<div class="found-in-item">'
      result = result .. '<a href="/wiki/' .. item .. '">'
       result = result .. '<img src="/images/' .. imageName .. '" alt="' .. item .. '" title="' .. item .. '">'
      -- close the tags
      result = result .. '</a></div>'
    else
      result = result .. '<div class="found-in-item">'
      result = result .. '<img src="/images/' .. imageName .. '" alt="' .. item .. '" title="' .. item .. '">'
      -- close the tags
      result = result .. '</div>'
     end
     end
   end
   end

Revision as of 10:26, 29 June 2024

Documentation for this module may be created at Module:InfoboxModuleTest/doc

-- Module:InfoboxTestModule
local p = {}

function p.displayFoundIn(frame)
  local foundIn = frame.args[1]
  local items = mw.text.split(foundIn, ',')
  local result = ""

  -- if there are no items, add "Nothing" as a placeholder
  if #items == 0 then
    items = {"Nothing"}
  end

  for i, item in ipairs(items) do
    item = mw.text.trim(item)

    -- generate an image name from the item name by replacing spaces with underscores
    local imageName = string.gsub(item, " ", "_")
    
    if item == "Nothing" then
      imageName = "X"
    end

    -- construct the new format
    result = result .. '[[File:' .. imageName .. '.png|24px|link=]]&nbsp;&nbsp;' .. item

    -- add a separator if not the last item
    if i < #items then
      result = result .. ' '
    end
  end

  return result
end

return p