]> git.saurik.com Git - wxWidgets.git/blobdiff - utils/tex2rtf/src/htmlutil.cpp
Fixed accidental removal in one of past editings.
[wxWidgets.git] / utils / tex2rtf / src / htmlutil.cpp
index 550f553a0d5d09b96b3c66602f7a2e52b1bbe1de..b68d0b5b2f73d53ae7ccdc984f4e4cef83f6b49f 100644 (file)
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-#pragma implementation
-#endif
-
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
@@ -29,7 +25,7 @@
 #include "tex2any.h"
 #include "tex2rtf.h"
 #include "table.h"
-
+#include <stdio.h>
 #define HTML_FILENAME_PATTERN _T("%s_%s.html")
 
 #if !WXWIN_COMPATIBILITY_2_4
@@ -39,6 +35,7 @@ static inline wxChar* copystring(const wxChar* s)
 
 extern wxHashTable TexReferences;
 
+extern int passNumber;
 
 extern void DecToHex(int, wxChar *);
 void GenerateHTMLIndexFile(wxChar *fname);
@@ -262,7 +259,7 @@ void ProcessText2HTML(TexChunk *chunk)
   bool changed = false;
   int ptr = 0;
   int i = 0;
-  char ch = 1;
+  wxChar ch = 1;
   int len = wxStrlen(chunk->value);
   while (ch != 0)
   {
@@ -277,19 +274,19 @@ void ProcessText2HTML(TexChunk *chunk)
       i += 2;
       changed = true;
     }
-    else if (!inVerbatim && ch == '`' && (len >= i+1 && chunk->value[i+1] == '`'))
+    else if (!inVerbatim && ch == _T('`') && (len >= i+1 && chunk->value[i+1] == '`'))
     {
       BigBuffer[ptr] = '"'; ptr ++;
       i += 2;
       changed = true;
     }
-    else if (!inVerbatim && ch == '`') // Change ` to '
+    else if (!inVerbatim && ch == _T('`')) // Change ` to '
     {
       BigBuffer[ptr] = 39; ptr ++;
       i += 1;
       changed = true;
     }
-    else if (ch == '<') // Change < to &lt
+    else if (ch == _T('<')) // Change < to &lt
     {
       BigBuffer[ptr] = 0;
       wxStrcat(BigBuffer, _T("&lt;"));
@@ -297,7 +294,7 @@ void ProcessText2HTML(TexChunk *chunk)
       i += 1;
       changed = true;
     }
-    else if (ch == '>') // Change > to &gt
+    else if (ch == _T('>')) // Change > to &gt
     {
       BigBuffer[ptr] = 0;
       wxStrcat(BigBuffer, _T("&gt;"));
@@ -662,7 +659,7 @@ void OutputBodyStart(void)
     if (s)
     {
       TexOutput(_T(" BACKGROUND=\""));
-      TexOutput(s); 
+      TexOutput(s);
       TexOutput(_T("\""));
     }
   }
@@ -905,7 +902,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
           if ( combineSubSections && !subsectionStarted )
           {
             fflush(Sections);
-              
+
             // Read old .con file in at this point
             wxChar buf[256];
             wxStrcpy(buf, CurrentSectionFile);
@@ -1323,7 +1320,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
         TexOutput(_T("\n<TABLE>\n"));
     else {
         TexOutput(_T("\n</TABLE>\n"));
-    // DHS 
+    // DHS
         TwoColWidthA = -1;
         TwoColWidthB = -1;
     }
@@ -1801,7 +1798,74 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
     break;
   }
 }
