Module:Portal image banner: Difference between revisions
Appearance
Content deleted Content added
Add max-width to the image that is being requested to prevent huge images from being downloaded each time. |
m Simplified code by clearing out redundancy, using "elseif" instead of "else if", and improving formatting |
||
Line 27: | Line 27: | ||
p._main = function(args) |
p._main = function(args) |
||
if not args[1] then |
if not args[1] then |
||
return error( |
return error('No page specified', 0) |
||
end |
end |
||
local lines=makeGalleryLinesTable(args) |
local lines=makeGalleryLinesTable(args) |
||
Line 45: | Line 45: | ||
return title.prefixedText .. '{{!}}' .. captiontext |
return title.prefixedText .. '{{!}}' .. captiontext |
||
end |
end |
||
function makeGalleryLinesTable(args) |
function makeGalleryLinesTable(args) |
||
Line 53: | Line 52: | ||
if not args.mode then |
if not args.mode then |
||
table.insert(galleryLinesTable, makeGalleryLine(args[i], args[i+1],args.link)) |
table.insert(galleryLinesTable, makeGalleryLine(args[i], args[i+1],args.link)) |
||
elseif args.mode == 'slideshow' then |
|||
table.insert(galleryLinesTable, makeGalleryLineSlideshow(args[i], args[i+1], args.link)) |
table.insert(galleryLinesTable, makeGalleryLineSlideshow(args[i], args[i+1], args.link)) |
||
else |
else |
||
error('Mode not supported') |
error('Mode not supported') |
||
end |
|||
end |
|||
i = i + 2 |
i = i + 2 |
||
end |
end |
||
return galleryLinesTable |
return galleryLinesTable |
||
end |
end |
||
function makeOutput(imageLines, overflow, maxHeight, mode, croptop) |
function makeOutput(imageLines, overflow, maxHeight, mode, croptop) |
||
local randomiseArgs = { ['t'] = imageLines } |
local randomiseArgs = { ['t'] = imageLines } |
||
Line 72: | Line 72: | ||
output = '<div class="portal-banner-image" style="max-height:' .. (maxHeight or 'initial') .. '; overflow:'..(overflow or 'auto').. |
output = '<div class="portal-banner-image" style="max-height:' .. (maxHeight or 'initial') .. '; overflow:'..(overflow or 'auto').. |
||
';"><div class="portal-banner-image-crop" style="position:relative; margin-top:-'..(croptop or '0')..'%;">'..seperate[1]..'</div></div>'..seperate[2] |
';"><div class="portal-banner-image-crop" style="position:relative; margin-top:-'..(croptop or '0')..'%;">'..seperate[1]..'</div></div>'..seperate[2] |
||
elseif mode == 'slideshow' then |
|||
galleryContent = table.concat(randomisedLines, '\n') |
galleryContent = table.concat(randomisedLines, '\n') |
||
output='<div class="portal-banner-image-slideshow nomobile" style="max-height:' .. (maxHeight or 'initial') .. '; overflow:'..(overflow or 'auto').. |
output='<div class="portal-banner-image-slideshow nomobile" style="max-height:' .. (maxHeight or 'initial') .. '; overflow:'..(overflow or 'auto').. |
||
Line 78: | Line 78: | ||
else |
else |
||
error('Mode not supported') |
error('Mode not supported') |
||
end |
|||
end |
end |
||
return output |
|||
end |
|||
return p |
return p |
Latest revision as of 20:55, 2 April 2023
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | 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 module depends on the following other modules: |
This module implements {{Portal image banner}}; see documentation there for usage instructions. This module will not work when used directly (as {{#invoke:Portal image banner|main}}). Rather it is advisable that the main template Portal image banner is used.
See also
local p = {}
local randomModule = require('Module:Random')
p.main = function(frame)
local parent = frame.getParent(frame)
local parentArgs = parent.args
local args = cleanupArgs(parentArgs)
local output = p._main(args)
return frame:preprocess(output)
end
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
p._main = function(args)
if not args[1] then
return error('No page specified', 0)
end
local lines=makeGalleryLinesTable(args)
return makeOutput(lines, args.overflow, args.maxheight, args.mode, args.croptop)
end
function makeGalleryLine(file, caption, link)
local title = mw.title.new(file, "File" )
local linktext = ( link and '{{!}}link=' .. link or '' )
local maxImageWidth = '{{!}}800px'
return '[[' .. title.prefixedText ..(caption and'{{!}}'..caption or '').. maxImageWidth .. linktext ..']]' .. (caption and '\n<div style="text-align:center;">' .. caption ..'</div>' or '\n')
end
function makeGalleryLineSlideshow(file, caption)
local title = mw.title.new(file, "File" )
local captiontext= '[[File:OOjs_UI_icon_info-progressive.svg|link=:'..title.prefixedText..']] <span style="font-size:110%;">'..(caption or '')..'</span>'
return title.prefixedText .. '{{!}}' .. captiontext
end
function makeGalleryLinesTable(args)
local galleryLinesTable = {}
local i = 1
while args[i] do
if not args.mode then
table.insert(galleryLinesTable, makeGalleryLine(args[i], args[i+1],args.link))
elseif args.mode == 'slideshow' then
table.insert(galleryLinesTable, makeGalleryLineSlideshow(args[i], args[i+1], args.link))
else
error('Mode not supported')
end
i = i + 2
end
return galleryLinesTable
end
function makeOutput(imageLines, overflow, maxHeight, mode, croptop)
local randomiseArgs = { ['t'] = imageLines }
local randomisedLines = randomModule.main('array', randomiseArgs )
local output, galleryContent
if not mode then
galleryContent = table.concat(randomisedLines, '\n',1,1)
seperate=mw.text.split(galleryContent,'\n')
output = '<div class="portal-banner-image" style="max-height:' .. (maxHeight or 'initial') .. '; overflow:'..(overflow or 'auto')..
';"><div class="portal-banner-image-crop" style="position:relative; margin-top:-'..(croptop or '0')..'%;">'..seperate[1]..'</div></div>'..seperate[2]
elseif mode == 'slideshow' then
galleryContent = table.concat(randomisedLines, '\n')
output='<div class="portal-banner-image-slideshow nomobile" style="max-height:' .. (maxHeight or 'initial') .. '; overflow:'..(overflow or 'auto')..
';"><div class="portal-banner-image-crop" style="position:relative; margin-top:-'..(croptop or '0')..'%;">'..'{{#tag:gallery|'..galleryContent..'|mode=slideshow}}'..'</div></div>'
else
error('Mode not supported')
end
return output
end
return p