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

Option Explicit

Dim s_BodyString As String ' body string
Dim m_Addin As Mm5WebExportAddIn
Dim m_Webex As WebExport
Dim m_TOCHandler As TOCHandler
Dim s_SnippetTemplate As String ' item snippet template
Dim i_StatDepth As Integer  ' maximum depth to display: 1 = central topic only, 2 = main topics only

Sub Main

    Dim m_Page As Mm5WebExportLib.Page
    Dim m_Topic As Topic
    Dim i_PageTopicCount As Integer ' topics on this page

    Set m_Addin = WebExportHelper.WebExportAddIn
    Set m_Webex = m_Addin.ActiveWebExport
    Set m_TOCHandler = New TOCHandler

    On Error GoTo ReportError
    
    m_Webex.Trace("Entering SnipSoData.mmbas", 2 )

    Set m_Page = m_Webex.ActivePage
    i_PageTopicCount = m_Page.TopicCount

    s_SnippetTemplate = m_Webex.ReadFile("SnipSoItem.htm") ' load template
    s_BodyString = ""

    If m_Page.Type <> wePageTypeCallout Then
        m_Webex.ActiveTopic = m_Page.GetTopic(1)
        Set m_Topic = m_Webex.ActiveTopic                                ' currently active topic
        i_StatDepth = m_Webex.GetParameter(TdNavigationDepth) + 1        'read depth to which to create outline
        Call p_StaticOutline(m_Topic.Document.CentralTopic, m_Topic, 1)  ' Generate outline code in s_BodyString
    End If

    ' Write out the result
    m_Webex.PushString(s_BodyString)
 
    m_Webex.Trace( "Exiting SnipSoData.mmbas", 2 )

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

Sub p_StaticOutline(ByVal m_Tree As Topic, ByVal m_focus As Topic, ByVal i_level As Integer)
    ' Construct a static outline for the current topic m_Tree
    Dim i_1 As Integer
    Dim b_Selected As Boolean
    Dim s_FileName As String
    Dim s_FileBookmark As String
    Dim s_TopicText As String ' item name
    Dim s_TopicLink As String ' item HREF
    Dim s_TopicTarget As String ' item target window or frame
    Dim s_ToCNumber As String ' ToC number for topic
    Dim s_TopicClass As String ' topic class name
    Dim s_SelectClass As String ' selected item class name
    Dim s_Snippet As String
    
    Dim pageForTopic As Page

    ' output this topic
    If i_level <= i_StatDepth Then ' within level constraints
        ' Mark this topic
        b_Selected = (m_focus Is m_Tree) ' mark if we are at actual topic
        If ((Not b_Selected) And (i_level = i_StatDepth)) Then b_Selected = f_InOurTree(m_Tree, m_focus)

        ' Set up cleaned name and hyperlink
        
        ' If this is a hyperlink-only topic
        if m_Webex.IsHyperlinkOnlyTopic(m_Tree) Then
        
            ' use the topic's hyperlink
            Dim text As String
            m_Webex.UrlFromHyperlink(m_Tree.Hyperlink, s_TopicLink, text, s_TopicTarget)
        Else
        
            ' Link to the topic or topic page (for first topic on page)
        
            s_TopicTarget = ""
        
            m_Webex.FilenameAndBookmarkFromTopic(m_Tree, s_FileName, s_FileBookmark)
            
            ' If this topic is the first topic on a page, 
            ' use a page link (with no bookmark) instead of a topic link (with a bookmark)
            ' to avoid some "ugly" page scrolling for this export when jumping to the main
            ' topics on pages
            
            ' if we can find the page for this topic
            Set pageForTopic = m_Webex.Pages.FindPage(m_Tree)
            If Not pageForTopic Is Nothing Then
	            ' if this page's first topic is this topic
	            If pageForTopic.GetTopic(1) Is m_Tree Then
	                ' empty out our bookmark to give us a page link
	                s_FileBookmark = ""
    		    End If
		    End If
            
            s_FileName = m_Webex.PathToUrl(s_FileName)
            s_TopicLink = s_FileName
            If Len(s_FileBookmark) > 0 Then
                s_TopicLink = s_TopicLink + "#" + m_Webex.PathToUrl(s_FileBookmark)
            End If
            
        End If
        
        s_ToCNumber = m_TOCHandler.GetOutlineNumberForTopic(m_Tree, _
                            m_Webex.GetParameter("Strings.Show.NumberToLevel"))
                            
        ' Set up page name
        If m_Tree.IsCentralTopic Then
            s_TopicText = m_Webex.GetParameter("Strings.Navigation.Home") ' read user defined home page name
        Else ' another page
            s_TopicText = NonEmptyTopicText(m_Webex, m_Tree)
        End If
        s_TopicText = CleanStringStaticOutline(s_TopicText, m_Webex, True) ' truncate if necessary
        s_TopicText = s_ToCNumber & s_TopicText ' prepend number

        ' Set up classes
        If b_Selected Then ' selected version
            s_SelectClass = "select"
        Else ' unselected version
            s_SelectClass = "unselect"
        End If
        If m_Tree.IsCentralTopic Then
            s_TopicClass = "hometopic"
        ElseIf m_Tree.IsMainTopic Then
            s_TopicClass = "maintopic"
        Else
            s_TopicClass = "subtopic"
        End If
        
        If Len(s_TopicTarget) > 0 Then
            s_TopicTarget = " target=""" + s_TopicTarget + """"
        End If

        ' Build snippet then append to body
        s_Snippet = s_SnippetTemplate ' get copy of snippet template
        s_Snippet = Replace(s_Snippet, "#staticoutline.dataitem.topicclass#", s_TopicClass)
        s_Snippet = Replace(s_Snippet, "#staticoutline.dataitem.selectclass#", s_SelectClass)
        s_Snippet = Replace(s_Snippet, "#staticoutline.dataitem.text#", s_TopicText)
        s_Snippet = Replace(s_Snippet, "#staticoutline.dataitem.link#", s_TopicLink)
        s_Snippet = Replace(s_Snippet, "#staticoutline.dataitem.target#", s_TopicTarget)
        s_BodyString = s_BodyString & s_Snippet

        ' Check if focus is in our tree, to drill down to subtopics
        If    m_Webex.GetParameter("Strings.Miscellaneous.OutlineExpandAll") _
           Or f_InOurTree(m_Tree, m_focus) Then ' focus is in this subtree

            For i_1 = 1 To m_Tree.SubTopics(False).Count
                Call p_StaticOutline(m_Tree.SubTopics(False).Item(i_1), m_focus, i_level + 1)
            Next i_1
        End If

    End If

End Sub

Function f_InOurTree(ByVal m_Tree As Topic, ByVal m_focus As Topic) As Boolean
' Check whether m_Focus is in the tree at or under m_Here

    f_InOurTree = False ' assume not
    While True
        If (m_focus Is m_Tree) Then
            f_InOurTree = True
            Exit While
        End If
        If (Not m_focus.IsCentralTopic) Then
            Set m_focus = m_focus.ParentTopic ' go up
        Else
            Exit While ' no more to do
        End If
    Wend

End Function