-
+/*     CheckTypeRef()
+
+       should be called at of argument which usually is
+       type declaration which propably contains name of
+       documented class
+
+       examples:
+               HTMLOnArgument
+                       - ltFUNC,
+                       - ltPARAM
+                       - ltCPARAM
+
+       checks: GetArgData() if contains Type Declaration
+                               and can be referenced to some file
+       prints:
+               before<a href="xxx&yyy">type</a>after
+
+       returns:
+               false   - if no reference was found
+               true    - if reference was found and HREF printed
+*/
+static bool CheckTypeRef()
+{
+  wxString typeDecl = GetArgData();
+  if( !typeDecl.empty() ) {
+    typeDecl.Replace(wxT("\\"),wxT(""));
+    wxString label = typeDecl;
+    label.Replace(wxT("const"),wxT(""));
+    label.Replace(wxT("virtual"),wxT(""));
+    label.Replace(wxT("static"),wxT(""));
+    label.Replace(wxT("extern"),wxT(""));
+    label = label.BeforeFirst('&');
+    label = label.BeforeFirst(wxT('*'));
+    label = label.BeforeFirst(wxT('\\'));
+    label.Trim(true); label.Trim(false);
+    wxString typeName = label;
+    label.MakeLower();
+    TexRef *texRef = FindReference((wxChar*)label.c_str());
+
+    if (texRef && texRef->refFile && wxStrcmp(texRef->refFile, _T("??")) != 0) {
+      int a = typeDecl.Find(typeName);
+      wxString before = typeDecl.Mid( 0, a );
+      wxString after = typeDecl.Mid( a+typeName.Length() );
+      //wxFprintf(stderr,wxT("%s <%s> %s to ... %s#%s !!!!\n"),
+       //      before.c_str(),
+       //      typeName.c_str(),
+       //      after.c_str(),
+       //      texRef->refFile,label.c_str());
+      TexOutput(before);
+      TexOutput(_T("<A HREF=\""));
+      TexOutput(texRef->refFile);
+      TexOutput(_T("#"));
+      TexOutput(label);
+      TexOutput(wxT("\">"));
+      TexOutput(typeName);
+      TexOutput(wxT("</A>"));
+      TexOutput(after);
+      return true;
+    } else {
+      //wxFprintf(stderr,wxT("'%s' from (%s) -> label %s NOT FOUND\n"),
+       //      typeName.c_str(),
+       //      typeDecl.c_str(),
+       //      label.c_str());
+      return false;
+    }
+  }
+  return false;
+}
 // Called on start/end of argument examination
 bool HTMLOnArgument(int macroId, int arg_no, bool start)
 {
@@ -1827,8 +1891,13 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
   }
   case ltFUNC:
   {
-    if (start && (arg_no == 1))
+    if (start && (arg_no == 1)) {
       TexOutput(_T("<B>"));
+      if( CheckTypeRef() ) {
+       TexOutput(_T("</B> "));
+       return false;
+      }
+    }
 
     if (!start && (arg_no == 1))
       TexOutput(_T("</B> "));
@@ -1889,27 +1958,21 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     break;
   }
   case ltPARAM:
-  {
-    if (start && (arg_no == 1))
-      TexOutput(_T("<B>"));
-    if (!start && (arg_no == 1))
-      TexOutput(_T("</B>"));
-    if (start && (arg_no == 2))
-    {
-      TexOutput(_T("<I>"));
-    }
-    if (!start && (arg_no == 2))
-    {
-      TexOutput(_T("</I>"));
-    }
-    break;
-  }
   case ltCPARAM:
   {
-    if (start && (arg_no == 1))
-      TexOutput(_T("<B>"));
-    if (!start && (arg_no == 1))
-      TexOutput(_T("</B> "));  // This is the difference from param - one space!
+    const wxChar* pend = macroId == ltCPARAM ?
+       _T("</B> ") : _T("</B>");
+    if( arg_no == 1) {
+      if( start ) {
+       TexOutput(_T("<B>"));
+       if( CheckTypeRef() ) {
+         TexOutput(pend);
+         return false;
+       }
+      } else {
+       TexOutput(pend);
+      }
+    }
     if (start && (arg_no == 2))
     {
       TexOutput(_T("<I>"));
@@ -1980,6 +2043,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     }
     break;
   }
+
   case ltHELPREF:
   case ltHELPREFN:
   case ltPOPREF:
@@ -2040,9 +2104,15 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
               TraverseChildrenFromChunk(helpRefText);
             if (!ignoreBadRefs)
               TexOutput(_T(" (REF NOT FOUND)"));
-            wxString errBuf;
-            errBuf.Printf(_T("Warning: unresolved reference '%s'"), refName);
-            OnInform((wxChar *)errBuf.c_str());
+
+            // for launching twice do not warn in preparation pass
+            if ((passNumber == 1 && !runTwice) ||
+                (passNumber == 2 && runTwice))
+            {
+              wxString errBuf;
+              errBuf.Printf(_T("Warning: unresolved reference '%s'"), refName);
+              OnInform((wxChar *)errBuf.c_str());
+            }
           }
         }
         else TexOutput(_T("??"));
@@ -2114,7 +2184,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
             TexOutput(_T("<img src=\""));
             TexOutput(ConvertCase(wxFileNameFromPath(inlineFilename)));
             TexOutput(_T("\""));
