Module:PagedGallery: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
m (it doesn't like braces)
m (fix regex escapes)
Line 2: Line 2:
local Box = require( 'Module:Box' ).Box
local Box = require( 'Module:Box' ).Box


local PAGE_NUMBER_FORMAT = "\((#+)\)"
local PAGE_NUMBER_FORMAT = "%((#+)%)"


local PagedGallery = Box.new()
local PagedGallery = Box.new()
Line 74: Line 74:
   getParent = function() return {
   getParent = function() return {
     args = {
     args = {
       format = 'The-Legend-of-Zelda-North-American-Instruction-Manual-Page-{##}.jpg',
       format = 'The-Legend-of-Zelda-North-American-Instruction-Manual-Page-(##).jpg',
       numPages = 47
       numPages = 47
     }
     }

Revision as of 01:29, April 3, 2021

Documentation for this module may be created at Module:PagedGallery/doc

local Args = require( 'Module:Args' )
local Box = require( 'Module:Box' ).Box

local PAGE_NUMBER_FORMAT = "%((#+)%)"

local PagedGallery = Box.new()
PagedGallery.__index = PagedGallery
setmetatable( PagedGallery, Box )

function PagedGallery.new( args )
  args.class = 'zdw-paged-gallery'
  local width = args.width or 300
  args.width = width * 2 .. 'px'
  local obj = Box.new( 'light', args )
  obj.imageWidth = width .. 'px'
  obj.pageNumReplacementFormat = "%0" .. #string.match( args.format, PAGE_NUMBER_FORMAT ) .. "d"
  return setmetatable( obj, PagedGallery )
end

function addButton( nav, kind, text )
  nav:tag( 'div' )
    :addClass( 'zdw-button zdw-button--paging' )
    :attr( 'data-nav', kind )
    :wikitext( text )
end

-- override Box:renderContent
function PagedGallery:renderContent()
  local content = mw.html.create( 'div' );
  local nav = content:tag( 'div' )
    :addClass( 'zdw-paged-gallery__navigation' )
  addButton( nav, 'first', '<< First' )
  addButton( nav, 'prev', '< Previous' )
  addButton( nav, 'next', 'Next >' )
  addButton( nav, 'last', 'Last >>' )

  -- cover
  content:tag( 'div' )
    :addClass( 'zdw-paged-gallery__page active' )
    :wikitext( self:renderImage( 0 ) )

  -- pages
  for i = 1, self.args.numPages - 1, 2 do
    content:tag( 'div' )
      :addClass( 'zdw-paged-gallery__page' )
      :wikitext( self:renderImage( i ) .. self:renderImage( i + 1 ) )
  end

  -- back
  if self.args.numPages % 2 == 1 then
    content:tag( 'div' )
      :addClass( 'zdw-paged-gallery__page' )
      :wikitext( self:renderImage( self.args.numPages ) )
  end

  return content
end

function PagedGallery:renderImage( pageNum )
  return '[[File:' .. self.args.format:gsub( PAGE_NUMBER_FORMAT, string.format( self.pageNumReplacementFormat, pageNum ) ) .. '|' .. self.imageWidth .. ']]'
end

local p = {}

function p.main( frame )
  local gallery = PagedGallery.new( Args.fromFrame( frame ) )
  return gallery:render()
end

-- for use in the debug console:
-- =p.main(p.debugframe)
p.debugframe = {
  args = {},
  getParent = function() return {
    args = {
      format = 'The-Legend-of-Zelda-North-American-Instruction-Manual-Page-(##).jpg',
      numPages = 47
    }
  } end
}

return p