17,273
edits
(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 | 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' | |||
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 | ||
local contentBackfillSection = headingLevel and currentSection.parent or currentSection | |||
repeat | |||
contentBackfillSection.content = contentBackfillSection.content .. line .. '\n' | |||
contentBackfillSection = contentBackfillSection.parent | |||
until not contentBackfillSection | |||
end | end | ||