Module:Excerpt slideshow
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
This module allows article excerpt (from Module:Excerpt) to be put into a slideshow (using Module:Random slideshow). It is intended to be used in portals.
The following templates use this module:
- {{Transclude excerpts as random slideshow}}
- {{Transclude linked excerpts as random slideshow}}
- {{Transclude list item excerpts as random slideshow}}
Tracking categories
The following categories track use where the "Selected general articles" list is built solely from templates:
- Category:Automated portals with article list built solely from one template (population: 0)
- Category:Automated portals with article list built solely from two templates (population: 0)
- Category:Automated portals with article list built solely from three templates (population: 0)
- Usages with more than three templates are not currently tracked
Pages where the "Selected general articles" list is built from a list embeded in the portal:
- Category:Automated portals with embedded list (population: 0)
Note that the Category:Automated portals with embedded list was enabled on 19 April 2019, and it will probably take a few days for all the portal pages to update
Testcases
The following testcase pages are available for testing changes made to this module's sandbox:
- Template:Transclude excerpts as random slideshow/testcases
- Template:Transclude linked excerpts as random slideshow/testcases
- Template:Transclude list item excerpts as random slideshow/testcases
See also
-- Creates a slideshow gallery where the order is randomised. Intended for use on portal pages.
local p = {}
local excerptModule = require('Module:Excerpt')
local randomModule = require('Module:Random')
local redirectModule = require('Module:Redirect')
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 normaliseCssMeasurement(input)
local suffix = string.reverse(string.sub(string.reverse(input), 1, 2))
if ( suffix == 'px' ) or ( suffix == 'em' ) or ( string.sub(suffix, 2, 2) == '%' ) then
return input
else
return input .. 'px'
end
end
function isDeclined(val)
if not val then return false end
local declinedWords = " decline declined exclude excluded false none not no n off omit omitted remove removed "
return string.find(declinedWords , ' '..val..' ', 1, true ) and true or false
end
function makeOutput(galleryLines, maxWidth, containerClassName, nonRandom)
local randomiseArgs = { ['t'] = galleryLines }
local sortedLines = nonRandom and galleryLines or randomModule.main('array', randomiseArgs)
local galleryContent = table.concat(sortedLines, '\n')
local output = '<div class="' .. containerClassName .. '" style="max-width:' .. normaliseCssMeasurement(maxWidth) .. '; margin:-4em auto;">{{#tag:gallery|' .. galleryContent .. '|mode=slideshow}}</div>'
return output
end
function makeGalleryLine(file, caption, credit)
local title = mw.title.new(file, "File" )
local creditLine = ( credit and '<p><span style="font-size:88%">' .. credit .. '</span></p>' or '' )
return title.prefixedText .. '{{!}}' .. ( caption or '' ) .. creditLine
end
function makeGalleryLinesTable(args)
local galleryLinesTable = {}
local i = 1
while args[i] do
table.insert(galleryLinesTable, makeGalleryLine(args[i], args[i+1], args['credit' .. (i+1)/2]))
i = i + 2
end
return galleryLinesTable
end
function extractGalleryFiles(wikitext)
local gallery = mw.ustring.match(wikitext, '<gallery.->%s*(.-)%s*</gallery>')
if not gallery then
return false
end
gallery = mw.ustring.gsub(gallery, '|', '{{!}}')
return mw.text.split(gallery, '%c')
end
function extractRegularFiles(wikitext)
local files = {}
local frame = mw.getCurrentFrame()
local expand = function(template)
return frame:preprocess(template)
end
for file in mw.ustring.gmatch(wikitext, '%b[]' ) do
-- remove keywords that don't work in galleries
file = mw.ustring.gsub(file, '|%s*thumb%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*thumbnail%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*left%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*right%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*center%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*framed?%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*frameless%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*upright%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*upright%s*=.-([|%]])', '%1')
-- remove spaces prior to captions (which cause pre-formatted text)
file = mw.ustring.gsub(file, '|%s*', '|')
-- expand templates
file = mw.ustring.gsub(file, '{%b{}}', expand)
-- remove loose closing braces which don't have matching opening braces
file = mw.ustring.gsub(file, '}}', '')
-- remove loose opening braces which don't have matching closing braces (and the subsequent content, which is probably just a template name)
file = mw.ustring.gsub(file, '{{.-([|%]])', '$1')
-- replace pipes and equals (which would otherwise break the {{#tag:}} syntax)
file = mw.ustring.gsub(file, '|', '{{!}}')
file = mw.ustring.gsub(file, '=', '{{=}}')
-- remove linebreaks
file = mw.ustring.gsub(file, '\n\n', '<br>')
file = mw.ustring.gsub(file, '\n', '')
-- remove surrounding square brackets
file = mw.ustring.gsub(file, '^%[%[', '')
file = mw.ustring.gsub(file, '%]%]$', '')
table.insert(files, file)
end
return files
end
function makeTranscludedGalleryLinesTables(args)
local namespaceNumber = function(pagetitle)
local titleObject = mw.title.new(pagetitle)
return titleObject and titleObject.namespace
end
local lines = {}
local i = 1
while args[i] do
if namespaceNumber(args[i]) == 6 then -- file namespace
-- args[i] is either just the filename, or uses syntax File:Name.jpg##Caption##Credit
local parts = mw.text.split(args[i], '##%s*')
local filename = parts[1]
local caption = args['caption'..i] or parts[2] or false
local credit = args['credit'..i] or parts[3] or false
local line = makeGalleryLine(filename, caption, credit)
table.insert(lines, line)
else
local content, pagename = excerptModule.getContent(args[i])
if not pagename then
return error('Cannot read a valid page for "' .. args[i] .. '"', 0)
elseif not content then
return error('No content found on page "' .. args[i] .. '"', 0)
end
if args['section'..i] then
content = excerptModule.getsection(content, args['section'..i]) or ''
end
content = excerptModule.cleanupText(content)
local galleryFiles = extractGalleryFiles(content)
if galleryFiles then
for _, f in pairs(galleryFiles) do
local filename = string.gsub(f, '{{!}}.*', '')
local isOkay = excerptModule.checkimage(filename)
if isOkay then
table.insert(lines, f)
end
end
end
local otherFiles = excerptModule.parse(content, {fileflags="1-100"}, true)
if otherFiles then
for _, f in pairs(extractRegularFiles(otherFiles)) do
if f and f ~= '' and mw.ustring.sub(f, 1, 5) == 'File:' then
table.insert(lines, f)
end
end
end
end
i = i + 1
end
return ( #lines > 0 ) and lines or error('No images found')
end
p._main = function(args, transclude, containerClassName)
if not args[1] then
return error(linked and 'No page specified' or 'No page specified', 0)
end
local lines = transclude and makeTranscludedGalleryLinesTables(args) or makeGalleryLinesTable(args)
return makeOutput(lines, args.width or '100%', containerClassName or 'randomSlideshow-container', isDeclined(args.random))
end
p.main = function(frame)
local parent = frame.getParent(frame)
local parentArgs = parent.args
local args = cleanupArgs(parentArgs)
local output = p._main(args, false)
return frame:extensionTag{ name='templatestyles', args = { src='Random slideshow/styles.css'} }
.. frame:preprocess(output)
end
p.transclude = function(frame)
local parent = frame.getParent(frame)
local parentArgs = parent.args
local args = cleanupArgs(parentArgs)
local output = p._main(args, true)
return frame:extensionTag{ name='templatestyles', args = { src='Random slideshow/styles.css'} }
.. frame:preprocess(output)
end
return p