Module:Listing: Difference between revisions

Want an adless experience? Log in or Create an account.
remove remaining staging checks (WW Enemies, HW Characters). Migration is complete.
(fix bool check on number (0 is not falsy))
(remove remaining staging checks (WW Enemies, HW Characters). Migration is complete.)
 
(13 intermediate revisions by the same user not shown)
Line 24: Line 24:
end
end


local Listbox = Box.new()
function handleLine( line, currentSection )
Listbox.__index = Listbox
  local headingLevel, headingText = string.match( line, '^%s*(=+)%s*(.-)%s*=+$' )
setmetatable( Listbox, Box )
  if headingLevel then -- line is a heading
    local newSection = {
      level = #headingLevel,
      name = headingText,
      content = "",
      summary = "",
      sections = {}
    }


function Listbox.new( args )
    -- find the right parent and insert
   local subject = args[1] .. ' ' .. args[2]
    while currentSection.level >= #headingLevel do
      currentSection = currentSection.parent
    end
    newSection.parent = currentSection
    currentSection.sections[#currentSection.sections + 1] = newSection
    currentSection = newSection
  elseif #currentSection.sections == 0 then -- no child headings yet so this is part of the summary of the current section
    currentSection.summary = currentSection.summary .. line .. '\n'
  end
 
  -- add to content of all parent sections, plus current section if line isn't a heading
  local contentBackfillSection = headingLevel and currentSection.parent or currentSection
  repeat
    contentBackfillSection.content = contentBackfillSection.content .. line .. '\n'
    contentBackfillSection = contentBackfillSection.parent
  until not contentBackfillSection
 
  -- determine if line is a category
  local cat = string.match( line, '^%s*%[%[Category:(.-)]]%s*$' )
  cat = cat and mw.text.split( cat, '|' )[1]
 
  return currentSection, cat
end
 
local Navbox = Box.new()
Navbox.__index = Navbox
setmetatable( Navbox, Box )
 
function Navbox.new( args )
   local subject = args[2] and (args[1] .. ' ' .. args[2]) or args[1]
   args.class = 'navbox'
   args.class = 'navbox'
   args.title = '[[' .. subject .. ']]'
   args.title = '[[' .. subject .. ']]'
Line 36: Line 72:
   local obj = Box.new( 'light', args )
   local obj = Box.new( 'light', args )
   obj.subject = subject
   obj.subject = subject
   obj.categories = { args[1], subject, args[2] }
   obj.categories = args[2] and { args[1], subject, args[2] } or {} -- will be set later when parsing the page
   return setmetatable( obj, Listbox )
   return setmetatable( obj, Navbox )
end
end


local p = {}
local p = {}


p.Listbox = Listbox
p.Navbox = Navbox


function p._parseListing( pageName, pageContent )
function p._parseListing( pageName, pageContent )
   local pageContent = pageContent or mw.title.new( pageName ):getContent()
   local pageContent = pageContent or mw.text.killMarkers( mw.getCurrentFrame():expandTemplate{ title = ':' .. pageName, args = {} } )
   local root = {
   local root = {
     level = 1,
     level = 1,
Line 52: Line 88:
     summary = "",
     summary = "",
     sections = {},
     sections = {},
    categories = {},
     topLevelLeaves = {}, -- i.e. L2 sections with no children
     topLevelLeaves = {}, -- i.e. L2 sections with no children
     groups = {} -- i.e. L2 sections that have children
     groups = {} -- i.e. L2 sections that have children
   }
   }
   local currentSection = root
   local currentSection = root
  local cat = nil


   -- can't think of a regex to split sections so go line by line
   -- can't think of a regex to split sections so go line by line
   for line in gsplit( pageContent, '\n' ) do
   for line in gsplit( pageContent, '\n' ) do
     local headingLevel, headingText = string.match( line, '^%s*(=+)%s*(.-)%s*=+$' )
     currentSection, cat = handleLine( line, currentSection )
     if headingLevel then -- line is a heading
     if cat and cat ~= "Listings" then root.categories[#root.categories + 1] = cat end
      local newSection = {
        level = #headingLevel,
        name = headingText,
        content = "",
        summary = "",
        sections = {}
      }
 
      -- find the right parent and insert
      while currentSection.level >= #headingLevel do
        currentSection = currentSection.parent
      end
      newSection.parent = currentSection
      currentSection.sections[#currentSection.sections + 1] = newSection
      currentSection = newSection
    elseif #currentSection.sections == 0 then -- no child headings yet so this is part of the summary of the current section
      currentSection.summary = currentSection.summary .. line .. '\n'
    end
 
    -- add to content of all parent sections, plus current section if line isn't a heading
    local contentBackfillSection = headingLevel and currentSection.parent or currentSection
    repeat
      contentBackfillSection.content = contentBackfillSection.content .. line .. '\n'
      contentBackfillSection = contentBackfillSection.parent
    until not contentBackfillSection
   end
   end


Line 93: Line 106:
       root.groups[#root.groups + 1] = section
       root.groups[#root.groups + 1] = section
     else
     else
       root.topLevelLeaves[#topLevelLeaves + 1] = section
       root.topLevelLeaves[#root.topLevelLeaves + 1] = section
     end
     end
   end
   end
Line 116: Line 129:
p.debugFrame = {
p.debugFrame = {
   args = {
   args = {
     [1] = 'User:Locke/Sandbox/Listing'
     [1] = 'The Legend of Zelda Locations'
   }
   }
}
}