Module:Sandbox/LVL: Difference between revisionsGive feedback
mNo edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
-- This module powers the Plink template, automatically finding the correct file extension for an icon. | |||
local p = {} | local p = {} | ||
-- | function p.main(frame) | ||
local | -- 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 | 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 | for _, ext in ipairs(extensions) do | ||
local | local title = mw.title.new('File:' .. base_filename .. ext) | ||
if | if title and title.exists then | ||
final_filename = base_filename .. ext | |||
break -- Stop searching once we find a match. | |||
end | end | ||
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 | |||
local | |||
return span_tag .. ' ' .. text_link | |||
local | |||
local | |||
return | |||
end | end | ||
return p | return p | ||