-            TexOutput(alignment); 
+            TexOutput(alignment);
             TexOutput(_T("></A>"));
           }
           else
@@ -2123,7 +2193,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
             TexOutput(_T("<img src=\""));
             TexOutput(ConvertCase(wxFileNameFromPath(inlineFilename)));
             TexOutput(_T("\""));
-            TexOutput(alignment); 
+            TexOutput(alignment);
             TexOutput(_T(">"));
             delete[] inlineFilename;
           }
@@ -2173,7 +2243,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       if (imageFile)
         delete[] imageFile;
       imageFile = NULL;
-      if (!f.IsEmpty())
+      if (!f.empty())
       {
         imageFile = copystring(f);
       }
@@ -2268,7 +2338,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
           wxSnprintf(buf, sizeof(buf), _T("\n<TD VALIGN=TOP WIDTH=%d>\n"),TwoColWidthB);
           TexOutput(buf);
         }
-        else 
+        else
         {
           TexOutput(_T("\n<TD VALIGN=TOP>\n"));
         }
@@ -2957,223 +3027,222 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
 
 bool HTMLGo(void)
 {
-  fileId = 0;
-  inVerbatim = false;
-  indentLevel = 0;
-  inTabular = false;
-  startRows = false;
-  tableVerticalLineLeft = false;
-  tableVerticalLineRight = false;
-  noColumns = 0;
-
-  if (InputFile && OutputFile)
-  {
-    // Do some HTML-specific transformations on all the strings,
-    // recursively
-    Text2HTML(GetTopLevelChunk());
-
-    wxChar buf[300];
-    if (truncateFilenames)
-      wxSnprintf(buf, sizeof(buf), _T("%s.htm"), FileRoot);
-    else
-      wxSnprintf(buf, sizeof(buf), _T("%s_contents.html"), FileRoot);
-    if (TitlepageName) delete[] TitlepageName;
-    TitlepageName = copystring(buf);
-    Titlepage = wxFopen(buf, _T("w"));
+    fileId = 0;
+    inVerbatim = false;
+    indentLevel = 0;
+    inTabular = false;
+    startRows = false;
+    tableVerticalLineLeft = false;
+    tableVerticalLineRight = false;
+    noColumns = 0;
 
-    if (truncateFilenames)
-      wxSnprintf(buf, sizeof(buf), _T("%s_fc.htm"), FileRoot);
-    else
-      wxSnprintf(buf, sizeof(buf), _T("%s_fcontents.html"), FileRoot);
+    if (!InputFile.empty() && !OutputFile.empty())
+    {
+        // Do some HTML-specific transformations on all the strings,
+        // recursively
+        Text2HTML(GetTopLevelChunk());
 
-    contentsFrameName = copystring(buf);
+        wxChar buf[300];
+        if (truncateFilenames)
+            wxSnprintf(buf, sizeof(buf), _T("%s.htm"), FileRoot);
+        else
+            wxSnprintf(buf, sizeof(buf), _T("%s_contents.html"), FileRoot);
+        if (TitlepageName) delete[] TitlepageName;
+        TitlepageName = copystring(buf);
+        Titlepage = wxFopen(buf, _T("w"));
 
-    Contents = wxFopen(TmpContentsName, _T("w"));
+        if (truncateFilenames)
+            wxSnprintf(buf, sizeof(buf), _T("%s_fc.htm"), FileRoot);
+        else
+            wxSnprintf(buf, sizeof(buf), _T("%s_fcontents.html"), FileRoot);
 
-    if (htmlFrameContents)
-    {
-//      FrameContents = wxFopen(TmpFrameContentsName, _T("w"));
-      FrameContents = wxFopen(contentsFrameName, _T("w"));
-      wxFprintf(FrameContents, _T("<HTML>\n<UL>\n"));
-    }
+        contentsFrameName = copystring(buf);
 
-    if (!Titlepage || !Contents)
-    {
-      OnError(_T("Cannot open output file!"));
-      return false;
-    }
-    AddTexRef(_T("contents"), wxFileNameFromPath(TitlepageName), ContentsNameString);
+        Contents = wxFopen(TmpContentsName, _T("w"));
 
-    wxFprintf(Contents, _T("<P><P><H2>%s</H2><P><P>\n"), ContentsNameString);
+        if (htmlFrameContents)
+        {
+//          FrameContents = wxFopen(TmpFrameContentsName, _T("w"));
+            FrameContents = wxFopen(contentsFrameName, _T("w"));
+            wxFprintf(FrameContents, _T("<HTML>\n<UL>\n"));
+        }
 
-    wxFprintf(Contents, _T("<UL>\n"));
+        if (!Titlepage || !Contents)
+        {
+            OnError(_T("Cannot open output file!"));
+            return false;
+        }
+        AddTexRef(_T("contents"), wxFileNameFromPath(TitlepageName), ContentsNameString);
 
-    SetCurrentOutput(Titlepage);
-    if (htmlWorkshopFiles) HTMLWorkshopStartContents();
-    OnInform(_T("Converting..."));
+        wxFprintf(Contents, _T("<P><P><H2>%s</H2><P><P>\n"), ContentsNameString);
 
-    TraverseDocument();
-    wxFprintf(Contents, _T("</UL>\n\n"));
+        wxFprintf(Contents, _T("<UL>\n"));
 
-//    SetCurrentOutput(Titlepage);
-    fclose(Titlepage);
+        SetCurrentOutput(Titlepage);
+        if (htmlWorkshopFiles) HTMLWorkshopStartContents();
+        OnInform(_T("Converting..."));
 
-    if (Contents)
-    {
-//      wxFprintf(Titlepage, _T("\n</BODY></HTML>\n"));
-      fclose(Contents);
-      Contents = NULL;
-    }
+        TraverseDocument();
+        wxFprintf(Contents, _T("</UL>\n\n"));
 
-    if (FrameContents)
-    {
-      wxFprintf(FrameContents, _T("\n</UL>\n"));
-      wxFprintf(FrameContents, _T("</HTML>\n"));
-      fclose(FrameContents);
-      FrameContents = NULL;
-    }
+//        SetCurrentOutput(Titlepage);
+        fclose(Titlepage);
 
-    if (Chapters)
-    {
-      wxFprintf(Chapters, _T("\n</FONT></BODY></HTML>\n"));
-      fclose(Chapters);
-      Chapters = NULL;
-    }
-    if (Sections)
-    {
-      wxFprintf(Sections, _T("\n</FONT></BODY></HTML>\n"));
-      fclose(Sections);
-      Sections = NULL;
-    }
-    if (Subsections && !combineSubSections)
-    {
-      wxFprintf(Subsections, _T("\n</FONT></BODY></HTML>\n"));
-      fclose(Subsections);
-      Subsections = NULL;
-    }
-    if (Subsubsections && !combineSubSections)
-    {
-      wxFprintf(Subsubsections, _T("\n</FONT></BODY></HTML>\n"));
-      fclose(Subsubsections);
-      Subsubsections = NULL;
-    }
-    if ( SectionContentsFD )
-    {
-        fclose(SectionContentsFD);
-        SectionContentsFD = NULL;
-    }
+        if (Contents)
+        {
+//            wxFprintf(Titlepage, _T("\n</BODY></HTML>\n"));
+            fclose(Contents);
+            Contents = NULL;
+        }
 
-    // Create a temporary file for the title page header, add some info,
-    // and concat the titlepage just generated.
-    // This is necessary in order to put the title of the document
-    // at the TOP of the file within <HEAD>, even though we only find out
-    // what it is later on.
-    FILE *tmpTitle = wxFopen(_T("title.tmp"), _T("w"));
-    if (tmpTitle)
-    {
-      if (DocumentTitle)
-      {
-        SetCurrentOutput(tmpTitle);
-        HTMLHead();
-        TexOutput(_T("\n<TITLE>"));
-        TraverseChildrenFromChunk(DocumentTitle);
-        TexOutput(_T("</TITLE></HEAD>\n"));
-      }
-      else
-      {
-        SetCurrentOutput(tmpTitle);
-        HTMLHeadTo(tmpTitle);
-        if (contentsString)
-          wxFprintf(tmpTitle, _T("<TITLE>%s</TITLE></HEAD>\n\n"), contentsString);
-        else
-          wxFprintf(tmpTitle, _T("<TITLE>%s</TITLE></HEAD>\n\n"), wxFileNameFromPath(FileRoot));
-      }
+        if (FrameContents)
+        {
+            wxFprintf(FrameContents, _T("\n</UL>\n"));
+            wxFprintf(FrameContents, _T("</HTML>\n"));
+            fclose(FrameContents);
+            FrameContents = NULL;
+        }
 
-      // Output frame information
-      if (htmlFrameContents)
-      {
-        wxChar firstFileName[300];
-        if (truncateFilenames)
-          wxSnprintf(firstFileName, sizeof(firstFileName), _T("%s1.htm"), FileRoot);
-        else
-          wxStrcpy(firstFileName, gs_filenames[1].c_str());
+        if (Chapters)
+        {
+            wxFprintf(Chapters, _T("\n</FONT></BODY></HTML>\n"));
+            fclose(Chapters);
+            Chapters = NULL;
+        }
+        if (Sections)
+        {
+            wxFprintf(Sections, _T("\n</FONT></BODY></HTML>\n"));
+            fclose(Sections);
+            Sections = NULL;
+        }
+        if (Subsections && !combineSubSections)
+        {
+            wxFprintf(Subsections, _T("\n</FONT></BODY></HTML>\n"));
+            fclose(Subsections);
+            Subsections = NULL;
+        }
+        if (Subsubsections && !combineSubSections)
+        {
+            wxFprintf(Subsubsections, _T("\n</FONT></BODY></HTML>\n"));
+            fclose(Subsubsections);
+            Subsubsections = NULL;
+        }
+        if ( SectionContentsFD )
+        {
+            fclose(SectionContentsFD);
+            SectionContentsFD = NULL;
+        }
 
-        wxFprintf(tmpTitle, _T("<FRAMESET COLS=\"30%%,70%%\">\n"));
+        // Create a temporary file for the title page header, add some info,
+        // and concat the titlepage just generated.
+        // This is necessary in order to put the title of the document
+        // at the TOP of the file within <HEAD>, even though we only find out
+        // what it is later on.
+        FILE *tmpTitle = wxFopen(_T("title.tmp"), _T("w"));
+        if (tmpTitle)
+        {
+            if (DocumentTitle)
+            {
+                SetCurrentOutput(tmpTitle);
+                HTMLHead();
+                TexOutput(_T("\n<TITLE>"));
+                TraverseChildrenFromChunk(DocumentTitle);
+                TexOutput(_T("</TITLE></HEAD>\n"));
+            }
+            else
+            {
+                SetCurrentOutput(tmpTitle);
+                HTMLHeadTo(tmpTitle);
+                if (contentsString)
+                    wxFprintf(tmpTitle, _T("<TITLE>%s</TITLE></HEAD>\n\n"), contentsString);
+                else
+                    wxFprintf(tmpTitle, _T("<TITLE>%s</TITLE></HEAD>\n\n"), wxFileNameFromPath(FileRoot));
+            }
 
-        wxFprintf(tmpTitle, _T("<FRAME SRC=\"%s\">\n"), ConvertCase(wxFileNameFromPath(contentsFrameName)));
-        wxFprintf(tmpTitle, _T("<FRAME SRC=\"%s\" NAME=\"mainwindow\">\n"), ConvertCase(wxFileNameFromPath(firstFileName)));
-        wxFprintf(tmpTitle, _T("</FRAMESET>\n"));
+            // Output frame information
+            if (htmlFrameContents)
+            {
+                wxChar firstFileName[300];
+                if (truncateFilenames)
+                    wxSnprintf(firstFileName, sizeof(firstFileName), _T("%s1.htm"), FileRoot);
+                else
+                    wxStrcpy(firstFileName, gs_filenames[1].c_str());
 
-        wxFprintf(tmpTitle, _T("<NOFRAMES>\n"));
-      }
+                wxFprintf(tmpTitle, _T("<FRAMESET COLS=\"30%%,70%%\">\n"));
 
-      // Output <BODY...> to temporary title page
-      OutputBodyStart();
-      fflush(tmpTitle);
+                wxFprintf(tmpTitle, _T("<FRAME SRC=\"%s\">\n"), ConvertCase(wxFileNameFromPath(contentsFrameName)));
+                wxFprintf(tmpTitle, _T("<FRAME SRC=\"%s\" NAME=\"mainwindow\">\n"), ConvertCase(wxFileNameFromPath(firstFileName)));
+                wxFprintf(tmpTitle, _T("</FRAMESET>\n"));
 
-      // Concat titlepage
-      FILE *fd = wxFopen(TitlepageName, _T("r"));
-      if (fd)
-      {
-        int ch = getc(fd);
-        while (ch != EOF)
-        {
-          wxPutc(ch, tmpTitle);
-          ch = getc(fd);
-        }
-        fclose(fd);
-      }
+                wxFprintf(tmpTitle, _T("<NOFRAMES>\n"));
+            }
 
-      wxFprintf(tmpTitle, _T("\n</FONT></BODY>\n"));
+            // Output <BODY...> to temporary title page
+            OutputBodyStart();
+            fflush(tmpTitle);
 
-      if (htmlFrameContents)
-      {
-        wxFprintf(tmpTitle, _T("\n</NOFRAMES>\n"));
-      }
-      wxFprintf(tmpTitle, _T("\n</HTML>\n"));
+            // Concat titlepage
+            FILE *fd = wxFopen(TitlepageName, _T("r"));
+            if (fd)
+            {
+                int ch = getc(fd);
+                while (ch != EOF)
+                {
+                    wxPutc(ch, tmpTitle);
+                    ch = getc(fd);
+                }
+                fclose(fd);
+            }
 
-      fclose(tmpTitle);
-      if (wxFileExists(TitlepageName)) wxRemoveFile(TitlepageName);
-      if (!wxRenameFile(_T("title.tmp"), TitlepageName))
-      {
-        wxCopyFile(_T("title.tmp"), TitlepageName);
-        wxRemoveFile(_T("title.tmp"));
-      }
-    }
+            wxFprintf(tmpTitle, _T("\n</FONT></BODY>\n"));
 
-    if (lastFileName) delete[] lastFileName;
-    lastFileName = NULL;
-    if (lastTopic) delete[] lastTopic;
-    lastTopic = NULL;
+            if (htmlFrameContents)
+            {
+                wxFprintf(tmpTitle, _T("\n</NOFRAMES>\n"));
+            }
+            wxFprintf(tmpTitle, _T("\n</HTML>\n"));
 
-    if (wxFileExists(ContentsName)) wxRemoveFile(ContentsName);
+            fclose(tmpTitle);
+            if (wxFileExists(TitlepageName)) wxRemoveFile(TitlepageName);
+            if (!wxRenameFile(_T("title.tmp"), TitlepageName))
+            {
+                wxCopyFile(_T("title.tmp"), TitlepageName);
+                wxRemoveFile(_T("title.tmp"));
+            }
+        }
 
-    if (!wxRenameFile(TmpContentsName, ContentsName))
-    {
-      wxCopyFile(TmpContentsName, ContentsName);
-      wxRemoveFile(TmpContentsName);
-    }
+        if (lastFileName) delete[] lastFileName;
+        lastFileName = NULL;
+        if (lastTopic) delete[] lastTopic;
+        lastTopic = NULL;
 
-    // Generate .htx file if requested
-    if (htmlIndex)
-    {
-      wxChar htmlIndexName[300];
-      wxSnprintf(htmlIndexName, sizeof(htmlIndexName), _T("%s.htx"), FileRoot);
-      GenerateHTMLIndexFile(htmlIndexName);
-    }
+        if (wxFileExists(ContentsName)) wxRemoveFile(ContentsName);
 
-    // Generate HTML Help Workshop files if requested
-    if (htmlWorkshopFiles)
-    {
-      HTMLWorkshopEndContents();
-      GenerateHTMLWorkshopFiles(FileRoot);
-    }
+        if (!wxRenameFile(TmpContentsName, ContentsName))
+        {
+            wxCopyFile(TmpContentsName, ContentsName);
+            wxRemoveFile(TmpContentsName);
+        }
+
+        // Generate .htx file if requested
+        if (htmlIndex)
+        {
+            wxChar htmlIndexName[300];
+            wxSnprintf(htmlIndexName, sizeof(htmlIndexName), _T("%s.htx"), FileRoot);
+            GenerateHTMLIndexFile(htmlIndexName);
+        }
 
+        // Generate HTML Help Workshop files if requested
+        if (htmlWorkshopFiles)
+        {
+            HTMLWorkshopEndContents();
+            GenerateHTMLWorkshopFiles(FileRoot);
+        }
 
-    return true;
-  }
+        return true;
+    }
 
-  return false;
+    return false;
 }
 
 // Output .htx index file
@@ -3383,4 +3452,4 @@ bool PrimaryAnchorOfTheFile( wxChar *file, wxChar *label )
     wxString file_label;
     file_label.Printf( HTML_FILENAME_PATTERN, FileRoot, label );
     return file_label.IsSameAs( file , false );
-}
\ No newline at end of file
+}