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
Jump to navigation
Jump to search
m (Rename module) |
(Hopefully add images and Nothing detection) |
||
Line 1: | Line 1: | ||
-- Module: | -- Module:InfoboxTestModule | ||
local p = {} | local p = {} | ||
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 #items == 0 then | |||
items = {"Nothing"} | |||
end | |||
for i, item in ipairs(items) do | for i, item in ipairs(items) do | ||
item = mw.text.trim(item) | item = mw.text.trim(item) | ||
result = result .. '<div class="found-in-item">' .. | |||
-- generate an image name from the item name by replacing spaces with underscores, then appending ".png" | |||
local imageName = string.gsub(item, " ", "_") | |||
imageName = imageName .. ".png" | |||
if item == "Nothing" then | |||
imageName = nothingImage | |||
end | |||
if not mw.ustring.match(imageName, "File:") then | |||
imageName = "File:" .. imageName | |||
end | |||
-- if the item is surrounded by [[ ]], remove them and mark it as a link | |||
if string.sub(item, 1, 2) == "[[" and string.sub(item, -2) == "]]" then | |||
item = string.sub(item, 3, -3) | |||
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 | ||
Revision as of 01:47, 25 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 = "" local nothingImage = "X.png" -- 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, then appending ".png" local imageName = string.gsub(item, " ", "_") imageName = imageName .. ".png" if item == "Nothing" then imageName = nothingImage end if not mw.ustring.match(imageName, "File:") then imageName = "File:" .. imageName end -- if the item is surrounded by [[ ]], remove them and mark it as a link if string.sub(item, 1, 2) == "[[" and string.sub(item, -2) == "]]" then item = string.sub(item, 3, -3) 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 return result end return p