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)
(Remove # from image URL (thanks Style Crate #1!))
 
(8 intermediate revisions by 2 users not shown)
Line 3: Line 3:


function p.displayFoundIn(frame)
function p.displayFoundIn(frame)
  -- Get the first argument passed to the module
   local foundIn = frame.args[1]
   local foundIn = frame.args[1]
  -- Split the string into a table based on the comma delimiter
   local items = mw.text.split(foundIn, ',')
   local items = mw.text.split(foundIn, ',')
  -- Initialize the result string
   local result = ""
   local result = ""


  local nothingImage = "X.png"
   -- If no items are found, add "Nothing" to the list
 
   -- if there are no items, add "Nothing" as a placeholder
   if #items == 0 then
   if #items == 0 then
     items = {"Nothing"}
     items = {"Nothing"}
   end
   end


  -- if the item is wrapped in brackets, remove them
  for i, item in ipairs(items) do
    items[i] = item:gsub("%[", ""):gsub("%]", "")
  end
  -- Iterate over the items and create a div element for each
   for i, item in ipairs(items) do
   for i, item in ipairs(items) do
     item = mw.text.trim(item)
     item = mw.text.trim(item)
    -- Create a version of the item name that can be used as an image name
    local imageName = string.gsub(item, " ", "_")


     -- generate an image name from the item name by replacing spaces with underscores, then appending ".png"
     -- If the item is "Nothing", use the X image
    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
    local link_name = item
       imageName = "File:" .. imageName
     if item ~= "Nothing" then
       link_name = "[[" .. item .. "]]"
     end
     end


     -- if the item is surrounded by [[ ]], remove them and mark it as a link
     -- remove every # from the iamge name
     if string.sub(item, 1, 2) == "[[" and string.sub(item, -2) == "]]" then
     imageName = imageName:gsub("#", "")
      item = string.sub(item, 3, -3)
 
      result = result .. '<div class="found-in-item">'
    -- Open the div element
      result = result .. '<a href="/wiki/' .. item .. '">'
    result = result .. '<div class="item_source" style="display: flex; gap: 5px;">'
      result = result .. '<img src="/images/' .. imageName .. '" alt="' .. item .. '" title="' .. item .. '">'
    -- Add the image and item name to the div element
      -- close the tags
     result = result .. '[[File:' .. imageName .. '.png|24px|link=]]' .. link_name
      result = result .. '</a></div>'
    -- Close the div element
     else
    result = result .. '</div>\n'
      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



Latest revision as of 06:23, 4 July 2024

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

-- Module:InfoboxTestModule
local p = {}

function p.displayFoundIn(frame)
  -- Get the first argument passed to the module
  local foundIn = frame.args[1]
  -- Split the string into a table based on the comma delimiter
  local items = mw.text.split(foundIn, ',')
  -- Initialize the result string
  local result = ""

  -- If no items are found, add "Nothing" to the list
  if #items == 0 then
    items = {"Nothing"}
  end

  -- if the item is wrapped in brackets, remove them
  for i, item in ipairs(items) do
    items[i] = item:gsub("%[", ""):gsub("%]", "")
  end

  -- Iterate over the items and create a div element for each
  for i, item in ipairs(items) do
    item = mw.text.trim(item)
    -- Create a version of the item name that can be used as an image name
    local imageName = string.gsub(item, " ", "_")

    -- If the item is "Nothing", use the X image
    if item == "Nothing" then
      imageName = "X"
    end

    local link_name = item
    if item ~= "Nothing" then
      link_name = "[[" .. item .. "]]"
    end

    -- remove every # from the iamge name
    imageName = imageName:gsub("#", "")

    -- Open the div element 
    result = result .. '<div class="item_source" style="display: flex; gap: 5px;">'
    -- Add the image and item name to the div element
    result = result .. '[[File:' .. imageName .. '.png|24px|link=]]' .. link_name
    -- Close the div element
    result = result .. '</div>\n'
  end

  return result
end

return p