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

Module:VanillaImage: Difference between revisions

From Vault Hunters Official Wiki
Initial creation
 
mNo edit summary
 
Line 10: Line 10:
     local alt = frame.args.alt or imageName
     local alt = frame.args.alt or imageName
     local link = frame.args.link or ""
     local link = frame.args.link or ""
    local prefix = frame.args.prefix or "auto"
      
      
     -- Remove any file extension if provided
     -- Remove any file extension if provided
Line 15: Line 16:
      
      
     -- Clean up the image name (replace spaces with underscores)
     -- Clean up the image name (replace spaces with underscores)
    imageName = imageName:gsub(" ", "_")
   
    -- Construct the URL to minecraft.wiki
    local baseUrl = "https://minecraft.wiki/images/"
   
    -- For the actual file path, we need the MD5 hash structure that MediaWiki uses
    -- Since we can't calculate MD5 in Lua easily, we'll use the direct Special:Redirect approach
    local imageUrl = "https://minecraft.wiki/w/Special:Redirect/file/" .. mw.uri.encode(imageName .. ".png", "PATH")
   
    -- Build the image HTML
    local linkStart = ""
    local linkEnd = ""
   
    if link ~= "" then
        linkStart = "[[" .. link .. "|"
        linkEnd = "]]"
    end
   
    -- Return the formatted image
    return linkStart .. "[[File:" .. imageUrl .. "|" .. size .. "|alt=" .. alt .. "]]" .. linkEnd
end
function p.direct(frame)
    -- Direct URL approach - displays external image
    local imageName = frame.args[1] or frame.args.name or ""
    local size = frame.args.size or frame.args[2] or "32"
    local alt = frame.args.alt or imageName
    local link = frame.args.link or ""
    local prefix = frame.args.prefix or "auto"
   
    -- Remove any file extension if provided
    imageName = imageName:gsub("%.png$", ""):gsub("%.gif$", "")
   
    -- Clean up the image name
     imageName = imageName:gsub(" ", "_")
     imageName = imageName:gsub(" ", "_")
      
      
Line 58: Line 25:
     end
     end
      
      
     -- Construct the direct image URL
     -- Construct the URL to minecraft.wiki
     local imageUrl = "https://minecraft.wiki/w/Special:Redirect/file/" .. mw.uri.encode(imageName .. ".png", "PATH")
     local imageUrl = "https://minecraft.wiki/w/Special:Redirect/file/" .. mw.uri.encode(imageName .. ".png", "PATH")
      
      
     -- Parse size (remove 'px' if present)
     -- Make sure size has 'px' suffix
     local sizeNum = size:gsub("px$", "")
     if not size:match("px$") then
        size = size .. "px"
    end
      
      
     -- Build HTML
     -- Build the wikitext for external image
     local html = mw.html.create('img')
     local wikitext = "[" .. imageUrl .. " " .. size .. "]"
        :attr('src', imageUrl)
        :attr('alt', alt)
        :attr('width', sizeNum)
        :addClass('minecraft-item')
      
      
    -- Wrap in link if specified
     if link ~= "" then
     if link ~= "" then
         local linkHtml = mw.html.create('a')
         wikitext = "[[" .. link .. "|" .. wikitext .. "]]"
            :attr('href', mw.title.new(link):fullUrl())
            :node(html)
        return tostring(linkHtml)
     end
     end
      
      
     return tostring(html)
     return wikitext
end
end
-- Alias 'direct' to 'item' for backwards compatibility
p.direct = p.item


return p
return p

Latest revision as of 05:42, 16 December 2025

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

-- Module:MinecraftImage
-- Links to images from minecraft.wiki

local p = {}

function p.item(frame)
    -- Get the image name from the template parameter
    local imageName = frame.args[1] or frame.args.name or ""
    local size = frame.args.size or frame.args[2] or "32px"
    local alt = frame.args.alt or imageName
    local link = frame.args.link or ""
    local prefix = frame.args.prefix or "auto"
    
    -- Remove any file extension if provided
    imageName = imageName:gsub("%.png$", ""):gsub("%.gif$", "")
    
    -- Clean up the image name (replace spaces with underscores)
    imageName = imageName:gsub(" ", "_")
    
    -- Add Invicon_ prefix automatically if not already present
    if prefix == "auto" and not imageName:match("^Invicon_") then
        imageName = "Invicon_" .. imageName
    elseif prefix ~= "auto" and prefix ~= "" then
        imageName = prefix .. imageName
    end
    
    -- Construct the URL to minecraft.wiki
    local imageUrl = "https://minecraft.wiki/w/Special:Redirect/file/" .. mw.uri.encode(imageName .. ".png", "PATH")
    
    -- Make sure size has 'px' suffix
    if not size:match("px$") then
        size = size .. "px"
    end
    
    -- Build the wikitext for external image
    local wikitext = "[" .. imageUrl .. " " .. size .. "]"
    
    -- Wrap in link if specified
    if link ~= "" then
        wikitext = "[[" .. link .. "|" .. wikitext .. "]]"
    end
    
    return wikitext
end

-- Alias 'direct' to 'item' for backwards compatibility
p.direct = p.item

return p