Module:Gallery: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
(fix Gallery ctor's metatable)
(center gallery captions by default)
 
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,

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