Module:Gallery
Appearance
Documentation for this module may be created at Module:Gallery/doc
local p = {}
-- Fisher–Yates shuffle
-- https://stackoverflow.com/a/68486276
local function shuffle(t)
for i = #t, 2, -1 do
local j = math.random(i)
t[i], t[j] = t[j], t[i]
end
end
-- Returns a gallery with the items in random order.
function p.random(frame)
local files = {}
for i, arg in ipairs(frame.args) do
table.insert(files, arg)
end
local links = {}
for key, value in pairs(frame.args) do
local i = not tonumber(key) and key:match("l(%d+)")
if i and tonumber(i) then
links[tonumber(i)] = value
end
end
local alts = {}
for key, value in pairs(frame.args) do
local i = not tonumber(key) and key:match("a(%d+)")
if i and tonumber(i) then
alts[tonumber(i)] = value
end
end
local captions = {}
for key, value in pairs(frame.args) do
local i = not tonumber(key) and key:match("c(%d+)")
if i and tonumber(i) then
captions[tonumber(i)] = value
end
end
local items = {}
for i, file in ipairs(files) do
local components = { file }
if links[i] then
-- Append a space to prevent Gallery extension from interpreting the
-- rest of the word as part of the URL.
table.insert(components, mw.ustring.format("link=%s ", links[i]))
end
if alts[i] then
table.insert(components, mw.ustring.format("alt=%s", alts[i]))
end
if captions[i] then
table.insert(components, captions[i])
end
table.insert(items, table.concat(components, "|"))
end
local seed = tonumber(mw.getLanguage( "en" ):formatDate( "U" )) * 10000 + os.clock() * 10000
math.randomseed(seed)
shuffle(items)
local content = table.concat(items, "\n")
local args = {
caption = frame.args.caption,
widths = frame.args.widths,
heights = frame.args.heights,
perrow = frame.args.perrow,
showfilename = frame.args.showfilename,
mode = frame.args.mode,
showthumbnails = frame.args.showthumbnails,
id = frame.args.id,
title = frame.args.title,
class = frame.args.class,
style = frame.args.style,
}
local gallery = frame:extensionTag {
name = "gallery",
content = content,
args = args,
}
return mw.html.create("div")
:addClass("osm-gallery-random")
:wikitext(gallery)
end
return p