Module:Subpage List: Difference between revisions

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 18: Line 18:
title = mw.title.getCurrentTitle()
title = mw.title.getCurrentTitle()
end
end
local subpages = utilsPage.dpl({
local subpages = p.getSubpages(title)
return p.buildTree(title, subpages, recurse)
end
 
function p.getSubpages(title)
if not title then
title = mw.title.getCurrentTitle()
elseif title == "string" then
title = mw.title.create(title)
end
return utilsPage.dpl({
namespace= title.nsText,
namespace= title.nsText,
titlematch= '%' .. title.text .. '/%',
titlematch= '%' .. title.text .. '/%',
})
})
return p.buildTree(title, subpages, recurse)
end
end



Revision as of 17:12, 27 October 2022


local p = {}

local utilsPage = require("Module:UtilsPage")

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 = p.getSubpages(title)
	return p.buildTree(title, subpages, recurse)
end

function p.getSubpages(title)
	if not title then
		title = mw.title.getCurrentTitle()
	elseif title == "string" then
		title = mw.title.create(title)
	end
	return utilsPage.dpl({
		namespace= title.nsText,
		titlematch= '%' .. title.text .. '/%',
	})
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 '<ul>'
	if not root.children then
		return ''
	end
	for nodeKey, node in pairs(root.children) do
		list = list .. string.format('<li>[[%s|%s]]</li>', node.value, nodeKey)
		list = list .. p.displayTree(node)
	end
	return list .. '</ul>'
end

return p