Module:HeroImage
From Fisk's Superheroes Wiki
More actions
Documentation for this module may be created at Module:HeroImage/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(value)
if isUrl(value) then
return value
end
if isHtmlImg(value) then
return value
end
return mw.getCurrentFrame():callParserFunction("filepath", value)
end
function p.show(frame)
local src = frame.args[1] or frame.args.src
local scale = tonumber(frame.args.scale) or 1
if not src then
return "''No image provided''"
end
local url = getFileUrl(src)
-- If user provides literal <img>, return it untouched
if isHtmlImg(url) then
return frame:extensionTag("html", url)
end
-- Build <img>
local img = mw.html.create("img")
:attr("src", url)
:css("max-width", 18 * scale .. "px")
:css("height", "auto")
:css("image-rendering", "pixelated")
return frame:extensionTag("html", tostring(img))
end
return p