Module:Random slideshow/sandbox: Difference between revisions

Content deleted Content added
Temporary somewhat working fix for the advanced-look processing/linter errors by avoiding (some) pipe replacement. This also disables the expansion of templates, as it was causing issues and im not entirely sure why its used right now other than for the old sake of {{#tag:
enable ignoreOnlyincludes
 
(9 intermediate revisions by the same user not shown)
Line 3:
local excerptModule = require('Module:Excerpt/portals/sandbox')
local randomModule = require('Module:Random')
local redirectModule = require('Module:Redirect')
 
function cleanupArgs(argsTable)
Line 40 ⟶ 39:
local sortedLines = nonRandom and galleryLines or randomModule.main('array', randomiseArgs)
for i = 1, #sortedLines do
-- moveinsert thea altswitcher-label parameterspan tojust after the endfirst ofpipe, the filewhich should itideally existbe to fix linterthe errorscaption
-- make sure the parameter isnt perhaps being confused by a wikilink inside it
local altParam = sortedLines[i]:gsub(
"^(.+)(%{%{!}}alt%{%{=}}[^{]*)(.*)$",
"%2")
if not string.find(altParam,"%[%[") then
sortedLines[i] = sortedLines[i]:gsub(
"^(.+)(%{%{!}}alt%{%{=}}[^{]*)(.*)$",
"%1%3%2")
end
-- insert a switcher-label span just after the first pipe (which has already been escaped as {{!}} instead the | character)
sortedLines[i] = sortedLines[i]:gsub(
"|",
'|<span class="switcher-label" style="display:none"><span class="randomSlideshow-sr-only">Image ' .. tostring(i) .. '</span></span>',
1)
mw.log("Workin with",sortedLines[i])
end
local galleryContent = table.concat(sortedLines, '\n')
galleryContent = string.gsub(galleryContent,"%{%{!}}","|")
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>'
Line 68 ⟶ 55:
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 '' )
return title.prefixedText .. '{{!}}|' .. ( caption or '' ) .. creditLine
end
 
Line 85 ⟶ 72:
 
function hasCaption(line)
local caption = mw.ustring.match(line, ".-{{!}}(.*)") or 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
Line 95 ⟶ 82:
return false
end
gallery = mw.ustring.gsub(gallery, '|', '{{!}}')
return mw.text.split(gallery, '%c')
end
Line 114 ⟶ 100:
file = mw.ustring.gsub(file, '|%s*center%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*centre%s*([|%]])', '%1')
gallery file = mw.ustring.gsub(galleryfile, '|%s*none%s*([|%]])', '{{!}}%1')
file = mw.ustring.gsub(file, '|%s*baseline%s*([|%]])', '%1')
file = mw.ustring.gsub(file, '|%s*sub%s*([|%]])', '%1')
Line 126 ⟶ 113:
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)
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, '{%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, '|%s*alt%s*=', '{{!}}alt=')
-- file = mw.ustring.gsub(file, '|', '{{!}}')
-- file = mw.ustring.gsub(file, '=', '{{=}}')
-- remove linebreaks
file = mw.ustring.gsub(file, '\n\n', '<br>')
Line 147 ⟶ 125:
file = mw.ustring.gsub(file, '%]%]$', '')
table.insert(files, file)
mw.log(file)
end
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%3|%2]]")
return file
end
 
Line 178 ⟶ 172:
content = excerptModule.getSection(content, args['section'..i]) or ''
end
content = excerptModule.cleanupText(content, {keepSubsections=true, ignoreOnlyincludes=true}) -- true means keep subsections
local galleryFiles = extractGalleryFiles(content)
Line 184 ⟶ 178:
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, doubleCheck(f.." (from '''[["..pagename.."]]''')"))
end
end
Line 197 ⟶ 191:
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, doubleCheck(f.." (from '''[["..pagename.."]]''')"))
end
end