Module:IconHandler

From Heterodontosaurus Balls

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

local p = {}

local function cleanString(input)
    -- Remove leading/trailing whitespaces and replace spaces with underscores
    return mw.text.trim(input or ""):gsub("%s+", "_")
end

local function getIconPath(iconName, suffix)
    -- Build the file name with suffix if it exists
    local iconPath = iconName .. "-icon" .. (suffix ~= "" and " " .. suffix or "") .. ".png"
    return iconPath
end

local function processIcon(frame)
    local iconName = frame.args[1] or "404"
    local suffix = cleanString(frame.args[2] or "")

    -- Generate the icon path with optional suffix
    local iconPath = getIconPath(iconName, suffix)

-- Check if the icon file exists, fallback to "404-icon.png" if it doesn't
--if not mw.title.new("File:" .. iconPath).exists then
--    iconPath = "404-icon.png"
--end

    -- Return the formatted file link, including suffix if provided
    return string.format("[[File:%s]]", iconPath)
end

-- Expose the processIcon function for use in the template
p.processIcon = processIcon
return p