Module:Sandbox/Hike395/RS explained

-- Creates a slideshow gallery where the order is randomised. Intended for use on portal pages.local getArgs = require('Module:Arguments').getArgs

local p = local excerptModule = require('Module:Excerpt/portals')local randomModule = require('Module:Random')local redirectModule = require('Module:Redirect')

local counter =

local function initCounter counter = end

local function incCounter(s, inc) local val = counter[s] inc = inc or 1 if val then counter[s] = val+inc else counter[s] = inc endend

local function startTimer(s) incCounter(s,-os.clock)end

local function stopTimer(s) incCounter(s,os.clock)end

local function dumpCounter local result = nil for k, v in pairs(counter) do if result then result = result..', ' end result = (result or )..k..': '..tostring(v) end return resultend

function cleanupArgs(argsTable) local cleanArgs = for key, val in pairs(argsTable) do if type(val)

'string' then val = val:match('^%s*(.-)%s*$') incCounter('cleanupArgsMatch') if val ~= then cleanArgs[key] = val end else cleanArgs[key] = val end end return cleanArgsend

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' endend

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 " incCounter('declinedFind') return string.find(declinedWords, ' '..val..' ', 1, true) and true or falseend

function makeOutput(galleryLines, maxWidth, containerClassName, nonRandom) local randomiseArgs = local sortedLines = nonRandom and galleryLines or randomModule.main('array', randomiseArgs) for i = 1, #sortedLines do -- insert a switcher-label span just after the first pipe (which has already been escaped as instead the | character) incCounter('makeOutputGsub') sortedLines[i] = sortedLines[i]:gsub("%", '

Image ' .. tostring(i) .. '', 1) end local galleryContent = table.concat(sortedLines, '\n') local output = '

' stopTimer("overall") return dumpCounterend

function makeGalleryLine(file, caption, credit) local title = mw.title.new(file, "File") if not title then return "File:Blank.png" end local creditLine = (credit and ' ' .. credit .. '

' or ) return title.prefixedText .. '' .. (caption or ) .. creditLineend

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 hasCaption(line) incCounter('hasCaptionMatch') local caption = mw.ustring.match(line, ".-(.*)") -- require caption to exist with more than 5 characters (avoids sizes etc being mistaken for captions) return caption and #caption>5 and true or falseend

function extractGalleryFiles(wikitext) incCounter('extractGalleryMatch') local gallery = mw.ustring.match(wikitext, '') if not gallery then return false end incCounter('extractGalleryGsub') gallery = mw.ustring.gsub(gallery, '|', '') return mw.text.split(gallery, '%c')end

function extractRegularFiles(wikitext) startTimer('regularFileTime') local files = local frame = mw.getCurrentFrame local expand = function(template) return frame:preprocess(template) end for file in mw.ustring.gmatch(wikitext, '%b[]') do incCounter('extractRegularFiles') -- 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*border%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*centre%s*([|%]])', '%1') file = mw.ustring.gsub(file, '|%s*baseline%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*top%s*([|%]])', '%1') file = mw.ustring.gsub(file, '|%s*text%-top%s*([|%]])', '%1') file = mw.ustring.gsub(file, '|%s*bottom%s*([|%]])', '%1') file = mw.ustring.gsub(file, '|%s*text%-bottom%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*[0-9%.]*%s*([|%]])', '%1') file = mw.ustring.gsub(file, '|%s*upright%s*=.-([|%]])', '%1') file = mw.ustring.gsub(file, '|%s*link%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') -- expand templates file = mw.ustring.gsub(file, '', 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, '