Module:Subpages 2

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

local sp = {}

local pageorder = { ["trope"] = { "Analysis", "Haiku", "Headscratchers", "Image Links", "Laconic", "Playing With", "Quotes", "Useful Notes" },   ["work"] = { "Analysis", "Characters", "Laconic", "Recap", "Trivia", "Setting", "YMMV", "Fridge", "Fanfic Recs", "Haiku", "Headscratchers", "Ho Yay", "Image Links", "Memes", "Radar", "Quotes", "Reviews", "Useful Notes", "WMG" },   ["creator"] = {"YMMV", "Quotes", "Fanfic Recs", "Analysis", "Trivia", "WMG", "Image Links", "Haiku", "Laconic"} } local otherpages = { ["trope"] = { "Animation", "Animated Films", "Anime", "Anime and Manga", "Board Games", "Comics", "Comic Books", "Fan Works", "Fanfiction", "Film", "Literature", "Live Action Television", "Live Action TV", "Machinima", "Manga", "Manga and Anime", "Music", "Musicians", "New Media", "Professional Wrestling", "Puppet Shows", "Radio", "Tabletop Games", "Television", "TV", "Theatre", "Theater", "Toys", "Webcomics", "Video Games", "Visual Art", "Visual Novels", "Web Original", "Web Animation", "Web Comics", "Websites", "Western Animation", "Wikis", "Wrestling", "Other", "Real Life" }, ["work"] = { "Awesome", "Funny", "Heartwarming", "Nightmare Fuel", "Tear Jerker" }, ["creator"] = { "Awesome", "Funny", "Heartwarming", "Nightmare Fuel", "Tear Jerker" } } local otherpagename = { ["trope"] = "By Medium", ["work"] = "Crowners", ["creator"] = "Crowners" }

local subpagelist = { } for tmpl, _ in pairs(pageorder) do	subpagelist[tmpl] = { } for _, pg in pairs(pageorder[tmpl]) do subpagelist[tmpl][pg] = true end for _, pg in pairs(otherpages[tmpl]) do subpagelist[tmpl][pg] = true end end

function sp.subpages( frame ) local page = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE') local template = frame.args[2] or "work" if pageorder[template] == nil then return "Module error: invalid template argument" end local pagelenoffset = string.len(page) + 2

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

local foundpages = { } for _, cat in ipairs(pageorder[template]) do foundpages[cat] = {} end for _, cat in ipairs(otherpages[template]) 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

if subpagelist[template][pageparts[1]] then table.insert(foundpages[pageparts[1]], pageparts[2]) else table.insert(foundpages["allother"], spx) end end

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

for _, cat in ipairs(pageorder[template]) 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[template]) do       if next(foundpages[cat]) ~= nil then moments = 1 end end if moments > 0 then table.insert(linkout, '*  '..otherpagename[template]..'\n') for _,cat in ipairs(otherpages[template]) 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