Module:Subpages 3

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 crowners = { ["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 crownername = { ["trope"] = "By Medium", ["work"] = "Crowners", ["creator"] = "Crowners" }

function sp.menu ( frame ) local page	 = frame.args[1] or frame:callParserFunction('TOPLEVELPAGE') local template = frame.args[2] or "work" local wppage  = frame.args[3] or page local haswp	= true --(frame.args[4] == '') if wppage == "" then wppage = page end

-- create variant top level page variables local mainpage = page; if template == "index" then template = "trope" mainpage = ":" .. page page = page:gsub("Category:", "", 1) end local pageslash = page..'/'

if pageorder[template] == nil then return "Module error: invalid template argument" end

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

local tree = build_tree(subpage_list)

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

for _, subpage in ipairs(pageorder[template]) do		text = build_list( { [subpage] = tree[subpage] }, pageslash, 1 ) if text ~= '' then table.insert(linkout, text) else table.insert(missingout, subpage) end tree[subpage] = nil end for _, subpage in ipairs(crowners[template]) do		text = build_list( { [subpage] = tree[subpage] }, pageslash, 2 ) if text ~= '' then table.insert(crownerout, text) else table.insert(missingout, subpage) end tree[subpage] = nil end --deal with the leftover subpages if next(tree) then local ordered_keys = {} for k in pairs(tree) do			table.insert(ordered_keys, k)		end table.sort(ordered_keys)

for i = 1, #ordered_keys do			local subpage = ordered_keys[i] text = build_list( { [subpage] = tree[subpage] }, pageslash, 2 ) if text ~= '' then table.insert(otherout, text) end end end

local jsdata = ' '	local crowner_link = next(crownerout) and '*  '..crownername[template]..'\n' or '' local wikipedia_link = haswp and "* Wikipedia\n' or '' local all_sp_link = '* All Subpages\n'

return table.concat(linkout) .. crowner_link .. table.concat(crownerout) .. wikipedia_link .. all_sp_link .. table.concat(otherout) .. '* Create New \n' .. jsdata end

function build_tree ( subpage_list ) local path_tree = { } for _, path in ipairs(subpage_list) do		local list = split(path, '/') local lastdir = path_tree for i, dir in ipairs(list) do			if i ~= 1 then lastdir[dir] = { } lastdir = lastdir[dir] end end end return path_tree end

function build_list ( tree, prefix, level ) prefix = prefix or '' level = level  or 1 local libase = mw.ustring.rep('*', level) local ul = '' for page, branch in pairs(tree) do		local li = libase if level == 1 then li = libase .. " '..page..'\n' else li = libase ..' '..page..'\n' end

if next(branch) then local nextprefix = (prefix..page..'/') li = li .. build_list(branch, nextprefix, level+1) end ul = ul .. li	end return ul end

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

local aRecord = {}

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

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

return aRecord end

return sp