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.
(load /Staging subpage for LoZ)
m (fix image links to use the link in {{Main}})
 
(21 intermediate revisions by the same user not shown)
Line 4: Line 4:
local Tabs = require( 'Module:Tabs' ).Tabs
local Tabs = require( 'Module:Tabs' ).Tabs


function buildGallery( sections )
-- https://www.mediawiki.org/wiki/Help:Images#Syntax
   local gallery = Gallery.new{
-- doesn't handle 'upright' which seems like a complicated and buggy arg that I hope we're not using
     widths = '62px',
local fileArgKeywords = { 'border', 'frameless', 'frame', 'thumb', 'thumbnail', 'left', 'right', 'center', 'none', 'baseline', 'sub', 'super', 'top', 'text-top', 'middle', 'bottom', 'text-bottom' }
    heights = '62px'
function isCaption( fileArg )
   }
   fileArg = mw.text.trim( fileArg )
  -- named args
  if fileArg:match( '=' ) then return false end
  -- size
  if fileArg:match( '^%d*x?%d+%s?px$' ) then return false end
  -- format and alignment
  for _, keyword in ipairs( fileArgKeywords ) do
     if fileArg == keyword then return false end
  end
  return true
end
 
function buildGallery( sections, galArgs )
  galArgs.widths = galArgs.widths or '62px'
  galArgs.heights = galArgs.heights or '62px'
   local gallery = Gallery.new( galArgs )
   for _, section in ipairs( sections ) do
   for _, section in ipairs( sections ) do
    -- !!!! This may break if Template:Main is modified !!!!
    local expandedMainTemplate = section.summary:match( 'Main article: %[%[(.-)]]' )
    local link = expandedMainTemplate and mw.text.split( expandedMainTemplate, '|' )[1]
    if not section.summary:find( '%[%[File:(.-)]]' ) then
      gallery:addFile( 'No Image.png', '[[' .. (link or section.name) .. '|' .. section.name .. ']]', {
        link = section.name,
        alt = section.name
      } )
    end
     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 = nil
       gallery:addFile( fileName or 'No Image.png', '[[' .. section.name .. '|' .. (fileCaption or section.name) .. ']]', {
      for i = #fileParts, 2, -1 do -- start at the end and work backwards to the first arg (skip filename)
         link = section.name,
        if isCaption( fileParts[i] ) then
          fileCaption = fileParts[i]
          break
        end
      end
       gallery:addFile( fileName or 'No Image.png', '[[' .. (link or section.name) .. '|' .. (fileCaption or section.name) .. ']]', {
         link = link or section.name,
         alt = fileCaption or section.name
         alt = fileCaption or section.name
       } )
       } )
Line 31: Line 61:
function Galbox.new( args )
function Galbox.new( args )
   local obj = Navbox.new( args )
   local obj = Navbox.new( args )
  obj.galArgs = Args.getTable( args.gallery )
   return setmetatable( obj, Galbox )
   return setmetatable( obj, Galbox )
end
end
Line 36: Line 67:
-- override
-- override
function Galbox:renderContent()
function Galbox:renderContent()
   local listing = Listing._parseListing( self.categories[1] == 'The Legend of Zelda' and self.subject .. '/Staging' or self.subject )
   local listing = Listing._parseListing( self.subject )
 
  -- If categories weren't set already (because caller used single arg), then use the ones parsed from the page.
  -- CODE SMELL: setting unrelated state. this only works because renderFooter is called after renderContent.
  if #self.categories == 0 then self.categories = listing.categories end


   -- test if there are no sections at all
   -- test if there are no sections at all
Line 44: Line 79:
   local defaultGallery
   local defaultGallery
   if #listing.topLevelLeaves > 0 then
   if #listing.topLevelLeaves > 0 then
     defaultGallery = buildGallery( listing.topLevelLeaves )
     defaultGallery = buildGallery( listing.topLevelLeaves, self.galArgs )
   end
   end


Line 62: Line 97:
       tabs:addTabTopWithContent{
       tabs:addTabTopWithContent{
         contentId = group.name,
         contentId = group.name,
         content = buildGallery( group.sections )
         content = buildGallery( group.sections, self.galArgs )
       }
       }
     end
     end
Line 69: Line 104:
   end -- if #listing.groups > 0
   end -- if #listing.groups > 0


   return defaultGallery:render()
   return defaultGallery
end -- function Galbox:renderContent()
end -- function Galbox:renderContent()


Line 85: Line 120:
   getParent = function() return {
   getParent = function() return {
     args = {
     args = {
       [1] = "The Legend of Zelda",
       [1] = "The Legend of Zelda Characters"
      [2] = "Enemies",
     }
     }
   } end
   } end

Latest revision as of 23:57, September 8, 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

-- https://www.mediawiki.org/wiki/Help:Images#Syntax
-- doesn't handle 'upright' which seems like a complicated and buggy arg that I hope we're not using
local fileArgKeywords = { 'border', 'frameless', 'frame', 'thumb', 'thumbnail', 'left', 'right', 'center', 'none', 'baseline', 'sub', 'super', 'top', 'text-top', 'middle', 'bottom', 'text-bottom' }
function isCaption( fileArg )
  fileArg = mw.text.trim( fileArg )
  -- named args
  if fileArg:match( '=' ) then return false end
  -- size
  if fileArg:match( '^%d*x?%d+%s?px$' ) then return false end
  -- format and alignment
  for _, keyword in ipairs( fileArgKeywords ) do
    if fileArg == keyword then return false end
  end
  return true
end

function buildGallery( sections, galArgs )
  galArgs.widths = galArgs.widths or '62px'
  galArgs.heights = galArgs.heights or '62px'
  local gallery = Gallery.new( galArgs )
  for _, section in ipairs( sections ) do
    -- !!!! This may break if Template:Main is modified !!!!
    local expandedMainTemplate = section.summary:match( 'Main article: %[%[(.-)]]' )
    local link = expandedMainTemplate and mw.text.split( expandedMainTemplate, '|' )[1]
    if not section.summary:find( '%[%[File:(.-)]]' ) then
      gallery:addFile( 'No Image.png', '[[' .. (link or section.name) .. '|' .. section.name .. ']]', {
        link = section.name,
        alt = section.name
      } )
    end
    for file in section.summary:gmatch( '%[%[File:(.-)]]' ) do
      local fileParts = mw.text.split( file, '|' )
      local fileName = fileParts[1]
      local fileCaption = nil
      for i = #fileParts, 2, -1 do -- start at the end and work backwards to the first arg (skip filename)
        if isCaption( fileParts[i] ) then
          fileCaption = fileParts[i]
          break
        end
      end
      gallery:addFile( fileName or 'No Image.png', '[[' .. (link or section.name) .. '|' .. (fileCaption or section.name) .. ']]', {
        link = link or 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 )
  obj.galArgs = Args.getTable( args.gallery )
  return setmetatable( obj, Galbox )
end

-- override
function Galbox:renderContent()
  local listing = Listing._parseListing( self.subject )

  -- If categories weren't set already (because caller used single arg), then use the ones parsed from the page.
  -- CODE SMELL: setting unrelated state. this only works because renderFooter is called after renderContent.
  if #self.categories == 0 then self.categories = listing.categories end

  -- 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, self.galArgs )
  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, self.galArgs )
      }
    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 Characters"
    }
  } end
}

return p