'#Reference {A3B5C45B-F842-44fc-A5E5-50CED69BA86E}#2.0#0#C:\Program Files\Mindjet\MindManager 6\Mm5WebExport.dll#Mm5webexport 2.0 Type Library
'#Uses "TOCHandler.cls"
'#Uses "GlobalUtilities.mmbas"
'#Uses "GlobalTypes.mmbas"

Dim CurrentBranchLevel As Long
Dim LevelCounter As Long
Dim TheOutlineNumber As String
Dim MainTopicCounter As Long
Dim LastLevel As Long

Dim s As Long
Dim CurrentOutlineNumber As String
Dim NumberOfLevelsToJumpDown As Long

Option Explicit

Sub Main

    Dim addin As Mm5WebExportAddIn
    Dim webex As WebExport
    Dim aPage As Mm5WebExportLib.Page

    Set addin = WebExportHelper.WebExportAddIn
    Set webex = addin.ActiveWebExport

    Dim oneDoc As Document
    Dim oneTopic As Topic

    Dim count As Long
    Dim bodyString As String

    On Error GoTo ReportError
        
    webex.Trace("Entering ToolsCntOutItem.mmbas",2)

    ' Outline level counter initialization
    LastLevel = 0
    MainTopicCounter = 0

   ' Get the central topic object
   Set oneTopic = webex.ActiveTopic
   Set oneDoc = oneTopic.Document
   Set oneTopic = oneDoc.CentralTopic

   webex.Trace("Page_Tools_Utilities_Contents_OutlineItem - Starting recursive call",1)

   ' Recursive map traversal
   Call MapTraversal(oneTopic, bodyString, webex)

   ' Store our result
   webex.PushString(bodyString)

   webex.Trace("Exiting ToolsCntOutItem.mmbas",2)

   Exit Sub
 
ReportError:
    ProcessError(webex)
    Exit Sub
 
End Sub

Function MapTraversal(ByVal oneTopic As Topic, ByRef bodyString As String, ByRef webex As WebExport)

    Dim oneTOCHandler As TOCHandler
    Dim nextTopic As Topic
    Dim TopicCount As Integer
    Dim LocalTemplate As String
    Dim LocalTopicString As String
    Dim k As Long
    Dim l As Long
    Dim TheTOCNumber As String
    Dim TopicText As String
    Dim TopicLink As HyperlinkData

    Set oneTOCHandler = New TOCHandler

    ' Keep track of "recurse level"
    LevelCounter = LevelCounter + 1

    TopicCount = oneTopic.SubTopics(False).Count

    For k = 1 To TopicCount

        Set nextTopic = oneTopic.SubTopics(False).Item(k)

        TheTOCNumber = oneTOCHandler.GetOutlineNumberForTopic(nextTopic, _
                          webex.GetParameter("Strings.Show.NumberToLevel"))

        TopicLink = TopicHyperlink(webex, nextTopic)

        webex.SetParameter("CustomParameter.TopicHyperlink", TopicLink.Url)
        webex.SetParameter("CustomParameter.TopicText", VerifyTopicText(webex, nextTopic))
        webex.SetParameter("CustomParameter.TopicTarget", TargetAttribute(TopicLink))

        ' Enforce maximum "TOC level"
        'If LevelCounter > webex.GetParameter("Strings.Miscellaneous.TableOfContentsOutlineNumbers") Then
        '     webex.SetParameter("CustomParameter.TOCOutlineNumberToWrite", "")
        '  Else
            webex.SetParameter("CustomParameter.TOCOutlineNumberToWrite", TheTOCNumber)
        'End If

        ' Enforce maximum "indentation levels"
        If LevelCounter > 9 Then
            webex.SetParameter("CustomParameter.TOCOutlineLevel", "x")
        Else
            webex.SetParameter("CustomParameter.TOCOutlineLevel", LevelCounter)
        End If

        LocalTemplate = webex.ReadFile("ToolsCntOutItem.htm")
        LocalTopicString = LocalTemplate

        ' Process our template and replace parameters, then store new bodystring
        ProcessString webex, LocalTopicString
        bodyString = bodyString + LocalTopicString

        ' Recursive call
        Call MapTraversal(nextTopic, bodyString, webex)

    Next k

    ' Close open levels as we exit recursive loop
    'bodyString = bodyString + "</div>" + vbCrLf

    ' Keep track of recurse level
    LevelCounter = LevelCounter - 1

End Function
