模組:沙盒/Kcx36/sandbox2
外观
local p = {}
-- Function to clean up arguments (remove leading/trailing spaces)
function cleanupArgs(argsTable)
local cleanArgs = {}
for key, val in pairs(argsTable) do
if type(val) == 'string' then
val = val:match('^%s*(.-)%s*$')
if val ~= '' then
cleanArgs[key] = val
end
else
cleanArgs[key] = val
end
end
return cleanArgs
end
-- Function to randomize the order of gallery items
function randomize(items)
math.randomseed(os.time())
local index = math.random(1, #items)
return items[index]
end
-- Function to generate the output for a random image and caption
function makeRandomImageOutput(args)
-- Create a table of image and caption pairs
local imageTable = {}
local i = 1
while args[i] do
table.insert(imageTable, {image = args[i], caption = args[i + 1]})
i = i + 2
end
-- Randomly select one image
local selectedImage = randomize(imageTable)
-- Return the formatted image with caption
return string.format('[[File:%s|thumb|%s]]', selectedImage.image, selectedImage.caption)
end
-- Main function to process the input and return the random image output
function p.main(frame)
local parent = frame:getParent()
local args = cleanupArgs(parent.args)
-- Generate the random image output
return makeRandomImageOutput(args)
end
return p