Module:Galbox: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
m (I thought that seemed weird)
(allow display names that differ from page names)
Line 10: Line 10:
   }
   }
   for _, section in ipairs( sections ) do
   for _, section in ipairs( sections ) do
    local mainTemplate = section.summary:match( '%{%{Main|(.-)}}' )
    local link = mainTemplate and mw.text.split( mainTemplate, '|' )[1]
     for file in section.summary:gmatch( '%[%[File:(.-)]]' ) do
     for file in section.summary:gmatch( '%[%[File:(.-)]]' ) do
       local fileParts = mw.text.split( file, '|' )
       local fileParts = mw.text.split( file, '|' )
       local fileName = fileParts[1]
       local fileName = fileParts[1]
       local fileCaption = fileParts[3] -- TODO need to be smarter about this. for now, assuming fileName|right|fileCaption
       local fileCaption = fileParts[3] -- TODO need to be smarter about this. for now, assuming fileName|right|fileCaption
       gallery:addFile( fileName or 'No Image.png', '[[' .. section.name .. '|' .. (fileCaption or section.name) .. ']]', {
       gallery:addFile( fileName or 'No Image.png', '[[' .. (link or section.name) .. '|' .. (fileCaption or section.name) .. ']]', {
         link = section.name,
         link = section.name,
         alt = fileCaption or section.name
         alt = fileCaption or section.name
Line 86: Line 88:
     args = {
     args = {
       [1] = "The Legend of Zelda",
       [1] = "The Legend of Zelda",
       [2] = "Enemies",
       [2] = "Characters",
     }
     }
   } end
   } end

Revision as of 01:42, July 26, 2020

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

local Args = require( 'Module:Args' )
local Gallery = require( 'Module:Gallery' ).Gallery
local Listing = require( 'Module:Listing' )
local Tabs = require( 'Module:Tabs' ).Tabs

function buildGallery( sections )
  local gallery = Gallery.new{
    widths = '62px',
    heights = '62px'
  }
  for _, section in ipairs( sections ) do
    local mainTemplate = section.summary:match( '%{%{Main|(.-)}}' )
    local link = mainTemplate and mw.text.split( mainTemplate, '|' )[1]
    for file in section.summary:gmatch( '%[%[File:(.-)]]' ) do
      local fileParts = mw.text.split( file, '|' )
      local fileName = fileParts[1]
      local fileCaption = fileParts[3] -- TODO need to be smarter about this. for now, assuming fileName|right|fileCaption
      gallery:addFile( fileName or 'No Image.png', '[[' .. (link or section.name) .. '|' .. (fileCaption or section.name) .. ']]', {
        link = section.name,
        alt = fileCaption or section.name
      } )
    end
  end

  return gallery:render()
end

local Navbox = Listing.Navbox

local Galbox = setmetatable( {}, Navbox )
Galbox.__index = Galbox

function Galbox.new( args )
  local obj = Navbox.new( args )
  return setmetatable( obj, Galbox )
end

-- override
function Galbox:renderContent()
  local listing = Listing._parseListing( self.categories[1] == 'The Legend of Zelda' and self.subject .. '/Staging' or self.subject )

  -- test if there are no sections at all
  if not #listing.sections then return '' end

  -- build default gallery for top-level items
  local defaultGallery
  if #listing.topLevelLeaves > 0 then
    defaultGallery = buildGallery( listing.topLevelLeaves )
  end

  -- build tabs for nested items
  if #listing.groups > 0 then
    local tabs = Tabs.new()

    -- add a tab for the default gallery
    if defaultGallery then
      tabs:addTabTopWithContent{
        contentId = 'General',
        content = defaultGallery
      }
    end

    for index, group in ipairs( listing.groups ) do
      tabs:addTabTopWithContent{
        contentId = group.name,
        content = buildGallery( group.sections )
      }
    end

    return tabs:render()
  end -- if #listing.groups > 0

  return defaultGallery
end -- function Galbox:renderContent()

local p = {}

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

-- for use in the debug console:
-- =p.main(p.debugframe)
p.debugframe = {
  args = {},
  getParent = function() return {
    args = {
      [1] = "The Legend of Zelda",
      [2] = "Characters",
    }
  } end
}

return p