Module:Random slideshow
Appearance
From Wikipedia, the free encyclopedia
This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing.
Page semi-protected Editing of this module by new or unregistered users is currently disabled.
See the protection policy and protection log for more details. If you cannot edit this module and you wish to make a change, you can submit an edit request , discuss changes on the talk page, request unprotection, log in, or create an account.
See the protection policy and protection log for more details. If you cannot edit this module and you wish to make a change, you can submit an edit request , discuss changes on the talk page, request unprotection, log in, or create an account.
This template contains coding that is not compatible with mobile versions of Wikipedia, causing display and accessibility problems. Read the documentation for an explanation.
This module depends on the following other modules:
Usage
{{#invoke:Random slideshow|main}}
- See {{Random slideshow }} for details.
{{#invoke:Random slideshow|transclude}}
- See {{Transclude files as random slideshow }} for details.
This module is also used by Module:Excerpt slideshow.
Display in the mobile view
On mobile devices, a standard gallery is displayed instead of the slideshow. If the screen width is less than 720px, only the first four images after randomisation are visible (example screenshot). For mobile devices with larger resolutions, all images are displayed (example screenshot). For some tables, it displays all images stacked (example screenshot).
Testcases
The following testcase pages are available for testing changes made to this module's sandbox:
The above documentation is transcluded from Module:Random slideshow/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
-- Creates a slideshow gallery where the order is randomised. Intended for use on portal pages. localp={} localexcerptModule=require('Module:Excerpt/portals') localrandomModule=require('Module:Random') functioncleanupArgs(argsTable) localcleanArgs={} forkey,valinpairs(argsTable)do iftype(val)=='string'then val=val:match('^%s*(.-)%s*$') ifval~=''then cleanArgs[key]=val end else cleanArgs[key]=val end end returncleanArgs end functionnormaliseCssMeasurement(input) localsuffix=string.reverse(string.sub(string.reverse(input),1,2)) if(suffix=='px')or(suffix=='em')or(string.sub(suffix,2,2)=='%')then returninput else returninput..'px' end end functionisDeclined(val) ifnotvalthenreturnfalseend localdeclinedWords=" decline declined exclude excluded false none not no n off omit omitted remove removed " returnstring.find(declinedWords,' '..val..' ',1,true)andtrueorfalse end functionmakeOutput(galleryLines,maxWidth,containerClassName,nonRandom) localrandomiseArgs={['t']=galleryLines} localsortedLines=nonRandomandgalleryLinesorrandomModule.main('array',randomiseArgs) fori=1,#sortedLinesdo -- insert a switcher-label span just after the first pipe, which should ideally be the caption sortedLines[i]=sortedLines[i]:gsub( "|", '|<span class="switcher-label" style="display:none"><span class="randomSlideshow-sr-only">Image '..tostring(i)..'</span></span>', 1) end localgalleryContent=table.concat(sortedLines,'\n') localoutput='<div class="'..containerClassName..'" style="max-width:'..normaliseCssMeasurement(maxWidth)..'; margin:-4em auto;"><div class="nomobile"><!--intentionally empty on desktop, and is not present on mobile website (outside template namesapce)--></div>' ..mw.getCurrentFrame():extensionTag({name="gallery",content=galleryContent,args={mode="slideshow",class="switcher-container"}})..'</div>' returnoutput end functionmakeGalleryLine(file,caption,credit) localtitle=mw.title.new(file,"File") ifnottitle then return"File:Blank.png|{{Error|File [[:File:"..file.."]] does not exist.}}" end localcreditLine=(creditand'<p><span style="font-size:88%">'..credit..'</span></p>'or'') returntitle.prefixedText..'|'..(captionor'')..creditLine end functionmakeGalleryLinesTable(args) localgalleryLinesTable={} locali=1 whileargs[i]do table.insert(galleryLinesTable,makeGalleryLine(args[i],args[i+1],args['credit'..(i+1)/2])) i=i+2 end returngalleryLinesTable end functionhasCaption(line) localcaption=mw.ustring.match(line,".-|(.*)") -- require caption to exist with more than 5 characters (avoids sizes etc being mistaken for captions) returncaptionand#caption>5andtrueorfalse end functionextractGalleryFiles(wikitext) localgallery=mw.ustring.match(wikitext,'<gallery.->%s*(.-)%s*</gallery>') ifnotgallerythen returnfalse end returnmw.text.split(gallery,'%c') end functionextractRegularFiles(wikitext) localfiles={} localframe=mw.getCurrentFrame() localexpand=function(template) returnframe:preprocess(template) end forfileinmw.ustring.gmatch(wikitext,'%b[]')do -- remove keywords that don't work in galleries file=mw.ustring.gsub(file,'|%s*baseline%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*border%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*bottom%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*center%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*centre%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*class%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*lang%s*=.-([|%]])','%1') file=mw.ustring.gsub(file,'|%s*left%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*link%s*=.-([|%]])','%1') file=mw.ustring.gsub(file,'|%s*none%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*right%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*sub%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*super%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*text%-bottom%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*text%-top%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*thumb%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*thumbnail%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*top%s*([|%]])','%1') file=mw.ustring.gsub(file,'|%s*upright%s*[0-9%.]*%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*','|') -- remove sizes, which sometimes get mistaken for captions file=mw.ustring.gsub(file,'|%s*%d*x?%d+%s*px%s*([|%]])','%1') -- 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 returnfiles end --Central function for fixing issues that could occur in both gallery-fetched and file-fetched files localfunctiondoubleCheck(file) -- disable pipes in wikilinks file=file:gsub( "%[%[([^%]]-)|(.-)]]", "[[%1__PIPE__%2]]") -- move any alt parameter to the end to avoid putting the switcher in too early and causing a linter error file=file:gsub( "^(.+)(|alt=[^|]*)(.*)$", "%1%3%2") -- bring back pipes in wikilinks file=file:gsub( "%[%[(.-)__PIPE__(.-)]]", "[[%1|%2]]") returnfile end functionmakeTranscludedGalleryLinesTables(args) localnamespaceNumber=function(pagetitle) localtitleObject=mw.title.new(pagetitle) returntitleObjectandtitleObject.namespace end locallines={} locali=1 whileargs[i]do ifnamespaceNumber(args[i])==6then-- file namespace -- args[i] is either just the filename, or uses syntax File:Name.jpg##Caption##Credit localparts=mw.text.split(args[i],'##%s*') localfilename=parts[1] localcaption=args['caption'..i]orparts[2]orfalse localcredit=args['credit'..i]orparts[3]orfalse localline=makeGalleryLine(filename,caption,credit) table.insert(lines,line) else localcontent,pagename=excerptModule.getContent(args[i]) ifnotpagenamethen returnerror('Cannot read a valid page for "'..args[i]..'"',0) elseifnotcontentthen returnerror('No content found on page "'..args[i]..'"',0) end ifargs['section'..i]then content=excerptModule.getSection(content,args['section'..i])or'' end content=excerptModule.cleanupText(content,{keepSubsections=true,ignoreOnlyincludes=true})-- true means keep subsections localgalleryFiles=extractGalleryFiles(content) ifgalleryFilesthen for_,finpairs(galleryFiles)do ifhasCaption(f)then localfilename=string.gsub(f,'|.*','') localisOkay=excerptModule.checkImage(filename) ifisOkaythen table.insert(lines,doubleCheck(f.." (from '''[["..pagename.."]]''')")) end end end end localotherFiles=excerptModule.parse(content,{fileflags="1-100",filesOnly=true}) ifotherFilesthen for_,finpairs(extractRegularFiles(otherFiles))do iffandf~=''andmw.ustring.sub(f,1,5)=='File:'andhasCaption(f)then table.insert(lines,doubleCheck(f.." (from '''[["..pagename.."]]''')")) end end end end i=i+1 end return(#lines>0)andlinesorerror('No images found') end p._main=function(args,transclude,extraClassName) ifnotargs[1]then returnerror(linkedand'No page specified'or'No page specified',0) end locallines=transcludeandmakeTranscludedGalleryLinesTables(args)ormakeGalleryLinesTable(args) localclassNames='randomSlideshow-container' ifextraClassNamethenclassNames=classNames..' '..extraClassNameend returnmakeOutput(lines,args.widthor'100%',classNames,isDeclined(args.random)) end p.main=function(frame) localparent=frame.getParent(frame) localparentArgs=parent.args localargs=cleanupArgs(parentArgs) localoutput=p._main(args,false) returnframe:extensionTag{name='templatestyles',args={src='Module:Random slideshow/styles.css'}} ..frame:preprocess(output) end p.transclude=function(frame) localparent=frame.getParent(frame) localparentArgs=parent.args localargs=cleanupArgs(parentArgs) localoutput=p._main(args,true) returnframe:extensionTag{name='templatestyles',args={src='Module:Random slideshow/styles.css'}} ..frame:preprocess(output) end returnp