Module:Listing: Difference between revisions

Want an adless experience? Log in or Create an account.
handle transclusion (yikes). limitation: this expands templates in the transcluded page, which makes looking for things like {{Main}} harder.
mNo edit summary
(handle transclusion (yikes). limitation: this expands templates in the transcluded page, which makes looking for things like {{Main}} harder.)
Line 22: Line 22:
       return v
       return v
   end
   end
end
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*=+$' )
  if headingLevel then -- line is a heading
    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
  return currentSection
end
end


Line 59: Line 102:
   -- 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 = handleLine( line, currentSection )
    if headingLevel then -- line is a heading
      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 116: Line 133:
p.debugFrame = {
p.debugFrame = {
   args = {
   args = {
     [1] = 'User:Locke/Sandbox/Listing'
     [1] = 'The Legend of Zelda Locations'
   }
   }
}
}