Module:Slot: Difference between revisions
From Fisk's Superheroes Wiki
More actions
DeathRealms (talk | contribs) mNo edit summary Tags: Manual revert Reverted |
DeathRealms (talk | contribs) mNo edit summary Tag: Manual revert |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function | local function isUrl(value) | ||
local function | return type(value) == "string" and value:match("^https?://") | ||
end | |||
local function isHtmlImg(value) | |||
return type(value) == "string" and value:match("^<img") | |||
end | |||
local function getFileUrl(fileName) | |||
if isUrl(fileName) then | |||
return fileName | |||
end | end | ||
if isHtmlImg(fileName) then | |||
return fileName | |||
end | |||
return mw.getCurrentFrame():callParserFunction("filepath", fileName) | |||
end | |||
function p.slot(frame) | |||
local args = frame.args | local args = frame.args | ||
local scale = args.scale or 1 | local scale = tonumber(args.scale) or 1 | ||
local html = mw.html.create("div") | local html = mw.html.create("div") | ||
| Line 15: | Line 31: | ||
width = "100%", | width = "100%", | ||
height = 18 * scale .. "px", | height = 18 * scale .. "px", | ||
["align-items"] = "center", | ["align-items"] = "center", | ||
["justify-content"] = "center", | ["justify-content"] = "center", | ||
| Line 22: | Line 37: | ||
local slots = {} | local slots = {} | ||
for | for key, value in pairs(args) do | ||
if | if key:match("^slot%d+$") then | ||
slots[ | local index = tonumber(key:sub(5)) | ||
slots[index] = value | |||
end | end | ||
end | end | ||
for | for _, name in ipairs(slots) do | ||
local slot = html:tag("div") | local slot = html:tag("div") | ||
:addClass("slot") | :addClass("slot") | ||
:css{ | :css{ | ||
position = "relative", | position = "relative", | ||
width = 18 * scale .. "px", | width = 18 * scale .. "px", | ||
height = 18 * scale .. "px", | height = 18 * scale .. "px", | ||
| Line 43: | Line 58: | ||
:css{ | :css{ | ||
position = "absolute", | position = "absolute", | ||
["image-rendering"] = "pixelated", | ["image-rendering"] = "pixelated", | ||
width = 18 * scale .. "px", | width = 18 * scale .. "px", | ||
height = 18 * scale .. "px", | height = 18 * scale .. "px", | ||
-- The image is shifted to the right when in an Infobox if the top and left are not set to 0 | |||
top = "0", | top = "0", | ||
left = "0", | left = "0", | ||
} | } | ||
slot:tag("img") | local value = name | ||
if isHtmlImg(value) then | |||
slot:wikitext(value) | |||
else | |||
slot:tag("img") | |||
:attr("src", getFileUrl(value)) | |||
:css{ | |||
position = "absolute", | |||
["image-rendering"] = "pixelated", | |||
top = scale .. "px", | |||
left = scale .. "px", | |||
width = 16 * scale .. "px", | |||
height = 16 * scale .. "px", | |||
} | |||
end | |||
end | end | ||
return frame:extensionTag("html", tostring(html)) | return frame:extensionTag("html", tostring(html)) | ||
end | end | ||
return p | return p | ||
Latest revision as of 21:27, 15 November 2025
Documentation for this module may be created at Module:Slot/doc
local p = {}
local function isUrl(value)
return type(value) == "string" and value:match("^https?://")
end
local function isHtmlImg(value)
return type(value) == "string" and value:match("^<img")
end
local function getFileUrl(fileName)
if isUrl(fileName) then
return fileName
end
if isHtmlImg(fileName) then
return fileName
end
return mw.getCurrentFrame():callParserFunction("filepath", fileName)
end
function p.slot(frame)
local args = frame.args
local scale = tonumber(args.scale) or 1
local html = mw.html.create("div")
:addClass("slots")
:css{
position = "relative",
width = "100%",
height = 18 * scale .. "px",
["align-items"] = "center",
["justify-content"] = "center",
display = "flex",
}
local slots = {}
for key, value in pairs(args) do
if key:match("^slot%d+$") then
local index = tonumber(key:sub(5))
slots[index] = value
end
end
for _, name in ipairs(slots) do
local slot = html:tag("div")
:addClass("slot")
:css{
position = "relative",
width = 18 * scale .. "px",
height = 18 * scale .. "px",
margin = scale .. "px",
}
slot:tag("img")
:attr("src", getFileUrl("Slot.png"))
:css{
position = "absolute",
["image-rendering"] = "pixelated",
width = 18 * scale .. "px",
height = 18 * scale .. "px",
-- The image is shifted to the right when in an Infobox if the top and left are not set to 0
top = "0",
left = "0",
}
local value = name
if isHtmlImg(value) then
slot:wikitext(value)
else
slot:tag("img")
:attr("src", getFileUrl(value))
:css{
position = "absolute",
["image-rendering"] = "pixelated",
top = scale .. "px",
left = scale .. "px",
width = 16 * scale .. "px",
height = 16 * scale .. "px",
}
end
end
return frame:extensionTag("html", tostring(html))
end
return p