模块:Random slideshow:修订间差异

创建页面,内容为“-- Creates a slideshow gallery where the order is randomised. Intended for use on portal pages. local p = {} local excerptModule = require('Module:Excerpt/portals') 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] = va…”
 
无编辑摘要
 
第3行: 第3行:
local excerptModule =  require('Module:Excerpt/portals')
local excerptModule =  require('Module:Excerpt/portals')
local randomModule = require('Module:Random')
local randomModule = require('Module:Random')
local redirectModule = require('Module:Redirect')


function cleanupArgs(argsTable)
function cleanupArgs(argsTable)
第38行: 第37行:
local randomiseArgs = { ['t'] = galleryLines }
local randomiseArgs = { ['t'] = galleryLines }
local sortedLines = nonRandom and galleryLines or randomModule.main('array', 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 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
local galleryContent = table.concat(sortedLines, '\n')
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>'
local output = '<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>'
return output
return output
end
end
第45行: 第52行:
function makeGalleryLine(file, caption, credit)
function makeGalleryLine(file, caption, credit)
local title = mw.title.new(file, "File" )
local title = mw.title.new(file, "File" )
if not title
then
return "File:Blank.png|{{Error|File [[:File:" .. file .. "]] does not exist.}}"
end
local creditLine = ( credit and '<p><span style="font-size:88%">' .. credit .. '</span></p>' or '' )
local creditLine = ( credit and '<p><span style="font-size:88%">' .. credit .. '</span></p>' or '' )
return title.prefixedText .. '{{!}}' .. ( caption or '' ) .. creditLine
return title.prefixedText .. '|' .. ( caption or '' ) .. creditLine
end
end


第60行: 第71行:


function hasCaption(line)
function hasCaption(line)
local caption = mw.ustring.match(line, ".-{{!}}(.*)")
local caption = mw.ustring.match(line, ".-|(.*)")
-- require caption to exist with more than 5 characters (avoids sizes etc being mistaken for captions)
-- 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
return caption and #caption>5 and true or false
第70行: 第81行:
return false
return false
end
end
gallery = mw.ustring.gsub(gallery, '|', '{{!}}')
return mw.text.split(gallery, '%c')
return mw.text.split(gallery, '%c')
end
end
第84行: 第94行:
file = mw.ustring.gsub(file, '|%s*thumb%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*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*left%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*right%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*center%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*centre%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*none%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*framed?%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*frameless%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*[0-9%.]*%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*upright%s*=.-([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*upright%s*=.-([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*link%s*=.-([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*lang%s*=.-([|%]])', '%1')
-- remove spaces prior to captions (which cause pre-formatted text)
-- remove spaces prior to captions (which cause pre-formatted text)
file = mw.ustring.gsub(file, '|%s*', '|')
file = mw.ustring.gsub(file, '|%s*', '|')
-- remove sizes, which sometimes get mistaken for captions
-- remove sizes, which sometimes get mistaken for captions
file = mw.ustring.gsub(file, '|%d*x?%d+px([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*%d*x?%d+%s*px%s*([|%]])', '%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
-- remove linebreaks
file = mw.ustring.gsub(file, '\n\n', '<br>')
file = mw.ustring.gsub(file, '\n\n', '<br>')
第113行: 第126行:
end
end
return files
return files
end
--Central function for fixing issues that could occur in both gallery-fetched and file-fetched files
local function doubleCheck(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]]")
return file
end
end


第147行: 第177行:
for _, f in pairs(galleryFiles) do
for _, f in pairs(galleryFiles) do
if hasCaption(f) then
if hasCaption(f) then
local filename = string.gsub(f, '{{!}}.*', '')
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, doubleCheck(f.." (from '''[["..pagename.."]]''')"))
end
end
end
end
第160行: 第190行:
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:' and hasCaption(f) then
if f and f ~= '' and mw.ustring.sub(f, 1, 5) == 'File:' and hasCaption(f) then
table.insert(lines, f)
table.insert(lines, doubleCheck(f.." (from '''[["..pagename.."]]''')"))
end
end
end
end
第171行: 第201行:
end
end


p._main = function(args, transclude, containerClassName)
p._main = function(args, transclude, extraClassName)
if not args[1] then
if not args[1] then
return error(linked and 'No page specified' or 'No page specified', 0)
return error(linked and 'No page specified' or 'No page specified', 0)
end
end
local lines = transclude and makeTranscludedGalleryLinesTables(args) or makeGalleryLinesTable(args)
local lines = transclude and makeTranscludedGalleryLinesTables(args) or makeGalleryLinesTable(args)
return makeOutput(lines, args.width or '100%', containerClassName or 'randomSlideshow-container', isDeclined(args.random))
local classNames = 'randomSlideshow-container'
if extraClassName then classNames = classNames .. ' ' .. extraClassName end
return makeOutput(lines, args.width or '100%', classNames, isDeclined(args.random))
end
end


第184行: 第216行:
local args = cleanupArgs(parentArgs)
local args = cleanupArgs(parentArgs)
local output = p._main(args, false)
local output = p._main(args, false)
return frame:extensionTag{ name='templatestyles', args = { src='Random slideshow/styles.css'} }  
return frame:extensionTag{ name='templatestyles', args = { src='Module:Random slideshow/styles.css'} }  
.. frame:preprocess(output)
.. frame:preprocess(output)
end
end
第193行: 第225行:
local args = cleanupArgs(parentArgs)
local args = cleanupArgs(parentArgs)
local output = p._main(args, true)
local output = p._main(args, true)
return frame:extensionTag{ name='templatestyles', args = { src='Random slideshow/styles.css'} }  
return frame:extensionTag{ name='templatestyles', args = { src='Module:Random slideshow/styles.css'} }  
.. frame:preprocess(output)
.. frame:preprocess(output)
end
end


return p
return p