Module:GalleryPreview: Difference between revisions
Jump to navigation
Jump to search
Mediawiki>The JoTS Option to include image captions from source gallery. |
m 1 revision imported |
(No difference)
|
Latest revision as of 02:23, 30 April 2021
Documentation for this module may be created at Module:GalleryPreview/doc
--| Module:GalleryPreview (v1.2.0)
--- Creates a preview of images from a gallery page.
--by author: "The JoTS"
-- update 10/28/2017 - Option to include image captions from source gallery.
-- <nowiki>
--% Creates a metatable wrapper for checking arguments.
--@ frArgs (table) The arguments table from a frame object. (i.e. frame.args)
--: (table) "Wrapped" table. Arguments my be indexed with membership operator.
local function argChecker(frArgs)
return setmetatable({}, {
__index = function(t, arg)
if arg:match('^_') then
return frArgs[arg:gsub('^_','')]
else
return assert(frArgs[arg],
'Argument "' .. arg .. '" was not provided')
end
end
})
end
-- Gallery object template
local gallery_src = mw.html.create("gallery")
:attr({navigation = 'true', hideaddbutton = 'true', position = 'center'})
-- Module source
return {
main = function(frame)
local args = argChecker(frame.args)
local gallerypg = args.gallery_page -- Link to gallery
local settings = args._settings -- Settings to apply to <gallery>
local defaultTxt = args._default -- Text if link is a redlink
local portalImg = args._portal_image -- Portal image that links to page
local inclCaption = args._include_caption -- Include image caption
local numOfImgs = assert(tonumber(args.number_of_images),
'Argument "number_of_images" is not an integer')
- (portalImg and 1 or 0)
gallerypg = mw.title.new(gallerypg)
if gallerypg.exists then
local contents = gallerypg:getContent()
local galleries = contents:gmatch(
"<%s*[Gg]allery.->(.-)</%s*[Gg]allery%s*>")
local images = {} -- will contain all images from all galleries
local selected = mw.clone(gallery_src)
-- Import all images
for gallery in galleries do
for img in mw.text.gsplit(gallery, '\n') do
if img:gsub('%s','') ~= '' then
table.insert(images, img)
end
end
end
-- Set randomseed
math.randomseed(os.time())
-- Select images randomly
for n = 1, math.min(#images, numOfImgs) do
local i = math.random(#images)
local img = inclCaption
and images[i]
or mw.text.split(images[i], '|')[1] -- omit caption
selected:wikitext(img .. '\n')
table.remove(images, i)
end
-- Include portal image
if portalImg then
selected:wikitext(portalImg .. "|link=" .. gallerypg.text ..'\n')
end
-- Apply custom settings to gallery
if settings then
local settPair =
settings:gmatch([=[(%w+):%s*["'](.-)["'][,;]?]=])
for opt,val in settPair do
selected:attr(opt, val)
end
end
return frame:preprocess(tostring(selected))
else
return frame:preprocess(defaultTxt or
("No gallery for {{PAGENAME}} exists yet! You can create it ["
.. gallerypg:fullUrl("action=edit") .. " here]."))
end
end
}