'#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 "ParameterNames.mmbas"
'#Uses "GlobalParameterNames.mmbas"
'#Uses "GlobalUtilities.mmbas"

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 oneTOCHandler As TOCHandler
   Dim oneImage As Image
   Dim oneTopic As Topic
   Dim oneIconCollection As Icons
   Dim oneIcon As Icon

   Dim count As Long
   Dim firstTopic As Long
   Dim onePageExportTopic As Long
   Dim i As Long
   Dim template As String
   Dim topicString As String
   Dim bodyString As String
   Dim thefilename As String
   Dim thebookmark As String
   Dim TheTOCNumber As String
   Dim TheTopicText As String
   Dim fileName As String
   Dim fileBookmark As String

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

   template = webex.ReadFile("BodyItem.htm")

   ' Get our list of topics for this page
   Set oneTOCHandler = New TOCHandler
   Set aPage = webex.ActivePage

   count = aPage.TopicCount
   firstTopic = 1				' export topics from 'firstTopic' to 'count'

   If count > 0 Then

		' special handling for OnePage export: on first pass only export central topic
		' and on second pass export all other topics. Default is export all topics
		If aPage.Type = wePageTypeCallout Then
			' no special handling for callout topics and callout pages
			onePageExportTopic = 0
		Else
			onePageExportTopic = webex.GetParameter(TsOnePageExportTopic)

	    	If onePageExportTopic = 1 Then
	    		count = 1							' export central topic only
	    		webex.SetParameter(TsOnePageExportTopic, 2 ) ' next step
	    	ElseIf onePageExportTopic = 2 Then
        		firstTopic = 2					' then skip central topic and start with first main topic
	    		webex.SetParameter(TsOnePageExportTopic, 0 ) ' back to default for callout pages
	    	End If
	    End If

        ' For each topic
        For i = firstTopic To count

            webex.ActiveTopic = aPage.GetTopic(i)
            Set oneTopic = webex.ActiveTopic

            ' Store icons used in a variant list (to determine what items to include in the icon legend)
            Set oneIconCollection = oneTopic.Icons(True)
            If oneIconCollection.Count > 0 Then
                For Each oneIcon In oneIconCollection
                    If Not webex.IsSuccessful Then
                        Exit For
                    End If
                    webex.VariantSets(weVariantSetIcons).Add(oneIcon.Name)
                Next
            End If

            ' Get TOC number if enabled
            webex.SetParameter("CustomParameter.TOCOutlineNumberToWrite", _
                oneTOCHandler.GetOutlineNumberForTopic(oneTopic, webex.GetParameter("Strings.Show.NumberToLevel")))

            ' Read our template
            topicString = template

            ' Use different topic format for first topic on page and all others
            If i = 1 Then
                topicString = Replace(topicString, "#OutlineNumber#", "outlineNumberFirst")
                topicString = Replace(topicString, "#TopicLine#", "topicLineFirst")
            ElseIf (onePageExportTopic = 2) _
                    And oneTopic.IsMainTopic  Then			' one page export for main topics
                topicString = Replace(topicString, "#OutlineNumber#", "outlineNumberSecond")
                topicString = Replace(topicString, "#TopicLine#", "topicLineSecond")
            Else
                topicString = Replace(topicString, "#OutlineNumber#", "outlineNumber")
                topicString = Replace(topicString, "#TopicLine#", "topicLine")
            End If

            TheTopicText = webex.GetParameter(WeCurrentTopicText)

            webex.SetParameter("CustomParameter.CurrentTopicText", VerifyText(webex, TheTopicText))
            webex.FilenameAndBookmarkFromTopic(oneTopic, fileName, fileBookmark)
            webex.SetParameter("CustomParameter.TopicBookmark",  fileBookmark)

            ' Process all commands and parameters
            ProcessString webex, topicString
   
            If Not webex.ActiveTopic.Notes.IsEmpty Then
                ExportNotes webex, topicString
            Else
                topicString = Replace(topicString, "#Notes#", "")
            End If

            bodyString = bodyString + topicString
        Next i

    End If

    ' Write out the result
    webex.PushString(bodyString)
 
    webex.Trace( "Exiting BodyItem.mmbas", 2 )

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

Sub ExportNotes(webex As WebExport, topicString As String)

    Dim notesXhtml As String

    notesXhtml = GlobalExportNotes(webex, webex.ActiveTopic)

    topicString = Replace(topicString, "#Notes#", notesXhtml)
    
End Sub

