Module:Gallery: Difference between revisions
Jump to navigation
Jump to search
Want an adless experience? Log in or Create an account.
(For now this is intended to just e a meta-module for generating <gallery> blocks within modules. In wikitext, it's easier to just use <gallery> directly, so this does not expose functions to be called from templates) |
(center gallery captions by default) |
||
(One intermediate revision by the same user not shown) | |||
Line 29: | Line 29: | ||
function Gallery.new( args ) | function Gallery.new( args ) | ||
args.style = 'text-align:center' -- TODO need to be smarter about this if we want to allow overriding it or adding other styles | |||
return setmetatable( { | return setmetatable( { | ||
args = args, | args = args, | ||
files = {} | files = {} | ||
}, | }, Gallery ) | ||
end | end | ||
Latest revision as of 00:53, July 21, 2020
Documentation for this module may be created at Module:Gallery/doc
local File = {} File.__index = File function File.new( filename, caption, args ) return setmetatable( { filename = filename, caption = type( caption ) == 'string' and caption or nil, args = args or type( caption ) == 'table' and caption or nil }, File ) end function File:render() local parts = {} parts[1] = self.filename if self.caption then parts[2] = self.caption end if self.args then for k, v in pairs(self.args) do parts[#parts + 1] = k .. '=' .. v end end return table.concat( parts, '|' ) end local Gallery = {} Gallery.__index = Gallery function Gallery.new( args ) args.style = 'text-align:center' -- TODO need to be smarter about this if we want to allow overriding it or adding other styles return setmetatable( { args = args, files = {} }, Gallery ) end function Gallery:addFile( filename, caption, args ) self.files[#self.files + 1] = File.new( filename, caption, args ) end function Gallery:render() local lines = {} for index, file in ipairs( self.files ) do lines[index] = file:render() end return mw.getCurrentFrame():extensionTag( 'gallery', table.concat( lines, '\n' ), self.args ) end local p = {} p.Gallery = Gallery return p