Module:HeroImage: Difference between revisions
From Fisk's Superheroes Wiki
More actions
DeathRealms (talk | contribs) mNo edit summary |
DeathRealms (talk | contribs) mNo edit summary |
||
| Line 2: | Line 2: | ||
local function isUrl(value) | local function isUrl(value) | ||
return type(value) == "string" and value:match("^https?://") | |||
end | end | ||
local function isHtmlImg(value) | local function isHtmlImg(value) | ||
return type(value) == "string" and value:match("^<img") | |||
end | end | ||
local function getFileUrl(value) | 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 | |||
local function getDimensions(url) | |||
local title = mw.ext.imageinfo.getImageInfo{ url = url } | |||
if not title or not title.width or not title.height then | |||
return nil, nil | |||
end | |||
return tonumber(title.width), tonumber(title.height) | |||
end | end | ||
function p.show(frame) | function p.show(frame) | ||
local src = frame.args[1] or frame.args.src | |||
if not src then | |||
return "''No image provided''" | |||
end | |||
local url = getFileUrl(src) | |||
if isHtmlImg(url) then | |||
return frame:extensionTag("html", url) | |||
end | |||
local override = tonumber(frame.args.width) | |||
local width, height = getDimensions(url) | |||
local finalWidth | |||
if override then | |||
finalWidth = override | |||
elseif width and height then | |||
local isPortrait = height > width | |||
if isPortrait then | |||
finalWidth = 250 | |||
else | |||
finalWidth = 400 | |||
end | |||
else | |||
finalWidth = 300 | |||
end | |||
local img = mw.html.create("img") | |||
:attr("src", url) | |||
:css("width", finalWidth .. "px") | |||
:css("height", "auto") | |||
:css("max-width", finalWidth .. "px") | |||
return frame:extensionTag("html", tostring(img)) | |||
end | end | ||
return p | return p | ||
Revision as of 06:01, 16 November 2025
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
local function getDimensions(url)
local title = mw.ext.imageinfo.getImageInfo{ url = url }
if not title or not title.width or not title.height then
return nil, nil
end
return tonumber(title.width), tonumber(title.height)
end
function p.show(frame)
local src = frame.args[1] or frame.args.src
if not src then
return "''No image provided''"
end
local url = getFileUrl(src)
if isHtmlImg(url) then
return frame:extensionTag("html", url)
end
local override = tonumber(frame.args.width)
local width, height = getDimensions(url)
local finalWidth
if override then
finalWidth = override
elseif width and height then
local isPortrait = height > width
if isPortrait then
finalWidth = 250
else
finalWidth = 400
end
else
finalWidth = 300
end
local img = mw.html.create("img")
:attr("src", url)
:css("width", finalWidth .. "px")
:css("height", "auto")
:css("max-width", finalWidth .. "px")
return frame:extensionTag("html", tostring(img))
end
return p