Toggle menu
66
63
2
559
Fisk's Superheroes Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:HeroImage: Difference between revisions

From Fisk's Superheroes Wiki
Created page with "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...."
 
mNo edit summary
Line 2: Line 2:


local function isUrl(value)
local function isUrl(value)
    return type(value) == "string" and value:match("^https?://")
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")
return type(value) == "string" and value:match("^<img")
end
end


local function getFileUrl(value)
local function getFileUrl(value)
    if isUrl(value) then
if isUrl(value) then return value end
        return value
if isHtmlImg(value) then return value end
    end
    if isHtmlImg(value) then
        return value
    end


    return mw.getCurrentFrame():callParserFunction("filepath", value)
return mw.getCurrentFrame():callParserFunction("filepath", value)
end
end


function p.show(frame)
function p.show(frame)
    local src = frame.args[1] or frame.args.src
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


    if not src then
local url = getFileUrl(src)
        return "''No image provided''"
local width = tonumber(frame.args.width) or 250
    end


    local url = getFileUrl(src)
-- If plain <img> provided, return as-is
if isHtmlImg(url) then
return frame:extensionTag("html", url)
end


    -- If user provides literal <img>, return it untouched
local img = mw.html.create("img")
    if isHtmlImg(url) then
:attr("src", url)
        return frame:extensionTag("html", url)
:css("width", width .. "px")
    end
:css("height", "auto")


    -- Build <img>
return frame:extensionTag("html", tostring(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
end


return p
return p

Revision as of 06:00, 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

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)
	local width = tonumber(frame.args.width) or 250

	-- If plain <img> provided, return as-is
	if isHtmlImg(url) then
		return frame:extensionTag("html", url)
	end

	local img = mw.html.create("img")
		:attr("src", url)
		:css("width", width .. "px")
		:css("height", "auto")

	return frame:extensionTag("html", tostring(img))
end

return p