Module:Subpages 2

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

local sp = {}

local majorpages = { -- ["Main"] = true, ["Analysis"] = true, ["Characters"] = true, ["Laconic"] = true, ["Recap"] = true, ["Trivia"] = true, ["Setting"] = true, ["YMMV"] = true, } local cmoapages = { ["Awesome"] = true, ["Funny"] = true, ["Heartwarming"] = true, ["Nightmare Fuel"] = true, ["Tear Jerker"] = true, } local minorpages = { ["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, ["All Subpages"] = true, -- ["Wikipedia"] = true, }

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) + 1

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"] = {}

for _, spfull in ipairs(allsp) do       spx = string.sub(spfull, pagelenoffset) category, ssp = split(spx, "/", 1)

if subpagelist[category] then table.insert(foundpages[category], ssp) else table.insert(foundpages["allother"], spx) end end

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

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 cat ~= "" 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 foundpages 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') for _, cat in foundpages["allother"] do       table.insert(linkout, "*" .. linkup( page, cat )) end

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

function sp.subpages_old( frame ) page = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE') allsp = frame:callParserFunction('SUBPAGES', { page, sep = "|" }) links = {} local major = {} local cmoa = {} local minor = {} local other = {} for i in string.gmatch(allsp, page .. "/([^%|]+)") do       links[i] = true if majorpages[i] then table.insert(major, i)       elseif cmoapages[i] then table.insert(cmoa, i)        elseif minorpages[i] then table.insert(minor, i)       else table.insert(other, i)       end --table.insert(links, "" .. page .. "/" .. i .. "\n\n") end

-- initialize result with Main link local linkout = { '* Main\n' } for _, sp in ipairs(major) do       table.insert(linkout, linkup( page, sp )) end table.insert(linkout, '* Superlatives\n') for _, sp in ipairs(cmoa) do        table.insert(linkout, "*" .. linkup( page, sp )) end for _, sp in ipairs(minor) do       table.insert(linkout, linkup( page, sp )) end table.insert(linkout, "* Wikipedia\n') local tmp = "foo/bar/baz" local tmp2 = split(tmp, "/", 1)

return ' \n' .. table.concat(linkout) .. '* All Subpages' .. ' ' ..   table.concat(other, "\t") .. ' '       .. ' \n' ..' '.. table.concat(tmp2, ';') ..' \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