Module:Sandbox/LVL

From The Deadlock Wiki
Revision as of 21:33, 5 August 2025 by LVL (talk | contribs)
Jump to navigation Jump to search

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

-- This module powers the Plink template, automatically finding the correct file extension for an icon.
local p = {}

function p.main(frame)
    -- Get the arguments passed from the template.
    local args = frame:getParent().args
    
    local link_target = args[1] or ''
    local display_text = args.altText or args[2] or link_target
    local base_filename = args.pic or link_target
    local size = args.size or args[3] or '24'

    -- A list of common file extensions to check, in order of preference.
    local extensions = { '.png', '.svg', '.jpg', '.jpeg', '.gif' }
    
    local final_filename = base_filename .. '.png' -- Default to .png to create a redlink if no file is found.
    
    -- Loop through the extensions and check if a file with that name exists.
    for _, ext in ipairs(extensions) do
        local title = mw.title.new('File:' .. base_filename .. ext)
        if title and title.exists then
            final_filename = base_filename .. ext
            break -- Stop searching once we find a match.
        end
    end
    
    -- Build the final wikitext output, same as the original template.
    local file_link = string.format('[[File:%s|link=%s|%spx]]', final_filename, link_target, size)
    local text_link = string.format('[[%s|%s]]', link_target, display_text)
    local span_tag = string.format('<span class="pic-link" style="filter: brightness(100) saturate(100)%%;">%s</span>', file_link) -- Use %% to escape the % character
    
    return span_tag .. ' ' .. text_link
end

return p