Module:Listing: Difference between revisions

From Zelda Dungeon Wiki
Jump to navigation Jump to search
Want an adless experience? Log in or Create an account.
(comment out some things to try to reduce cpu usage... this doesn't seem like it should be so expensive though)
(replaced mw.text.gsplit with a much more performant implementation so things are snappy now)
Line 1: Line 1:
local p = {}
local p = {}


function p._parseListing( pageName )
-- got this from http://lua-users.org/wiki/SplitJoin
   local pageContent = mw.title.new( pageName ):getContent()
-- idk how it works but it's 100x more performant than mw.text.gsplit
function gsplit(s,sep)
  local lasti, done, g = 1, false, s:gmatch('(.-)'..sep..'()')
  return function()
      if done then
        return
      end
      local v,i = g()
      if s == '' or sep == '' then
        done = true
        return s
      end
      if v == nil then
        done = true
        return s:sub(lasti)
      end
      lasti = i
      return v
  end
end
 
function p._parseListing( pageName, pageContent )
   local pageContent = pageContent or mw.title.new( pageName ):getContent()
   local root = {
   local root = {
     level = 1,
     level = 1,
Line 13: Line 35:


   -- 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 mw.text.gsplit( pageContent, '\n' ) do
   for line in gsplit( pageContent, '\n' ) do
     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 32: Line 54:
       currentSection = newSection
       currentSection = newSection
     elseif #currentSection.sections == 0 then -- no child headings yet so this is part of the summary of the current section
     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'
      currentSection.summary = currentSection.summary .. line .. '\n'
     end
     end


     -- add to content of all parent sections, plus current section if line isn't a heading
     -- add to content of all parent sections, plus current section if line isn't a heading
-- content currently isn't needed so disabling this for perf
    local contentBackfillSection = headingLevel and currentSection.parent or currentSection
--    local contentBackfillSection = headingLevel and currentSection.parent or currentSection
    repeat
--    repeat
      contentBackfillSection.content = contentBackfillSection.content .. line .. '\n'
--      contentBackfillSection.content = contentBackfillSection.content .. line .. '\n'
      contentBackfillSection = contentBackfillSection.parent
--      contentBackfillSection = contentBackfillSection.parent
    until not contentBackfillSection
--    until not contentBackfillSection
   end
   end



Revision as of 06:01, July 24, 2020

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

local p = {}

-- got this from http://lua-users.org/wiki/SplitJoin
-- idk how it works but it's 100x more performant than mw.text.gsplit
function gsplit(s,sep)
   local lasti, done, g = 1, false, s:gmatch('(.-)'..sep..'()')
   return function()
      if done then
         return
      end
      local v,i = g()
      if s == '' or sep == '' then
         done = true
         return s
      end
      if v == nil then
         done = true
         return s:sub(lasti)
      end
      lasti = i
      return v
   end
end

function p._parseListing( pageName, pageContent )
  local pageContent = pageContent or mw.title.new( pageName ):getContent()
  local root = {
    level = 1,
    name = pageName,
    content = "",
    summary = "",
    sections = {}
  }
  local currentSection = root

  -- can't think of a regex to split sections so go line by line
  for line in gsplit( pageContent, '\n' ) do
    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
  end

  return root
end

function p.bullets( frame )
  local listing = p._parseListing( frame.args[1] )
  local bullets = mw.html.create( 'ul' )
  for _, section in ipairs( listing.sections ) do
    local sublist = bullets:tag( 'li' )
      :wikitext( section.name )
      :tag( 'ul' )
    for _, subsection in ipairs( section.sections ) do
      sublist:tag( 'li' ):wikitext( subsection.name )
    end
  end
  return tostring( bullets )
end

p.debugFrame = {
  args = {
    [1] = 'User:Locke/Sandbox/Listing'
  }
}

p.debugContent = [[
Summary {{Infobox|thing=value}}

== First Section ==
Section 1 summary {{Template|prop=val}}

=== Subsection of First Section ===
Subsection 1 content {{Template|prop=val}}

== Second Section ==
Section 2 summary {{Template|prop=val}}

=== Subsection of Second Section ===
Subsection 2 content {{Template|prop=val}}

{{Cat}}
]]

return p