Module:Manual

Documentation for this module may be created at Module:Manual/doc

local Manual = {}
 
 
local function tocNode(pageName, subpages)
    return { name = pageName, subpages = subpages or {} }
end
 
-- TODO: Install latest version of Scribunto so we can use:
-- mw.text.unstrip(frame:preprocess("{{Special:PrefixIndex/Manual/}}"))
-- As is there is no other way of getting a list of subpages. When transcluding mediawiki
-- uses strip markers
local function generateTOCTable()
    return tocNode("Manual", {
        tocNode("Manual/Studio_Overview", {
            tocNode("Manual/Studio_Overview/Interface", {
                tocNode("Manual/Studio_Overview/Interface/Home"),
                tocNode("Manual/Studio_Overview/Interface/Model"),
                tocNode("Manual/Studio_Overview/Interface/Terrain"),
                tocNode("Manual/Studio_Overview/Interface/Test"),
                tocNode("Manual/Studio_Overview/Interface/View"),
                tocNode("Manual/Studio_Overview/Interface/Plugins"),
                tocNode("Manual/Studio_Overview/Interface/Toolbox"),
                tocNode("Manual/Studio_Overview/Interface/Game_explorer"),
                tocNode("Manual/Studio_Overview/Interface/Explorer"),
                tocNode("Manual/Studio_Overview/Interface/Properties")
			}),
            tocNode("Manual/Studio_Overview/Games_and_places"),
            tocNode("Manual/Studio_Overview/Publishing_and_saving"),
            tocNode("Manual/Studio_Overview/Customization", {
                tocNode("Manual/Studio_Overview/Customization/Move_windows"),
                tocNode("Manual/Studio_Overview/Customization/Quick_access_toolbar"),
                tocNode("Manual/Studio_Overview/Customization/Hotkeys"),
                --tocNode( "Manual/Studio_Overview/Customization/Settings")
            })
		})
	})
end
 
local function printNode(node)
    mw.log(node.name .. " " .. node.link)
    for _, subnode in pairs(node.subpages) do
        printNode(subnode)
    end
end
 
local function IsDescendant(currentPage, node)
    local shortPage = string.sub(currentPage, 1, string.len(node.name))
    mw.log("Checking " .. shortPage .. " vs " .. node.name .. ": " .. tostring(shortPage == node.name))
	return shortPage:gsub(" ", ""):gsub("_", "") == node.name:gsub(" ", ""):gsub("_", "")
end
 
local TOC = generateTOCTable()
 
--printNode(TOC)
 
local collapseCaption = "-"
local expandCaption = "+"
 
function Manual.GenerateTOC(frame)    
    local currentPage = frame.args[1]
 
    local ret = ""
    local function scan(node, tab)
        mw.log(IsDescendant(currentPage, node))
        ret = ret .. string.rep(' ',tab)
        if #node.subpages > 0 then
            ret = ret .. '[<span class="manual-menu-button mw-customtoggle-' .. node.name:gsub("/", "") .. '" style="cursor:pointer;">'
            --ret = ret .. (IsDescendant(currentPage:gsub("%s+", ""), node) and collapseCaption or expandCaption)
            ret = ret .. (IsDescendant(currentPage, node) and collapseCaption or expandCaption)
            ret = ret .. '</span>]'
        end
        local len, index = string.find(node.name, "^.*/")
    	if not index then index = 0 end
        ret = ret..("[[%s|%s]]<br/>"):format(node.name,string.sub(node.name, index+1)):gsub("_", " ")
 
        if #node.subpages > 0 then
            --ret = ret .. '<div class = "mw-collapsible ' .. (IsDescendant(currentPage:gsub("%s+", ""), node) and '' or 'mw-collapsed')  .. '" id="mw-customcollapsible-' .. node.name:gsub("%s+", "") .. '">'
            ret = ret .. '<div class = "mw-collapsible ' .. (IsDescendant(currentPage, node) and '' or 'mw-collapsed')  .. '" id="mw-customcollapsible-' .. node.name:gsub("/", "") .. '">'
        end
 
        for _, subnode in pairs(node.subpages) do
            scan(subnode, tab+1)
        end
        if #node.subpages > 0 then
            ret = ret .. '</div>'
        end
    end
 
    scan(TOC.subpages[1],0)
    mw.log(ret)
 
    return ret
end
 
return Manual