Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:PlayerHead: Difference between revisions

From Vault Hunters Official Wiki
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
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
--------------------------------------------------------------------------------
-- Renders a Minecraft player head using https://mc-heads.net/avatar/{name}/{size}
-- Requires: Extension:ExternalImage (provides <img …> parser tag)
--------------------------------------------------------------------------------
local p = {}
local p = {}


-- escape HTML attribute values becuase I have idea if this exists or not
-- Helper to strip “px” or other non-digits from a size string.
local function escape(s)
local function cleanSize( s )
     return (s:gsub('[\"&\'<>]', {
     s = (s or ''):match('^%s*(%d+)')  -- keep leading digits only
        ['"'] = "&quot;",
     return tonumber(s) or 64
        ["&"] = "&amp;",
        ["'"] = "&#39;",
        ["<"] = "&lt;",
        [">"] = "&gt;"
     }))
end
end


function p.render(frame)
function p.render( frame )
     local args   = frame:getParent().args
     local args     = frame:getParent().args
     local name   = mw.text.trim(args[1] or "")
     local name     = mw.text.trim( args[1] or '' )
     local size  = tonumber(args[2]) or 64
     if name == '' then
    local title  = args[3] or ""
        return 'Error: No player name provided.'
     local overlay = (args.overlay or "yes"):lower()  -- "yes" (default) or "no"
     end


     if name == "" then
     local size    = cleanSize( args[2] or '64' )
        return "<strong class=\"error\">PlayerHead: no player specified</strong>"
    local title    = mw.text.trim( args[3] or '' )
    local overlay  = ( (args.overlay or 'yes'):lower() ~= 'no' )  -- boolean
 
    -- Build URL
    local url = string.format( 'https://mc-heads.net/avatar/%s/%d', name, size )
    if not overlay then
        url = url .. '?overlay=false'
     end
     end


     -- Build URL – MC-Heads supports sizes 8 - 600 px and an optional /nohelm suffix :contentReference[oaicite:0]{index=0}
     -- Build <img> via the ExternalImage tag (so it’s parsed, not escaped)
     local url = string.format(
     local img = frame:extensionTag(
         "https://mc-heads.net/avatar/%s/%d%s",
         'img',                                -- tag name supplied by the extension
         escape(name),
        '',                                   -- no inner content
        size,
         {
        overlay == "no" and "/nohelm" or ""
            src    = url,
            width  = tostring(size) .. "px",
            height = tostring(size) .. "px",
            alt    = title ~= '' and title or ('Head of ' .. name),
            class = "mc-player-head"
        }
     )
     )


     -- Make da tag
     -- Optional wrapper span so you can style all heads via .mc-player-head
    local img = '<img class="mc-player-head"'
     return tostring(img)
        .. string.format(' src="%s"', url)
        .. string.format(' width="%d" height="%d"', size, size)
        .. (title ~= "" and string.format(' title="%s" alt="%s"', escape(title), escape(title)) or ' alt=""')
        .. ' />'
 
     return img
end
end


return p
return p

Latest revision as of 18:48, 6 July 2025

Documentation for this module may be created at Module:PlayerHead/doc

--------------------------------------------------------------------------------
-- Renders a Minecraft player head using https://mc-heads.net/avatar/{name}/{size}
-- Requires: Extension:ExternalImage (provides <img …> parser tag)
--------------------------------------------------------------------------------
local p = {}

-- Helper to strip “px” or other non-digits from a size string.
local function cleanSize( s )
    s = (s or ''):match('^%s*(%d+)')   -- keep leading digits only
    return tonumber(s) or 64
end

function p.render( frame )
    local args     = frame:getParent().args
    local name     = mw.text.trim( args[1] or '' )
    if name == '' then
        return 'Error: No player name provided.'
    end

    local size     = cleanSize( args[2] or '64' )
    local title    = mw.text.trim( args[3] or '' )
    local overlay  = ( (args.overlay or 'yes'):lower() ~= 'no' )   -- boolean

    -- Build URL
    local url = string.format( 'https://mc-heads.net/avatar/%s/%d', name, size )
    if not overlay then
        url = url .. '?overlay=false'
    end

    -- Build <img> via the ExternalImage tag (so it’s parsed, not escaped)
    local img = frame:extensionTag(
        'img',                                 -- tag name supplied by the extension
        '',                                    -- no inner content
        {
            src    = url,
            width  = tostring(size) .. "px",
            height = tostring(size) .. "px",
            alt    = title ~= '' and title or ('Head of ' .. name),
            class = "mc-player-head"
        }
    )

    -- Optional wrapper span so you can style all heads via .mc-player-head
    return tostring(img)
end

return p