Khác biệt giữa bản sửa đổi của “Mô đun:Random slideshow”
Nội dung được xóa Nội dung được thêm vào
Không có tóm lược sửa đổi Thẻ: Đã bị lùi lại |
Đã lùi lại sửa đổi 69357725 của Irha1412 (thảo luận) Cập nhật này gây nên Lỗi Lua trong Mô_đun:Random_slideshow tại dòng 199: Không tìm thấy hình ảnh. Thẻ: Lùi sửa Đã bị lùi lại |
||
Dòng 1: | Dòng 1: | ||
-- Creates a slideshow gallery where the order is randomised. Intended for use on portal pages. |
-- Creates a slideshow gallery where the order is randomised. Intended for use on portal pages. |
||
local p = {} |
local p = {} |
||
local excerptModule = require('Mô đun:Excerpt |
local excerptModule = require('Mô đun:Excerpt') |
||
local randomModule = require('Mô đun:Random') |
local randomModule = require('Mô đun:Random') |
||
local redirectModule = require('Mô đun:Redirect') |
local redirectModule = require('Mô đun:Redirect/sandbox') |
||
function cleanupArgs(argsTable) |
function cleanupArgs(argsTable) |
||
Dòng 38: | Dòng 38: | ||
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;"> |
local output = '<div class="' .. containerClassName .. '" style="max-width:' .. normaliseCssMeasurement(maxWidth) .. '; margin:-4em auto;">{{#tag:gallery|' .. galleryContent .. '|mode=slideshow}}</div>' |
||
.. mw.getCurrentFrame():extensionTag({name="gallery",content=galleryContent,args={mode="slideshow",class="switcher-container"}}) .. '</div>' |
|||
return output |
return output |
||
end |
end |
||
Dòng 53: | Dòng 45: | ||
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 .. ' |
return title.prefixedText .. '{{!}}' .. ( caption or '' ) .. creditLine |
||
end |
end |
||
Dòng 72: | Dòng 60: | ||
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 |
||
Dòng 82: | Dòng 70: | ||
return false |
return false |
||
end |
end |
||
⚫ | |||
return mw.text.split(gallery, '%c') |
return mw.text.split(gallery, '%c') |
||
end |
end |
||
Dòng 95: | Dòng 84: | ||
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*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*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 |
file = mw.ustring.gsub(file, '|%s*upright%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') |
|||
-- 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, '| |
file = mw.ustring.gsub(file, '|%d*x?%d+px([|%]])', '%1') |
||
-- expand templates |
|||
⚫ | |||
-- remove loose closing braces which don't have matching opening braces |
|||
⚫ | |||
-- remove loose opening braces which don't have matching closing braces (and the subsequent content, which is probably just a template name) |
|||
⚫ | |||
-- replace pipes and equals (which would otherwise break the {{#tag:}} syntax) |
|||
⚫ | |||
⚫ | |||
-- remove linebreaks |
-- remove linebreaks |
||
file = mw.ustring.gsub(file, '\n\n', '<br>') |
file = mw.ustring.gsub(file, '\n\n', '<br>') |
||
Dòng 125: | Dòng 113: | ||
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 |
||
Dòng 153: | Dòng 124: | ||
while args[i] do |
while args[i] do |
||
if namespaceNumber(args[i]) == 6 then -- file namespace |
if namespaceNumber(args[i]) == 6 then -- file namespace |
||
-- args[i] is either just the filename, or uses syntax |
-- args[i] is either just the filename, or uses syntax Tập tin:Name.jpg##Caption##Credit |
||
local parts = mw.text.split(args[i], '##%s*') |
local parts = mw.text.split(args[i], '##%s*') |
||
local filename = parts[1] |
local filename = parts[1] |
||
Dòng 168: | Dòng 139: | ||
end |
end |
||
if args['section'..i] then |
if args['section'..i] then |
||
content = excerptModule. |
content = excerptModule.getsection(content, args['section'..i]) or '' |
||
end |
end |
||
content = excerptModule.cleanupText(content, |
content = excerptModule.cleanupText(content, true) -- true means keep subsections |
||
local galleryFiles = extractGalleryFiles(content) |
local galleryFiles = extractGalleryFiles(content) |
||
Dòng 176: | Dòng 147: | ||
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. |
local isOkay = excerptModule.checkimage(filename) |
||
if isOkay then |
if isOkay then |
||
table.insert(lines, |
table.insert(lines, f) |
||
end |
end |
||
end |
end |
||
Dòng 185: | Dòng 156: | ||
end |
end |
||
local otherFiles = excerptModule.parse(content, {fileflags="1-100", |
local otherFiles = excerptModule.parse(content, {fileflags="1-100"}, true) |
||
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:' and hasCaption(f) then |
if f and f ~= '' and mw.ustring.sub(f, 1, 5) == 'Tập tin:' or 'File:' and hasCaption(f) then |
||
table.insert(lines, |
table.insert(lines, f) |
||
end |
end |
||
end |
end |
||
Dòng 200: | Dòng 171: | ||
end |
end |
||
p._main = function(args, transclude, |
p._main = function(args, transclude, containerClassName) |
||
if not args[1] then |
if not args[1] then |
||
return error(linked and 'Không có trang nào được chỉ định' or 'Không có trang nào được chỉ định', 0) |
return error(linked and 'Không có trang nào được chỉ định' or 'Không có trang nào được chỉ định', 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)) |
|||
if extraClassName then classNames = classNames .. ' ' .. extraClassName end |
|||
return makeOutput(lines, args.width or '100%', classNames, isDeclined(args.random)) |
|||
end |
end |
||
Dòng 215: | Dòng 184: | ||
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=' |
return frame:extensionTag{ name='templatestyles', args = { src='Trình chiếu ngẫu nhiên/styles.css'} } |
||
.. frame:preprocess(output) |
.. frame:preprocess(output) |
||
end |
end |
||
Dòng 224: | Dòng 193: | ||
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=' |
return frame:extensionTag{ name='templatestyles', args = { src='Trình chiếu ngẫu nhiên/styles.css'} } |
||
.. frame:preprocess(output) |
.. frame:preprocess(output) |
||
end |
end |
Phiên bản lúc 04:07, ngày 27 tháng 3 năm 2024
Tài liệu mô đun[tạo]
-- Creates a slideshow gallery where the order is randomised. Intended for use on portal pages.
local p = {}
local excerptModule = require('Mô đun:Excerpt')
local randomModule = require('Mô đun:Random')
local redirectModule = require('Mô đun:Redirect/sandbox')
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] = val
end
else
cleanArgs[key] = 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 = { ['t'] = 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[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)
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[i] do
if namespaceNumber(args[i]) == 6 then -- file namespace
-- args[i] is either just the filename, or uses syntax Tập tin:Name.jpg##Caption##Credit
local parts = mw.text.split(args[i], '##%s*')
local filename = parts[1]
local caption = args['caption'..i] or parts[2] or false
local credit = args['credit'..i] or parts[3] or false
local line = makeGalleryLine(filename, caption, credit)
table.insert(lines, line)
else
local content, pagename = excerptModule.getContent(args[i])
if not pagename then
return error('Không thể đọc trang hợp lệ cho "' .. args[i] .. '"', 0)
elseif not content then
return error('Không tìm thấy nội dung trên trang "' .. args[i] .. '"', 0)
end
if args['section'..i] then
content = excerptModule.getsection(content, args['section'..i]) or ''
end
content = excerptModule.cleanupText(content, true) -- true means keep subsections
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) == 'Tập tin:' or 'File:' and hasCaption(f) then
table.insert(lines, f)
end
end
end
end
i = i + 1
end
return ( #lines > 0 ) and lines or error('Không tìm thấy hình ảnh')
end
p._main = function(args, transclude, containerClassName)
if not args[1] then
return error(linked and 'Không có trang nào được chỉ định' or 'Không có trang nào được chỉ định', 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='Trình chiếu ngẫu nhiên/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='Trình chiếu ngẫu nhiên/styles.css'} }
.. frame:preprocess(output)
end
return p