| Modul ini dinilai sebagai modul alfa. Modul ini sudah siap untuk input dari pihak ketiga, dan bisa digunakan dalam beberapa halaman untuk melihat apakah ada masalah yang timbul, namun harus terus diawasi. Saran untuk fitur baru atau perubahan dalam mekanisme input dan output dipersilahkan.Random slideshow |
| Modul ini menggunakan Lua: |
| Modul ini menggunakan TemplateStyles: |
PenggunaanPenggunaan
{{#invoke:Random slideshow|main}}- Lihat {{Random slideshow}}.
{{#invoke:Random slideshow|transclude}}- Lihat {{Transclude files as random slideshow}}.
Dokumentasi di atas ditransklusikan dari Modul:Random slideshow/doc. (sunting | riwayat)
Penyunting dapat melakukan uji coba pada halaman bak pasir (buat | cermin) dan kasus uji (buat) modul ini.
Subhalaman modul ini.
Penyunting dapat melakukan uji coba pada halaman bak pasir (buat | cermin) dan kasus uji (buat) modul ini.
Subhalaman modul ini.
-- 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