Module:Listbox: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
(allow display names that differ from page names)
(match expanded {{Main}} in the case that it's in a transcluded page)
Line 7: Line 7:
   for _, section in ipairs( sections ) do
   for _, section in ipairs( sections ) do
     local mainTemplate = section.summary:match( '%{%{Main|(.-)}}' )
     local mainTemplate = section.summary:match( '%{%{Main|(.-)}}' )
     local link = mainTemplate and mw.text.split( mainTemplate, '|' )[1]
    -- !!!! This may break if Template:Main is modified !!!!
    local expandedMainTemplate = section.summary:match( 'Main article: %[%[(.-)]]' )
     local link = mainTemplate and mw.text.split( mainTemplate, '|' )[1] or expandedMainTemplate and mw.text.split( expandedMainTemplate, '|' )[1]
     list:tag( 'li' )
     list:tag( 'li' )
       :wikitext( '[[' .. (link or section.name) .. '|' .. section.name .. ']]' )
       :wikitext( '[[' .. (link or section.name) .. '|' .. section.name .. ']]' )
Line 65: Line 67:
     args = {
     args = {
       [1] = "The Legend of Zelda",
       [1] = "The Legend of Zelda",
       [2] = "Characters"
       [2] = "Locations"
     }
     }
   } end
   } end

Revision as of 03:59, July 26, 2020

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

local Args = require( 'Module:Args' )
local Listing = require( 'Module:Listing' )

function buildHList( parent, sections )
  local list = parent:addClass( 'hlist' )
    :tag( 'ul' )
  for _, section in ipairs( sections ) do
    local mainTemplate = section.summary:match( '%{%{Main|(.-)}}' )
    -- !!!! This may break if Template:Main is modified !!!!
    local expandedMainTemplate = section.summary:match( 'Main article: %[%[(.-)]]' )
    local link = mainTemplate and mw.text.split( mainTemplate, '|' )[1] or expandedMainTemplate and mw.text.split( expandedMainTemplate, '|' )[1]
    list:tag( 'li' )
      :wikitext( '[[' .. (link or section.name) .. '|' .. section.name .. ']]' )
  end
end

local Navbox = Listing.Navbox

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

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

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

  local content = mw.html.create( 'table' )
  if #listing.topLevelLeaves > 0 then
    local defaultCell = content:tag( 'tr' )
      :tag( 'td' )
      :addClass( 'odd' )
      :attr( 'colspan', '2' )
      :css( 'text-align', 'center' )
    buildHList( defaultCell, listing.topLevelLeaves )
  end
  if #listing.groups > 0 then
    for index, group in ipairs( listing.groups ) do
      local row = content:tag( 'tr' )
      row:tag( 'th' )
        :addClass( 'label' )
        :wikitext( group.name )
      local cell = row:tag( 'td' )
        :addClass( (index + (#listing.topLevelLeaves > 0 and 1 or 0)) % 2 == 0 and 'even' or 'odd' )
      buildHList( cell, group.sections )
    end
  end

  return content
end

local p = {}

function p.main( frame )
  local listbox = Listbox.new( Args.fromFrame( frame ) )
  return listbox: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] = "Locations"
    }
  } end
}

return p