Module:Listing: Difference between revisions

Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
remove remaining staging checks (WW Enemies, HW Characters). Migration is complete.
(handle transclusion (yikes). limitation: this expands templates in the transcluded page, which makes looking for things like {{Main}} harder.)
(remove remaining staging checks (WW Enemies, HW Characters). Migration is complete.)
 
(10 intermediate revisions by the same user not shown)
Line 25: Line 25:


function handleLine( line, currentSection )
function handleLine( line, currentSection )
  -- recurse on transclusion
  local transcludedTitle, args = line:match( '^{{:(.-)|(.-)}}$' )
  if transcludedTitle then
    local transcludedContent = mw.text.killMarkers( mw.getCurrentFrame():expandTemplate{ title = ':' .. transcludedTitle, args = { [1] = args } } )
    for transcludedLine in gsplit( transcludedContent, '\n' ) do
      currentSection = handleLine( transcludedLine, currentSection )
    end
    return currentSection
  end
   local headingLevel, headingText = string.match( line, '^%s*(=+)%s*(.-)%s*=+$' )
   local headingLevel, headingText = string.match( line, '^%s*(=+)%s*(.-)%s*=+$' )
   if headingLevel then -- line is a heading
   if headingLevel then -- line is a heading
Line 64: Line 53:
   until not contentBackfillSection
   until not contentBackfillSection


   return currentSection
  -- 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
end


Line 72: Line 65:


function Navbox.new( args )
function Navbox.new( args )
   local subject = args[1] .. ' ' .. args[2]
   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 79: 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, Navbox )
   return setmetatable( obj, Navbox )
end
end
Line 88: Line 81:


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 95: 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
     currentSection = handleLine( line, currentSection )
     currentSection, cat = handleLine( line, currentSection )
    if cat and cat ~= "Listings" then root.categories[#root.categories + 1] = cat end
   end
   end


Navigation menu