Revision as of 00:22, 28 February 2019 view sourceEvad37 (talk | contribs)Autopatrolled, Administrators44,554 edits extractRegularFiles: remove sizes, which sometimes get mistaken for captions← Previous edit | Revision as of 00:24, 20 March 2019 view source Evad37 (talk | contribs)Autopatrolled, Administrators44,554 edits only show transcluded images if they have captionsNext edit → | ||
Line 57: | Line 57: | ||
end | end | ||
return galleryLinesTable | return galleryLinesTable | ||
end | |||
function hasCaption(line) | |||
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 false | |||
end | end | ||
Line 140: | Line 146: | ||
if galleryFiles then | if galleryFiles then | ||
for _, f in pairs(galleryFiles) do | for _, f in pairs(galleryFiles) do | ||
local filename = string.gsub(f, '{{!}}.*', '') | if hasCaption(f) then | ||
local filename = string.gsub(f, '{{!}}.*', '') | |||
local isOkay = excerptModule.checkimage(filename) | local isOkay = excerptModule.checkimage(filename) | ||
if isOkay then | if isOkay then | ||
table.insert(lines, f) | table.insert(lines, f) | ||
end | |||
end | end | ||
end | end | ||
Line 151: | Line 159: | ||
if otherFiles then | if otherFiles then | ||
for _, f in pairs(extractRegularFiles(otherFiles)) do | for _, f in pairs(extractRegularFiles(otherFiles)) do | ||
if f and f ~= '' and mw.ustring.sub(f, 1, 5) == 'File:' then | if f and f ~= '' and mw.ustring.sub(f, 1, 5) == 'File:' and hasCaption(f) then | ||
table.insert(lines, f) | table.insert(lines, f) | ||
end | end |
Revision as of 00:24, 20 March 2019
Module documentation[view] [edit] [history] [purge]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 template contains coding that is not compatible with mobile versions of Misplaced Pages, causing display and accessibility problems. Read the documentation for an explanation. |
This module depends on the following other modules: |
This module uses TemplateStyles: |
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:
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.
-- 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 = val end else cleanArgs = 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 = { = 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 do table.insert(galleryLinesTable, makeGalleryLine(args, args, args)) i = i + 2 end return galleryLinesTable end function hasCaption(line) 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 false 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*', '|') -- remove sizes, which sometimes get mistaken for captions file = mw.ustring.gsub(file, '|%d*x?%d+px(])', '%1') -- 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 do if namespaceNumber(args) == 6 then -- file namespace -- args is either just the filename, or uses syntax File:Name.jpg##Caption##Credit local parts = mw.text.split(args, '##%s*') local filename = parts local caption = args or parts or false local credit = args or parts or false local line = makeGalleryLine(filename, caption, credit) table.insert(lines, line) else local content, pagename = excerptModule.getContent(args) if not pagename then return error('Cannot read a valid page for "' .. args .. '"', 0) elseif not content then return error('No content found on page "' .. args .. '"', 0) end if args then content = excerptModule.getsection(content, args) or '' end content = excerptModule.cleanupText(content) local galleryFiles = extractGalleryFiles(content) if galleryFiles then for _, f in pairs(galleryFiles) do if hasCaption(f) then local filename = string.gsub(f, '{{!}}.*', '') local isOkay = excerptModule.checkimage(filename) if isOkay then table.insert(lines, f) end 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:' and hasCaption(f) 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 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 pCategory: