Module:Subpage List

local p = {}

local dpl = require("Module:UtilsDPL").dpl

function p.Main(frame) local args = frame:getParent.args local pageName = args[1] local recurse = tonumber(args["recurse"]) local pageTree = p.main(pageName, recurse) return p.displayTree(pageTree) end

function p.main(pageName, recurse) local title if pageName then title = mw.title.new(pageName) else title = mw.title.getCurrentTitle end local subpages = dpl({		namespace= title.nsText,		titlematch= '%' .. title.text .. '/%',	}) return p.buildTree(title, subpages, recurse) end

function p.buildTree(title, subpages, recurse, root) if root == nil then root = root or {} root.value = title.text p.buildTree(title, subpages, recurse, root) return root; end local children for _, subpage in pairs(subpages) do		local subpageTitle = mw.title.new(subpage) if subpageTitle.baseText == title.text then children = children or {} local node = { value= subpageTitle.fullText } local nodeKey = subpageTitle.subpageText children[nodeKey] = node if recurse == nil or recurse > 0 then p.buildTree(subpageTitle, subpages, recurse and recurse - 1, node) end end end root.children = children return root end

function p.displayTree(root, list) local list = list or '' if not root.children then return '' end for nodeKey, node in pairs(root.children) do list = list .. string.format('%s', node.value, nodeKey) list = list .. p.displayTree(node) end return list .. '' end

return p