More actions
Created page with "local p = {} -- escape HTML attribute values becuase I have idea if this exists or not local function escape(s) return (s:gsub('[\"&\'<>]', { ['"'] = """, ["&"] = "&", ["'"] = "'", ["<"] = "<", [">"] = ">" })) end function p.render(frame) local args = frame:getParent().args local name = mw.text.trim(args[1] or "") local size = tonumber(args[2]) or 64 local title = args[3] or ""..." |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p.render(frame) | function p.render(frame) | ||
local args | local args = frame:getParent().args | ||
local name | local name = mw.text.trim(args[1] or "") | ||
local size | local size = tonumber(args[2]) or 64 | ||
local title | local title = mw.text.trim(args[3] or "") | ||
local overlay = (args | local overlay = mw.text.trim(args["overlay"] or "yes") | ||
if name == "" then | if name == "" then | ||
return " | return "Error: No player name provided." | ||
end | |||
-- Construct the image URL | |||
local base = "https://mc-heads.net/avatar/" | |||
local url = string.format("%s%s/%d", base, name, size) | |||
if overlay == "no" then | |||
url = url .. ".png?overlay=false" | |||
end | end | ||
-- Build | -- Build the <img> tag | ||
local | local img = mw.html.create("img") | ||
" | :addClass("mc-player-head") | ||
:attr("src", url) | |||
size | :attr("width", size) | ||
:attr("height", size) | |||
if title ~= "" then | |||
img:attr("title", title) | |||
img:attr("alt", title) | |||
else | |||
img:attr("alt", "") | |||
end | |||
return img | return tostring(img) | ||
end | end | ||
return p | return p |
Revision as of 17:46, 6 July 2025
Documentation for this module may be created at Module:PlayerHead/doc
local p = {}
function p.render(frame)
local args = frame:getParent().args
local name = mw.text.trim(args[1] or "")
local size = tonumber(args[2]) or 64
local title = mw.text.trim(args[3] or "")
local overlay = mw.text.trim(args["overlay"] or "yes")
if name == "" then
return "Error: No player name provided."
end
-- Construct the image URL
local base = "https://mc-heads.net/avatar/"
local url = string.format("%s%s/%d", base, name, size)
if overlay == "no" then
url = url .. ".png?overlay=false"
end
-- Build the <img> tag
local img = mw.html.create("img")
:addClass("mc-player-head")
:attr("src", url)
:attr("width", size)
:attr("height", size)
if title ~= "" then
img:attr("title", title)
img:attr("alt", title)
else
img:attr("alt", "")
end
return tostring(img)
end
return p