Module:Subpages 2

-- subpages = require('Module:Subpages).subpages -- for page in subpages('Page') do ... end

local sp = {}

local pageorder = { "Analysis", "Characters", "Laconic", "Recap", "Trivia", "Setting", "YMMV", "Fridge", "Fanfic Recs", "Haiku", "Headscratchers", "Ho Yay", "Image Links", "Memes", "Radar", "Quotes", "Reviews", "Shout Out", "Useful Notes", "WMG" } local otherpages = { "Awesome", "Funny", "Heartwarming", "Nightmare Fuel", "Tear Jerker" } local subpagelist = { ["Analysis"] = true, ["Characters"] = true, ["Laconic"] = true, ["Recap"] = true, ["Trivia"] = true, ["Setting"] = true, ["YMMV"] = true, ["Awesome"] = true, ["Funny"] = true, ["Heartwarming"] = true, ["Nightmare Fuel"] = true, ["Tear Jerker"] = true, ["Fridge"] = true, ["Fanfic Recs"] = true, ["Haiku"] = true, ["Headscratchers"] = true, ["Ho Yay"] = true, ["Image Links"] = true, ["Memes"] = true, ["Radar"] = true, ["Quotes"] = true, ["Reviews"] = true, ["Shout Out"] = true, ["Useful Notes"] = true, ["WMG"] = true, }

function sp.subpages( frame ) page = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE') pagelenoffset = string.len(page) + 2

allsptxt = frame:callParserFunction('SUBPAGES', { page, sep = "|" }) allsp = split(allsptxt, "|")

local foundpages = { } for _, cat in ipairs(pageorder) do foundpages[cat] = {} end for _, cat in ipairs(otherpages) do foundpages[cat] = {} end foundpages["allother"] = {}

categorytmp = { }

for _, spfull in ipairs(allsp) do       spx = string.sub(spfull, pagelenoffset) pageparts = split(spx, "/", 1) if pageparts[2] == nil then pageparts[2] = '' end

--table.insert(categorytmp, category) if subpagelist[pageparts[1]] then table.insert(foundpages[pageparts[1]], pageparts[2]) --return "1" .. category else table.insert(foundpages["allother"], spx) end end --if 1==1 then return table.concat(categorytmp, "|") end

-- initialize result with Main link local linkout = { '* Main\n' } local wantedpages = { }

for _, cat in ipairs(pageorder) do       if next(foundpages[cat]) == nil then table.insert(wantedpages, cat) else table.insert( linkout, linkup( page, cat )) for _, ssp in ipairs(foundpages[cat]) do               if ssp ~= "" then table.insert( linkout, "*" .. linkup( page, cat .. '/' .. ssp )) end end end end local moments = 0 for _, cat in ipairs(otherpages) do       if next(foundpages[cat]) ~= nil then moments = 1 end end if moments > 0 then table.insert(linkout, '* Superlatives\n') for _,cat in ipairs(otherpages) do           for _, ssp in ipairs(foundpages[cat]) do                if ssp == '' then table.insert( linkout, "*" .. linkup( page, cat )) else table.insert( linkout, "*" .. linkup( page, cat .. '/' .. ssp )) end end end end table.insert(linkout, "* Wikipedia\n') table.insert(linkout, '* All Subpages\n') for _, cat in ipairs(foundpages["allother"]) do       table.insert(linkout, "*" .. linkup( page, cat )) end

return table.concat(linkout) .. "* Create New' .. ' ' .. table.concat( wantedpages, "\t" ) .. ' '       .. '\n' .. ' /wiki/'..page..' \n' end

function linkup( pagename, subpage ) return '* ' .. subpage .."\n" end

function split(str, sSeparator, nMax, bRegexp) assert(sSeparator ~= '') assert(nMax == nil or nMax >= 1)

local aRecord = {}

if string.len(str) > 0 then local bPlain = not bRegexp nMax = nMax or -1

local nField=1 nStart=1 local nFirst,nLast = string.find(str, sSeparator, nStart, bPlain) while nFirst and nMax ~= 0 do           aRecord[nField] = string.sub(str, nStart, nFirst-1) nField = nField+1 nStart = nLast+1 nFirst,nLast = string.find(str, sSeparator, nStart, bPlain) nMax = nMax-1 end aRecord[nField] = string.sub(str, nStart) end

return aRecord end

return sp