]> git.saurik.com Git - wxWidgets.git/blobdiff - utils/tex2rtf/src/htmlutil.cpp
Missing includes.
[wxWidgets.git] / utils / tex2rtf / src / htmlutil.cpp
index a286f46279f422c4349e4f12ca32d0358d21c7fe..ec8fdfb658ddc0d8860c08b6391bc45f65b5f2ed 100644 (file)
@@ -2,7 +2,8 @@
 // Name:        htmlutil.cpp
 // Purpose:     Converts Latex to HTML
 // Author:      Julian Smart
-// Modified by:
+// Modified by: Wlodzimierz ABX Skiba 2003/2004 Unicode support
+//              Ron Lee
 // Created:     7.9.93
 // RCS-ID:      $Id$
 // Copyright:   (c) Julian Smart
 #ifndef WX_PRECOMP
 #endif
 
+#include "wx/arrstr.h"
+
 #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
 static inline wxChar* copystring(const wxChar* s)
@@ -38,6 +43,8 @@ extern wxHashTable TexReferences;
 extern void DecToHex(int, wxChar *);
 void GenerateHTMLIndexFile(wxChar *fname);
 
+bool PrimaryAnchorOfTheFile( wxChar *file, wxChar *label );
+
 void GenerateHTMLWorkshopFiles(wxChar *fname);
 void HTMLWorkshopAddToContents(int level, wxChar *s, wxChar *file);
 void HTMLWorkshopStartContents();
@@ -67,18 +74,18 @@ FILE *FrameContents = NULL;
 FILE *Titlepage = NULL;
 // FILE *FrameTitlepage = NULL;
 int fileId = 0;
-bool subsectionStarted = FALSE;
+bool subsectionStarted = false;
 
 // Which column of a row are we in? (Assumes no nested tables, of course)
 int currentColumn = 0;
 
 // Are we in verbatim mode? If so, format differently.
-static bool inVerbatim = FALSE;
+static bool inVerbatim = false;
 
 // Need to know whether we're in a table or figure for benefit
 // of listoffigures/listoftables
-static bool inFigure = FALSE;
-static bool inTable = FALSE;
+static bool inFigure = false;
+static bool inTable = false;
 
 // This is defined in the Tex2Any library.
 extern wxChar *BigBuffer;
@@ -175,12 +182,17 @@ void SetCurrentSubsubsectionName(wxChar *s, wxChar *file)
   SetCurrentTopic(s);
 }
 
+
+// mapping between fileId and filenames if truncateFilenames=false:
+static wxArrayString gs_filenames;
+
+
 /*
  * Close former filedescriptor and reopen using another filename.
  *
  */
 
-void ReopenFile(FILE **fd, wxChar **fileName)
+void ReopenFile(FILE **fd, wxChar **fileName, const wxChar *label)
 {
   if (*fd)
   {
@@ -190,9 +202,16 @@ void ReopenFile(FILE **fd, wxChar **fileName)
   fileId ++;
   wxChar buf[400];
   if (truncateFilenames)
-    wxSprintf(buf, _T("%s%d.htm"), FileRoot, fileId);
+  {
+    wxSnprintf(buf, sizeof(buf), _T("%s%d.htm"), FileRoot, fileId);
+  }
   else
-    wxSprintf(buf, _T("%s%d.html"), FileRoot, fileId);
+  {
+    if (fileId == 1)
+      gs_filenames.Add(wxEmptyString);
+    wxSnprintf(buf, sizeof(buf), HTML_FILENAME_PATTERN, FileRoot, label);
+    gs_filenames.Add(buf);
+  }
   if (*fileName) delete[] *fileName;
   *fileName = copystring(wxFileNameFromPath(buf));
   *fd = wxFopen(buf, _T("w"));
@@ -240,10 +259,10 @@ void ReopenSectionContentsFile(void)
 
 void ProcessText2HTML(TexChunk *chunk)
 {
-  bool changed = FALSE;
+  bool changed = false;
   int ptr = 0;
   int i = 0;
-  char ch = 1;
+  wxChar ch = 1;
   int len = wxStrlen(chunk->value);
   while (ch != 0)
   {
@@ -256,35 +275,35 @@ void ProcessText2HTML(TexChunk *chunk)
     {
       BigBuffer[ptr] = 0; wxStrcat(BigBuffer, _T("<P>\n\n")); ptr += 5;
       i += 2;
-      changed = TRUE;
+      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;
+      changed = true;
     }
-    else if (!inVerbatim && ch == '`') // Change ` to '
+    else if (!inVerbatim && ch == _T('`')) // Change ` to '
     {
       BigBuffer[ptr] = 39; ptr ++;
       i += 1;
-      changed = TRUE;
+      changed = true;
     }
-    else if (ch == '<') // Change < to &lt
+    else if (ch == _T('<')) // Change < to &lt
     {
       BigBuffer[ptr] = 0;
       wxStrcat(BigBuffer, _T("&lt;"));
       ptr += 4;
       i += 1;
-      changed = TRUE;
+      changed = true;
     }
-    else if (ch == '>') // Change > to &gt
+    else if (ch == _T('>')) // Change > to &gt
     {
       BigBuffer[ptr] = 0;
       wxStrcat(BigBuffer, _T("&gt;"));
       ptr += 4;
       i += 1;
-      changed = TRUE;
+      changed = true;
     }
     else
     {
@@ -325,7 +344,7 @@ void Text2HTML(TexChunk *chunk)
         return;
 
       if (def && (def->macroId == ltVERBATIM || def->macroId == ltVERB || def->macroId == ltSPECIAL))
-        inVerbatim = TRUE;
+        inVerbatim = true;
 
       wxNode *node = chunk->children.GetFirst();
       while (node)
@@ -336,7 +355,7 @@ void Text2HTML(TexChunk *chunk)
       }
 
       if (def && (def->macroId == ltVERBATIM || def->macroId == ltVERB || def->macroId == ltSPECIAL))
-        inVerbatim = FALSE;
+        inVerbatim = false;
 
       break;
     }
@@ -384,7 +403,9 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
   {
 //    contentsReference = "<img align=center src=\"contents.gif\" BORDER=0 ALT=\"Contents\">";
     contentsReference = contentsReferenceBuf;
-    wxSprintf(contentsReference, _T("<img align=center src=\"%s\" BORDER=0 ALT=\"Contents\">"), ConvertCase(_T("contents.gif")));
+    wxSnprintf(contentsReference, sizeof(contentsReferenceBuf),
+               _T("<img align=center src=\"%s\" BORDER=0 ALT=\"Contents\">"),
+               ConvertCase(_T("contents.gif")));
   }
 
   wxChar *upReference; // no need to initialize because always assigned below
@@ -394,7 +415,9 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
   {
 //    upReference = "<img align=center src=\"up.gif\" ALT=\"Up\">";
     upReference = upReferenceBuf;
-    wxSprintf(upReference, _T("<img align=center src=\"%s\" BORDER=0 ALT=\"Up\">"), ConvertCase(_T("up.gif")));
+    wxSnprintf(upReference, sizeof(upReferenceBuf),
+               _T("<img align=center src=\"%s\" BORDER=0 ALT=\"Up\">"),
+               ConvertCase(_T("up.gif")));
   }
 
   wxChar *backReference; // no need to initialize because always assigned below
@@ -404,7 +427,9 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
   {
 //    backReference = "<img align=center src=\"back.gif\" ALT=\"Previous\">";
     backReference = backReferenceBuf;
-    wxSprintf(backReference, _T("<img align=center src=\"%s\" BORDER=0 ALT=\"Previous\">"), ConvertCase(_T("back.gif")));
+    wxSnprintf(backReference, sizeof(backReferenceBuf),
+               _T("<img align=center src=\"%s\" BORDER=0 ALT=\"Previous\">"),
+               ConvertCase(_T("back.gif")));
   }
 
   wxChar *forwardReference; // no need to initialize because always assigned below
@@ -414,7 +439,9 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
   {
 //    forwardReference = "<img align=center src=\"forward.gif\" ALT=\"Next\">";
     forwardReference = forwardReferenceBuf;
-    wxSprintf(forwardReference, _T("<img align=center src=\"%s\" BORDER=0 ALT=\"Next\">"), ConvertCase(_T("forward.gif")));
+    wxSnprintf(forwardReference, sizeof(forwardReferenceBuf),
+               _T("<img align=center src=\"%s\" BORDER=0 ALT=\"Next\">"),
+               ConvertCase(_T("forward.gif")));
   }
 
   TexOutput(_T("<CENTER>"));
@@ -430,13 +457,17 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
   {
     wxChar buf1[80];
     wxStrcpy(buf1, ConvertCase(wxFileNameFromPath(FileRoot)));
-    wxSprintf(buf, _T("\n<A HREF=\"%s.%s\">%s</A> "), buf1, ConvertCase(_T("htm")), contentsReference);
+    wxSnprintf(buf, sizeof(buf),
+               _T("\n<A HREF=\"%s.%s\">%s</A> "),
+               buf1, ConvertCase(_T("htm")), contentsReference);
   }
   else
   {
     wxChar buf1[80];
     wxStrcpy(buf1, ConvertCase(wxFileNameFromPath(FileRoot)));
-    wxSprintf(buf, _T("\n<A HREF=\"%s%s\">%s</A> "), buf1, ConvertCase(_T("_contents.html")), contentsReference);
+    wxSnprintf(buf, sizeof(buf),
+               _T("\n<A HREF=\"%s%s\">%s</A> "),
+               buf1, ConvertCase(_T("_contents.html")), contentsReference);
   }
 //  TexOutput(_T("<NOFRAMES>"));
   TexOutput(buf);
@@ -449,10 +480,14 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
 
   if (upLabel && upFilename)
   {
-    if (wxStrlen(upLabel) > 0)
-      wxSprintf(buf, _T("<A HREF=\"%s#%s\">%s</A> "), ConvertCase(upFilename), upLabel, upReference);
+    if ( (wxStrlen(upLabel) > 0) && !PrimaryAnchorOfTheFile(upFilename, upLabel) )
+      wxSnprintf(buf, sizeof(buf),
+                 _T("<A HREF=\"%s#%s\">%s</A> "),
+                 ConvertCase(upFilename), upLabel, upReference);
     else
-      wxSprintf(buf, _T("<A HREF=\"%s\">%s</A> "), ConvertCase(upFilename), upReference);
+      wxSnprintf(buf, sizeof(buf),
+                 _T("<A HREF=\"%s\">%s</A> "),
+                 ConvertCase(upFilename), upReference);
     if (wxStrcmp(upLabel, _T("contents")) == 0)
     {
 //      TexOutput(_T("<NOFRAMES>"));
@@ -470,7 +505,14 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
 
   if (previousLabel && previousFilename)
   {
-    wxSprintf(buf, _T("<A HREF=\"%s#%s\">%s</A> "), ConvertCase(previousFilename), previousLabel, backReference);
+    if (PrimaryAnchorOfTheFile(previousFilename, previousLabel))
+      wxSnprintf(buf, sizeof(buf),
+                 _T("<A HREF=\"%s\">%s</A> "),
+                 ConvertCase(previousFilename), backReference);
+    else
+      wxSnprintf(buf, sizeof(buf),
+                 _T("<A HREF=\"%s#%s\">%s</A> "),
+                 ConvertCase(previousFilename), previousLabel, backReference);
     if (wxStrcmp(previousLabel, _T("contents")) == 0)
     {
 //      TexOutput(_T("<NOFRAMES>"));
@@ -483,7 +525,7 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
   else
   {
     // A placeholder so the buttons don't keep moving position
-    wxSprintf(buf, _T("%s "), backReference);
+    wxSnprintf(buf, sizeof(buf), _T("%s "), backReference);
     TexOutput(buf);
   }
 
@@ -517,13 +559,20 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
 
   if (nextLabel && nextFilename)
   {
-    wxSprintf(buf, _T("<A HREF=\"%s#%s\">%s</A> "), ConvertCase(nextFilename), nextLabel, forwardReference);
+    if (PrimaryAnchorOfTheFile(nextFilename, nextLabel))
+      wxSnprintf(buf, sizeof(buf),
+                 _T("<A HREF=\"%s\">%s</A> "),
+                 ConvertCase(nextFilename), forwardReference);
+    else
+      wxSnprintf(buf, sizeof(buf),
+                 _T("<A HREF=\"%s#%s\">%s</A> "),
+                 ConvertCase(nextFilename), nextLabel, forwardReference);
     TexOutput(buf);
   }
   else
   {
     // A placeholder so the buttons don't keep moving position
-    wxSprintf(buf, _T("%s "), forwardReference);
+    wxSnprintf(buf, sizeof(buf), _T("%s "), forwardReference);
     TexOutput(buf);
   }
 
@@ -555,13 +604,13 @@ wxChar *ParseColourString(wxChar *bkStr, bool *isPicture)
     wxString token1 = tok.GetNextToken();
     if (!tok.HasMoreTokens())
     {
-      *isPicture = TRUE;
+      *isPicture = true;
       return resStr;
     }
     else
     {
       wxString token2 = tok.GetNextToken();
-      *isPicture = FALSE;
+      *isPicture = false;
       if (tok.HasMoreTokens())
       {
         wxString token3 = tok.GetNextToken();
@@ -608,7 +657,7 @@ void OutputBodyStart(void)
   TexOutput(_T("\n<BODY"));
   if (backgroundImageString)
   {
-    bool isPicture = FALSE;
+    bool isPicture = false;
     wxChar *s = ParseColourString(backgroundImageString, &isPicture);
     if (s)
     {
@@ -619,7 +668,7 @@ void OutputBodyStart(void)
   }
   if (backgroundColourString)
   {
-    bool isPicture = FALSE;
+    bool isPicture = false;
     wxChar *s = ParseColourString(backgroundColourString, &isPicture);
     if (s)
     {
@@ -631,7 +680,7 @@ void OutputBodyStart(void)
   // Set foreground text colour, if one is specified
   if (textColourString)
   {
-    bool isPicture = FALSE;
+    bool isPicture = false;
     wxChar *s = ParseColourString(textColourString, &isPicture);
     if (s)
     {
@@ -641,7 +690,7 @@ void OutputBodyStart(void)
   // Set link text colour, if one is specified
   if (linkColourString)
   {
-    bool isPicture = FALSE;
+    bool isPicture = false;
     wxChar *s = ParseColourString(linkColourString, &isPicture);
     if (s)
     {
@@ -651,7 +700,7 @@ void OutputBodyStart(void)
   // Set followed link text colour, if one is specified
   if (followedLinkColourString)
   {
-    bool isPicture = FALSE;
+    bool isPicture = false;
     wxChar *s = ParseColourString(followedLinkColourString, &isPicture);
     if (s)
     {
@@ -700,10 +749,10 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
         chapterNo ++;
 
       SetCurrentOutput(NULL);
-      startedSections = TRUE;
+      startedSections = true;
 
       wxChar *topicName = FindTopicName(GetNextChunk());
-      ReopenFile(&Chapters, &ChaptersName);
+      ReopenFile(&Chapters, &ChaptersName, topicName);
       AddTexRef(topicName, ChaptersName, ChapterNameString);
 
       SetCurrentChapterName(topicName, ChaptersName);
@@ -719,9 +768,9 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
 
       wxChar titleBuf[200];
       if (truncateFilenames)
-        wxSprintf(titleBuf, _T("%s.htm"), wxFileNameFromPath(FileRoot));
+        wxSnprintf(titleBuf, sizeof(titleBuf), _T("%s.htm"), wxFileNameFromPath(FileRoot));
       else
-        wxSprintf(titleBuf, _T("%s_contents.html"), wxFileNameFromPath(FileRoot));
+        wxSnprintf(titleBuf, sizeof(titleBuf), _T("%s_contents.html"), wxFileNameFromPath(FileRoot));
 
       wxFprintf(Chapters, _T("<A NAME=\"%s\"></A>"), topicName);
 
@@ -729,12 +778,18 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
                        lastTopic, lastFileName,  // Last topic
                        topicName, ChaptersName); // This topic
 
-      wxFprintf(Contents, _T("\n<LI><A HREF=\"%s#%s\">"), ConvertCase(ChaptersName), topicName);
+      if(PrimaryAnchorOfTheFile(ChaptersName, topicName))
+        wxFprintf(Contents, _T("\n<LI><A HREF=\"%s\">"), ConvertCase(ChaptersName));
+      else
+        wxFprintf(Contents, _T("\n<LI><A HREF=\"%s#%s\">"), ConvertCase(ChaptersName), topicName);
 
       if (htmlFrameContents && FrameContents)
       {
         SetCurrentOutput(FrameContents);
-        wxFprintf(FrameContents, _T("\n<LI><A HREF=\"%s#%s\" TARGET=\"mainwindow\">"), ConvertCase(ChaptersName), topicName);
+        if(PrimaryAnchorOfTheFile(ChaptersName, topicName))
+          wxFprintf(FrameContents, _T("\n<LI><A HREF=\"%s\" TARGET=\"mainwindow\">"), ConvertCase(ChaptersName));
+        else
+          wxFprintf(FrameContents, _T("\n<LI><A HREF=\"%s#%s\" TARGET=\"mainwindow\">"), ConvertCase(ChaptersName), topicName);
         OutputCurrentSection();
         wxFprintf(FrameContents, _T("</A>\n"));
       }
@@ -765,16 +820,16 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
     {
       subsectionNo = 0;
       subsubsectionNo = 0;
-      subsectionStarted = FALSE;
+      subsectionStarted = false;
 
       if (macroId != ltSECTIONSTAR)
         sectionNo ++;
 
       SetCurrentOutput(NULL);
-      startedSections = TRUE;
+      startedSections = true;
 
       wxChar *topicName = FindTopicName(GetNextChunk());
-      ReopenFile(&Sections, &SectionsName);
+      ReopenFile(&Sections, &SectionsName, topicName);
       AddTexRef(topicName, SectionsName, SectionNameString);
 
       SetCurrentSectionName(topicName, SectionsName);
@@ -796,9 +851,19 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
 
       SetCurrentOutputs(jumpFrom, Sections);
       if (DocumentStyle == LATEX_ARTICLE)
-        wxFprintf(jumpFrom, _T("\n<LI><A HREF=\"%s#%s\">"), ConvertCase(SectionsName), topicName);
+      {
+        if(PrimaryAnchorOfTheFile(SectionsName, topicName))
+          wxFprintf(jumpFrom, _T("\n<LI><A HREF=\"%s\">"), ConvertCase(SectionsName));
+        else
+          wxFprintf(jumpFrom, _T("\n<LI><A HREF=\"%s#%s\">"), ConvertCase(SectionsName), topicName);
+      }
       else
-        wxFprintf(jumpFrom, _T("\n<A HREF=\"%s#%s\"><B>"), ConvertCase(SectionsName), topicName);
+      {
+        if(PrimaryAnchorOfTheFile(SectionsName, topicName))
+          wxFprintf(jumpFrom, _T("\n<A HREF=\"%s\"><B>"), ConvertCase(SectionsName));
+        else
+          wxFprintf(jumpFrom, _T("\n<A HREF=\"%s#%s\"><B>"), ConvertCase(SectionsName), topicName);
+      }
 
       wxFprintf(Sections, _T("\n<H2>"));
       OutputCurrentSection();
@@ -839,6 +904,8 @@ 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);
@@ -850,7 +917,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
                 int ch = getc(fd);
                 while (ch != EOF)
                 {
-                    putc(ch, Sections);
+                    wxPutc(ch, Sections);
                     ch = getc(fd);
                 }
                 fclose(fd);
@@ -861,21 +928,21 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
             ReopenSectionContentsFile();
           }
 
-          startedSections = TRUE;
-          subsectionStarted = TRUE;
+          startedSections = true;
+          subsectionStarted = true;
 
           wxChar *topicName = FindTopicName(GetNextChunk());
 
           if ( !combineSubSections )
           {
             SetCurrentOutput(NULL);
-            ReopenFile(&Subsections, &SubsectionsName);
+            ReopenFile(&Subsections, &SubsectionsName, topicName);
             AddTexRef(topicName, SubsectionsName, SubsectionNameString);
             SetCurrentSubsectionName(topicName, SubsectionsName);
             if (htmlWorkshopFiles) HTMLWorkshopAddToContents(2, topicName, SubsectionsName);
             SetCurrentOutput(Subsections);
 
-           HTMLHead();
+            HTMLHead();
             TexOutput(_T("<title>"));
             OutputCurrentSection();
             TexOutput(_T("</title></head>\n"));
@@ -887,7 +954,10 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
                            topicName, SubsectionsName); // This topic
 
             SetCurrentOutputs(Sections, Subsections);
-            wxFprintf(Sections, _T("\n<A HREF=\"%s#%s\"><B>"), ConvertCase(SubsectionsName), topicName);
+            if(PrimaryAnchorOfTheFile(SubsectionsName, topicName))
+              wxFprintf(Sections, _T("\n<A HREF=\"%s\"><B>"), ConvertCase(SubsectionsName));
+            else
+              wxFprintf(Sections, _T("\n<A HREF=\"%s#%s\"><B>"), ConvertCase(SubsectionsName), topicName);
 
             wxFprintf(Subsections, _T("\n<H3>"));
             OutputCurrentSection();
@@ -943,20 +1013,20 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
         if (macroId != ltSUBSUBSECTIONSTAR)
           subsubsectionNo ++;
 
-        startedSections = TRUE;
+        startedSections = true;
 
         wxChar *topicName = FindTopicName(GetNextChunk());
 
         if ( !combineSubSections )
         {
             SetCurrentOutput(NULL);
-            ReopenFile(&Subsubsections, &SubsubsectionsName);
+            ReopenFile(&Subsubsections, &SubsubsectionsName, topicName);
             AddTexRef(topicName, SubsubsectionsName, SubsubsectionNameString);
             SetCurrentSubsubsectionName(topicName, SubsubsectionsName);
             if (htmlWorkshopFiles) HTMLWorkshopAddToContents(3, topicName, SubsubsectionsName);
 
             SetCurrentOutput(Subsubsections);
-           HTMLHead();
+            HTMLHead();
             TexOutput(_T("<title>"));
             OutputCurrentSection();
             TexOutput(_T("</title></head>\n"));
@@ -969,7 +1039,10 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
                          topicName, SubsubsectionsName); // This topic
 
             SetCurrentOutputs(Subsections, Subsubsections);
-            wxFprintf(Subsections, _T("\n<A HREF=\"%s#%s\"><B>"), ConvertCase(SubsubsectionsName), topicName);
+            if(PrimaryAnchorOfTheFile(SubsubsectionsName, topicName))
+              wxFprintf(Subsections, _T("\n<A HREF=\"%s\"><B>"), ConvertCase(SubsubsectionsName));
+            else
+              wxFprintf(Subsections, _T("\n<A HREF=\"%s#%s\"><B>"), ConvertCase(SubsubsectionsName), topicName);
 
             wxFprintf(Subsubsections, _T("\n<H3>"));
             OutputCurrentSection();
@@ -1082,9 +1155,9 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
 
         wxChar buf[100];
         if (TableData[currentColumn].justification == 'c')
-          wxSprintf(buf, _T("\n<TD ALIGN=CENTER>"));
+          wxSnprintf(buf, sizeof(buf), _T("\n<TD ALIGN=CENTER>"));
         else if (TableData[currentColumn].justification == 'r')
-          wxSprintf(buf, _T("\n<TD ALIGN=RIGHT>"));
+          wxSnprintf(buf, sizeof(buf), _T("\n<TD ALIGN=RIGHT>"));
         else if (TableData[currentColumn].absWidth)
         {
           // Convert from points * 20 into pixels.
@@ -1093,12 +1166,12 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
           // Say the display is 100 DPI (dots/pixels per inch).
           // There are 72 pts to the inch. So 1pt = 1/72 inch, or 100 * 1/72 dots.
           int pixels = (int)(points * 100.0 / 72.0);
-          wxSprintf(buf, _T("<TD ALIGN=CENTER WIDTH=%d>"), pixels);
+          wxSnprintf(buf, sizeof(buf), _T("<TD ALIGN=CENTER WIDTH=%d>"), pixels);
         }
         else
-          wxSprintf(buf, _T("\n<TD ALIGN=LEFT>"));
+          wxSnprintf(buf, sizeof(buf), _T("\n<TD ALIGN=LEFT>"));
         TexOutput(buf);
-               OutputFont();
+        OutputFont();
       }
       else
         TexOutput(_T("&amp;"));
@@ -1131,9 +1204,9 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
       // Start new row and cell, setting alignment for the first cell.
       wxChar buf[100];
       if (TableData[currentColumn].justification == 'c')
-        wxSprintf(buf, _T("<TR>\n<TD ALIGN=CENTER>"));
+        wxSnprintf(buf, sizeof(buf), _T("<TR>\n<TD ALIGN=CENTER>"));
       else if (TableData[currentColumn].justification == 'r')
-        wxSprintf(buf, _T("<TR>\n<TD ALIGN=RIGHT>"));
+        wxSnprintf(buf, sizeof(buf), _T("<TR>\n<TD ALIGN=RIGHT>"));
       else if (TableData[currentColumn].absWidth)
       {
         // Convert from points * 20 into pixels.
@@ -1142,12 +1215,12 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
         // Say the display is 100 DPI (dots/pixels per inch).
         // There are 72 pts to the inch. So 1pt = 1/72 inch, or 100 * 1/72 dots.
         int pixels = (int)(points * 100.0 / 72.0);
-        wxSprintf(buf, _T("<TR>\n<TD ALIGN=CENTER WIDTH=%d>"), pixels);
+        wxSnprintf(buf, sizeof(buf), _T("<TR>\n<TD ALIGN=CENTER WIDTH=%d>"), pixels);
       }
       else
-        wxSprintf(buf, _T("<TR>\n<TD ALIGN=LEFT>"));
+        wxSnprintf(buf, sizeof(buf), _T("<TR>\n<TD ALIGN=LEFT>"));
       TexOutput(buf);
-         OutputFont();
+      OutputFont();
     }
     else
     {
@@ -1170,10 +1243,11 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
   {
     if (start)
     {
-      if (inVerbatim)
+      #if (1) // if(inVerbatim)
         TexOutput(_T("~"));
-      else
+      #else
         TexOutput(_T(" "));
+      #endif
     }
     break;
   }
@@ -1286,7 +1360,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
     if (start)
     {
       wxChar buf[100];
-      wxSprintf(buf, _T("<PRE>\n"));
+      wxSnprintf(buf, sizeof(buf), _T("<PRE>\n"));
       TexOutput(buf);
     }
     else TexOutput(_T("</PRE>\n"));
@@ -1439,13 +1513,13 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
   case ltCOPYRIGHT:
   {
     if (start)
-      TexOutput(_T("&copy;"), TRUE);
+      TexOutput(_T("&copy;"), true);
     break;
   }
   case ltREGISTERED:
   {
     if (start)
-      TexOutput(_T("&reg;"), TRUE);
+      TexOutput(_T("&reg;"), true);
     break;
   }
   // Arrows
@@ -1613,16 +1687,22 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
   {
     if (start)
     {
+      // NB: if this is uncommented, the table of contents
+      // completely disappears. If left commented, it's in the wrong
+      // place.
+      //fflush(Titlepage);
+
       FILE *fd = wxFopen(ContentsName, _T("r"));
       if (fd)
       {
         int ch = getc(fd);
         while (ch != EOF)
         {
-          putc(ch, Titlepage);
+          wxPutc(ch, Titlepage);
           ch = getc(fd);
         }
         fclose(fd);
+        fflush(Titlepage);
       }
       else
       {
@@ -1668,18 +1748,18 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
           figureNo ++;
 
           if (DocumentStyle != LATEX_ARTICLE)
-            wxSprintf(figBuf, _T("%s %d.%d: "), FigureNameString, chapterNo, figureNo);
+            wxSnprintf(figBuf, sizeof(figBuf), _T("%s %d.%d: "), FigureNameString, chapterNo, figureNo);
           else
-            wxSprintf(figBuf, _T("%s %d: "), FigureNameString, figureNo);
+            wxSnprintf(figBuf, sizeof(figBuf), _T("%s %d: "), FigureNameString, figureNo);
       }
       else
       {
           tableNo ++;
 
           if (DocumentStyle != LATEX_ARTICLE)
-            wxSprintf(figBuf, _T("%s %d.%d: "), TableNameString, chapterNo, tableNo);
+            wxSnprintf(figBuf, sizeof(figBuf), _T("%s %d.%d: "), TableNameString, chapterNo, tableNo);
           else
-            wxSprintf(figBuf, _T("%s %d: "), TableNameString, tableNo);
+            wxSnprintf(figBuf, sizeof(figBuf), _T("%s %d: "), TableNameString, tableNo);
       }
 
       TexOutput(figBuf);
@@ -1706,14 +1786,14 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
   }
   case ltFIGURE:
   {
-    if (start) inFigure = TRUE;
-    else inFigure = FALSE;
+    if (start) inFigure = true;
+    else inFigure = false;
     break;
   }
   case ltTABLE:
   {
-    if (start) inTable = TRUE;
-    else inTable = FALSE;
+    if (start) inTable = true;
+    else inTable = false;
     break;
   }
   default:
@@ -1721,7 +1801,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.IsEmpty() ) {
+    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)
 {
@@ -1743,12 +1890,17 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
   {
     if (!start && (arg_no == 1))
       currentSection = GetArgChunk();
-    return FALSE;
+    return false;
   }
   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> "));
@@ -1809,27 +1961,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>"));
@@ -1868,19 +2014,19 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       {
         TexOutput(sec);
       }
-      return FALSE;
+      return false;
     }
     break;
   }
   case ltURLREF:
   {
     if (IsArgOptional())
-      return FALSE;
+      return false;
     else if ((GetNoArgs() - arg_no) == 1)
     {
       if (start)
         helpRefText = GetArgChunk();
-      return FALSE;
+      return false;
     }
     else if ((GetNoArgs() - arg_no) == 0) // Arg = 2, or 3 if first is optional
     {
@@ -1888,18 +2034,19 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       {
         TexChunk *ref = GetArgChunk();
         TexOutput(_T("<A HREF=\""));
-        inVerbatim = TRUE;
+        inVerbatim = true;
         TraverseChildrenFromChunk(ref);
-        inVerbatim = FALSE;
+        inVerbatim = false;
         TexOutput(_T("\">"));
         if (helpRefText)
           TraverseChildrenFromChunk(helpRefText);
         TexOutput(_T("</A>"));
       }
-      return FALSE;
+      return false;
     }
     break;
   }
+
   case ltHELPREF:
   case ltHELPREFN:
   case ltPOPREF:
@@ -1908,13 +2055,13 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     {
       if (start)
         helpRefFilename = GetArgChunk();
-      return FALSE;
+      return false;
     }
     if ((GetNoArgs() - arg_no) == 1)
     {
       if (start)
         helpRefText = GetArgChunk();
-      return FALSE;
+      return false;
     }
     else if ((GetNoArgs() - arg_no) == 0) // Arg = 2, or 3 if first is optional
     {
@@ -1935,16 +2082,20 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
             // If a filename is supplied, use it, otherwise try to
             // use the filename associated with the reference (from this document).
             if (helpRefFilename)
-               {
+            {
               TraverseChildrenFromChunk(helpRefFilename);
               TexOutput(_T("#"));
-               }
+              TexOutput(refName);
+            }
             else if (refFilename)
-               {
+            {
               TexOutput(ConvertCase(refFilename));
-              TexOutput(_T("#"));
-               }
-            TexOutput(refName);
+              if(!PrimaryAnchorOfTheFile(texRef->refFile, refName))
+              {
+                TexOutput(_T("#"));
+                TexOutput(refName);
+              }
+            }
             TexOutput(_T("\">"));
             if (helpRefText)
               TraverseChildrenFromChunk(helpRefText);
@@ -1963,7 +2114,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
         else TexOutput(_T("??"));
       }
-      return FALSE;
+      return false;
     }
     break;
   }
@@ -2023,7 +2174,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
           // If we have found the existing filename, make the inline
           // image point to the original file (could be PS, for example)
           if (originalFilename && (wxStrcmp(inlineFilename, originalFilename) != 0))
-         {
+          {
             TexOutput(_T("<A HREF=\""));
             TexOutput(ConvertCase(originalFilename));
             TexOutput(_T("\">"));
@@ -2032,17 +2183,17 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
             TexOutput(_T("\""));
             TexOutput(alignment); 
             TexOutput(_T("></A>"));
-         }
+          }
           else
 #endif
-         {
+          {
             TexOutput(_T("<img src=\""));
             TexOutput(ConvertCase(wxFileNameFromPath(inlineFilename)));
             TexOutput(_T("\""));
             TexOutput(alignment); 
             TexOutput(_T(">"));
             delete[] inlineFilename;
-         }
+          }
         }
         else
         {
@@ -2050,12 +2201,12 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
           TexOutput(_T("<A HREF=\""));
           TexOutput(ConvertCase(wxFileNameFromPath(filename)));
           TexOutput(_T("\">Picture</A>\n"));
-          wxSprintf(buf, _T("Warning: could not find an inline XBM/GIF for %s."), filename);
+          wxSnprintf(buf, sizeof(buf), _T("Warning: could not find an inline XBM/GIF for %s."), filename);
           OnInform(buf);
         }
       }
     }
-    return FALSE;
+    return false;
   }
   // First arg is PSBOX spec (ignored), second is image file, third is map name.
   case ltIMAGEMAP:
@@ -2082,7 +2233,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       if (f == _T(""))
       {
         wxChar buf[300];
-        wxSprintf(buf, _T("Warning: could not find an inline XBM/GIF for %s."), filename);
+        wxSnprintf(buf, sizeof(buf), _T("Warning: could not find an inline XBM/GIF for %s."), filename);
         OnInform(buf);
       }
       delete[] filename;
@@ -2126,15 +2277,15 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         imageFile = NULL;
       }
     }
-    return FALSE;
+    return false;
   }
   case ltINDENTED :
   {
     if ( arg_no == 1 )
-        return FALSE;
+        return false;
     else
     {
-        return TRUE;
+        return true;
     }
   }
   case ltITEM:
@@ -2142,9 +2293,9 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     if (start)
     {
       descriptionItemArg = GetArgChunk();
-      return FALSE;
+      return false;
     }
-    return TRUE;
+    return true;
   }
   case ltTWOCOLITEM:
   case ltTWOCOLITEMRULED:
@@ -2159,31 +2310,40 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     {
       if ( start ) {
         // DHS
-       if (TwoColWidthA > -1) {
+        if (TwoColWidthA > -1)
+        {
           wxChar buf[100];
-          wxSprintf(buf,_T("\n<TR><TD VALIGN=TOP WIDTH=%d>\n"),TwoColWidthA);
+          wxSnprintf(buf, sizeof(buf), _T("\n<TR><TD VALIGN=TOP WIDTH=%d>\n"),TwoColWidthA);
           TexOutput(buf);
-        } else
+        }
+        else
+        {
           TexOutput(_T("\n<TR><TD VALIGN=TOP>\n"));
-               OutputFont();
+        }
+        OutputFont();
       }  else
             TexOutput(_T("\n</FONT></TD>\n"));
     }
     if (arg_no == 2)
     {
       // DHS
-      if ( start ) {
-       if (TwoColWidthB > -1) {
+      if ( start )
+      {
+        if (TwoColWidthB > -1)
+        {
           wxChar buf[100];
-          wxSprintf(buf,_T("\n<TD VALIGN=TOP WIDTH=%d>\n"),TwoColWidthB);
+          wxSnprintf(buf, sizeof(buf), _T("\n<TD VALIGN=TOP WIDTH=%d>\n"),TwoColWidthB);
           TexOutput(buf);
-        } else 
-           TexOutput(_T("\n<TD VALIGN=TOP>\n"));
-               OutputFont();
+        }
+        else 
+        {
+          TexOutput(_T("\n<TD VALIGN=TOP>\n"));
+        }
+        OutputFont();
       }  else
            TexOutput(_T("\n</FONT></TD></TR>\n"));
     }
-    return TRUE;
+    return true;
   }
   case ltNUMBEREDBIBITEM:
   {
@@ -2205,18 +2365,18 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       if (ref)
       {
         if (ref->sectionNumber) delete[] ref->sectionNumber;
-        wxSprintf(buf, _T("[%d]"), citeCount);
+        wxSnprintf(buf, sizeof(buf), _T("[%d]"), citeCount);
         ref->sectionNumber = copystring(buf);
       }
 
-      wxSprintf(buf, _T("\n<DT> [%d] "), citeCount);
+      wxSnprintf(buf, sizeof(buf), _T("\n<DT> [%d] "), citeCount);
       TexOutput(buf);
       citeCount ++;
-      return FALSE;
+      return false;
     }
     if (arg_no == 2 && !start)
       TexOutput(_T("<P>\n"));
-    return TRUE;
+    return true;
   }
   case ltMARGINPAR:
   case ltMARGINPARODD:
@@ -2227,7 +2387,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     if (start)
     {
       TexOutput(_T("<HR>\n"));
-      return TRUE;
+      return true;
     }
     else
       TexOutput(_T("<HR><P>\n"));
@@ -2242,7 +2402,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       float points = ParseUnitArgument(val);
       TwoColWidthA = (int)((points * 100.0) / 72.0);
     }
-    return FALSE;
+    return false;
   }
   // DHS
   case ltTWOCOLWIDTHB:
@@ -2253,7 +2413,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       float points = ParseUnitArgument(val);
       TwoColWidthB = (int)((points * 100.0) / 72.0);
     }
-    return FALSE;
+    return false;
   }
   /*
    * Accents
@@ -2303,7 +2463,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
       }
     }
-    return FALSE;
+    return false;
   }
   case ltACCENT_ACUTE:
   {
@@ -2355,7 +2515,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
       }
     }
-    return FALSE;
+    return false;
   }
   case ltACCENT_CARET:
   {
@@ -2401,7 +2561,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
       }
     }
-    return FALSE;
+    return false;
   }
   case ltACCENT_TILDE:
   {
@@ -2438,7 +2598,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
       }
     }
-    return FALSE;
+    return false;
   }
   case ltACCENT_UMLAUT:
   {
@@ -2490,7 +2650,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
       }
     }
-    return FALSE;
+    return false;
   }
   case ltACCENT_DOT:
   {
@@ -2512,7 +2672,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
       }
     }
-    return FALSE;
+    return false;
   }
   case ltBACKGROUND:
   {
@@ -2521,7 +2681,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       wxChar *val = GetArgData();
       if (val)
       {
-        bool isPicture = FALSE;
+        bool isPicture = false;
         ParseColourString(val, &isPicture);
         if (isPicture)
         {
@@ -2537,7 +2697,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
       }
     }
-    return FALSE;
+    return false;
   }
   case ltBACKGROUNDIMAGE:
   {
@@ -2551,7 +2711,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         backgroundImageString = copystring(val);
       }
     }
-    return FALSE;
+    return false;
   }
   case ltBACKGROUNDCOLOUR:
   {
@@ -2565,7 +2725,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         backgroundColourString = copystring(val);
       }
     }
-    return FALSE;
+    return false;
   }
   case ltTEXTCOLOUR:
   {
@@ -2579,7 +2739,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         textColourString = copystring(val);
       }
     }
-    return FALSE;
+    return false;
   }
   case ltLINKCOLOUR:
   {
@@ -2593,7 +2753,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         linkColourString = copystring(val);
       }
     }
-    return FALSE;
+    return false;
   }
   case ltFOLLOWEDLINKCOLOUR:
   {
@@ -2607,7 +2767,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         followedLinkColourString = copystring(val);
       }
     }
-    return FALSE;
+    return false;
   }
   case ltACCENT_CADILLA:
   {
@@ -2629,16 +2789,16 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
       }
     }
-    return FALSE;
+    return false;
   }
 /*
   case ltFOOTNOTE:
   case ltFOOTNOTEPOPUP:
   {
     if (arg_no == 1)
-      return TRUE;
+      return true;
     else
-      return FALSE;
+      return false;
     break;
   }
 */
@@ -2650,10 +2810,10 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
       if (start)
       {
         currentRowNumber = 0;
-        inTabular = TRUE;
-        startRows = TRUE;
-        tableVerticalLineLeft = FALSE;
-        tableVerticalLineRight = FALSE;
+        inTabular = true;
+        startRows = true;
+        tableVerticalLineLeft = false;
+        tableVerticalLineRight = false;
 
         wxChar *alignString = copystring(GetArgData());
         ParseTableArgument(alignString);
@@ -2665,12 +2825,12 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         if (compatibilityMode)
         {
           TexOutput(_T("<TR>\n<TD>"));
-                 OutputFont();
+          OutputFont();
 /*
           for (int i = 0; i < noColumns; i++)
           {
             currentWidth += TableData[i].width;
-            wxSprintf(buf, _T("\\cellx%d"), currentWidth);
+            wxSnprintf(buf, sizeof(buf), _T("\\cellx%d"), currentWidth);
             TexOutput(buf);
           }
           TexOutput(_T("\\pard\\intbl\n"));
@@ -2678,13 +2838,13 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
         delete[] alignString;
 
-        return FALSE;
+        return false;
       }
     }
     else if (arg_no == 2 && !start)
     {
       TexOutput(_T("</TABLE>\n"));
-      inTabular = FALSE;
+      inTabular = false;
     }
     break;
   }
@@ -2692,7 +2852,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
   {
     if (start && (arg_no == 1))
     {
-      ReopenFile(&Chapters, &ChaptersName);
+      ReopenFile(&Chapters, &ChaptersName, _T("bibliography"));
       AddTexRef(_T("bibliography"), ChaptersName, _T("bibliography"));
       SetCurrentSubsectionName(_T("bibliography"), ChaptersName);
 
@@ -2702,9 +2862,9 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
 
       wxChar titleBuf[150];
       if (truncateFilenames)
-        wxSprintf(titleBuf, _T("%s.htm"), wxFileNameFromPath(FileRoot));
+        wxSnprintf(titleBuf, sizeof(titleBuf), _T("%s.htm"), wxFileNameFromPath(FileRoot));
       else
-        wxSprintf(titleBuf, _T("%s_contents.html"), wxFileNameFromPath(FileRoot));
+        wxSnprintf(titleBuf, sizeof(titleBuf), _T("%s_contents.html"), wxFileNameFromPath(FileRoot));
 
       HTMLHead();
       TexOutput(_T("<title>"));
@@ -2718,18 +2878,21 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
                        _T("bibliography"), ChaptersName); // This topic
 
       SetCurrentOutputs(Contents, Chapters);
-      wxFprintf(Contents, _T("\n<LI><A HREF=\"%s#%s\">"), ConvertCase(ChaptersName), "bibliography");
+      if(PrimaryAnchorOfTheFile(ChaptersName, _T("bibliography")))
+        wxFprintf(Contents, _T("\n<LI><A HREF=\"%s\">"), ConvertCase(ChaptersName));
+      else
+        wxFprintf(Contents, _T("\n<LI><A HREF=\"%s#%s\">"), ConvertCase(ChaptersName), _T("bibliography"));
 
       wxFprintf(Contents, _T("%s</A>\n"), ReferencesNameString);
       wxFprintf(Chapters, _T("</H2>\n</A>\n"));
 
       SetCurrentOutput(Chapters);
-      return FALSE;
+      return false;
     }
     if (!start && (arg_no == 2))
     {
     }
-    return TRUE;
+    return true;
   }
   case ltINDEX:
   {
@@ -2744,7 +2907,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         AddKeyWordForTopic(CurrentTopic, buf, currentFileName);
       }
     }
-    return FALSE;
+    return false;
   }
   case ltFCOL:
 //  case ltBCOL:
@@ -2761,7 +2924,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
           {
             wxStrcpy(buf2, _T("#000000"));
             wxChar buf[100];
-                       wxSprintf(buf, _T("Could not find colour name %s"), name);
+            wxSnprintf(buf, sizeof(buf), _T("Could not find colour name %s"), name);
             OnError(buf);
           }
           TexOutput(_T("<FONT COLOR=\""));
@@ -2771,7 +2934,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         }
         case 2:
         {
-          return TRUE;
+          return true;
         }
         default:
           break;
@@ -2781,14 +2944,14 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     {
       if (arg_no == 2) TexOutput(_T("</FONT>"));
     }
-    return FALSE;
+    return false;
   }
   case ltINSERTATLEVEL:
   {
     // This macro allows you to insert text at a different level
     // from the current level, e.g. into the Sections from within a subsubsection.
     if (useWord)
-        return FALSE;
+        return false;
     static int currentLevelNo = 1;
     static FILE* oldLevelFile = Chapters;
     if (start)
@@ -2833,16 +2996,16 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
           }
           if (outputFile)
             CurrentOutput1 = outputFile;
-          return FALSE;
+          return false;
         }
         case 2:
         {
-          return TRUE;
+          return true;
         }
         default:
           break;
       }
-      return TRUE;
+      return true;
     }
     else
     {
@@ -2850,24 +3013,24 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
         {
             CurrentOutput1 = oldLevelFile;
         }
-        return TRUE;
+        return true;
     }
   }
   default:
     return DefaultOnArgument(macroId, arg_no, start);
   }
-  return TRUE;
+  return true;
 }
 
 bool HTMLGo(void)
 {
   fileId = 0;
-  inVerbatim = FALSE;
+  inVerbatim = false;
   indentLevel = 0;
-  inTabular = FALSE;
-  startRows = FALSE;
-  tableVerticalLineLeft = FALSE;
-  tableVerticalLineRight = FALSE;
+  inTabular = false;
+  startRows = false;
+  tableVerticalLineLeft = false;
+  tableVerticalLineRight = false;
   noColumns = 0;
 
   if (InputFile && OutputFile)
@@ -2878,17 +3041,17 @@ bool HTMLGo(void)
 
     wxChar buf[300];
     if (truncateFilenames)
-      wxSprintf(buf, _T("%s.htm"), FileRoot);
+      wxSnprintf(buf, sizeof(buf), _T("%s.htm"), FileRoot);
     else
-      wxSprintf(buf, _T("%s_contents.html"), FileRoot);
+      wxSnprintf(buf, sizeof(buf), _T("%s_contents.html"), FileRoot);
     if (TitlepageName) delete[] TitlepageName;
     TitlepageName = copystring(buf);
     Titlepage = wxFopen(buf, _T("w"));
 
     if (truncateFilenames)
-      wxSprintf(buf, _T("%s_fc.htm"), FileRoot);
+      wxSnprintf(buf, sizeof(buf), _T("%s_fc.htm"), FileRoot);
     else
-      wxSprintf(buf, _T("%s_fcontents.html"), FileRoot);
+      wxSnprintf(buf, sizeof(buf), _T("%s_fcontents.html"), FileRoot);
 
     contentsFrameName = copystring(buf);
 
@@ -2904,7 +3067,7 @@ bool HTMLGo(void)
     if (!Titlepage || !Contents)
     {
       OnError(_T("Cannot open output file!"));
-      return FALSE;
+      return false;
     }
     AddTexRef(_T("contents"), wxFileNameFromPath(TitlepageName), ContentsNameString);
 
@@ -2978,15 +3141,15 @@ bool HTMLGo(void)
       if (DocumentTitle)
       {
         SetCurrentOutput(tmpTitle);
-       HTMLHead();
-        TexOutput(_T("\n<HEAD><TITLE>"));
+        HTMLHead();
+        TexOutput(_T("\n<TITLE>"));
         TraverseChildrenFromChunk(DocumentTitle);
         TexOutput(_T("</TITLE></HEAD>\n"));
       }
       else
       {
         SetCurrentOutput(tmpTitle);
-       HTMLHeadTo(tmpTitle);
+        HTMLHeadTo(tmpTitle);
         if (contentsString)
           wxFprintf(tmpTitle, _T("<TITLE>%s</TITLE></HEAD>\n\n"), contentsString);
         else
@@ -2998,9 +3161,9 @@ bool HTMLGo(void)
       {
         wxChar firstFileName[300];
         if (truncateFilenames)
-          wxSprintf(firstFileName, _T("%s1.htm"), FileRoot);
+          wxSnprintf(firstFileName, sizeof(firstFileName), _T("%s1.htm"), FileRoot);
         else
-          wxSprintf(firstFileName, _T("%s1.html"), FileRoot);
+          wxStrcpy(firstFileName, gs_filenames[1].c_str());
 
         wxFprintf(tmpTitle, _T("<FRAMESET COLS=\"30%%,70%%\">\n"));
 
@@ -3013,6 +3176,7 @@ bool HTMLGo(void)
 
       // Output <BODY...> to temporary title page
       OutputBodyStart();
+      fflush(tmpTitle);
 
       // Concat titlepage
       FILE *fd = wxFopen(TitlepageName, _T("r"));
@@ -3021,7 +3185,7 @@ bool HTMLGo(void)
         int ch = getc(fd);
         while (ch != EOF)
         {
-          putc(ch, tmpTitle);
+          wxPutc(ch, tmpTitle);
           ch = getc(fd);
         }
         fclose(fd);
@@ -3061,7 +3225,7 @@ bool HTMLGo(void)
     if (htmlIndex)
     {
       wxChar htmlIndexName[300];
-      wxSprintf(htmlIndexName, _T("%s.htx"), FileRoot);
+      wxSnprintf(htmlIndexName, sizeof(htmlIndexName), _T("%s.htx"), FileRoot);
       GenerateHTMLIndexFile(htmlIndexName);
     }
 
@@ -3073,10 +3237,10 @@ bool HTMLGo(void)
     }
 
 
-    return TRUE;
+    return true;
   }
 
-  return FALSE;
+  return false;
 }
 
 // Output .htx index file
@@ -3087,7 +3251,7 @@ void GenerateHTMLIndexFile(wxChar *fname)
     return;
 
   TopicTable.BeginFind();
-  wxNode *node = TopicTable.Next();
+  wxHashTable::Node *node = TopicTable.Next();
   while (node)
   {
     TexTopic *texTopic = (TexTopic *)node->GetData();
@@ -3123,7 +3287,7 @@ void GenerateHTMLWorkshopFiles(wxChar *fname)
 
   /* Generate project file : */
 
-  wxSprintf(buf, _T("%s.hhp"), fname);
+  wxSnprintf(buf, sizeof(buf), _T("%s.hhp"), fname);
   f = wxFopen(buf, _T("wt"));
   wxFprintf(f,
       _T("[OPTIONS]\n")
@@ -3160,16 +3324,16 @@ void GenerateHTMLWorkshopFiles(wxChar *fname)
   wxFprintf(f, _T("%s\n"), wxFileNameFromPath(TitlepageName));
   for (int i = 1; i <= fileId; i++) {
     if (truncateFilenames)
-      wxSprintf(buf, _T("%s%d.htm"), wxFileNameFromPath(FileRoot), i);
+      wxSnprintf(buf, sizeof(buf), _T("%s%d.htm"), wxFileNameFromPath(FileRoot), i);
     else
-      wxSprintf(buf, _T("%s%d.html"), wxFileNameFromPath(FileRoot), i);
+      wxStrcpy(buf, wxFileNameFromPath(gs_filenames[i].c_str()));
     wxFprintf(f, _T("%s\n"), buf);
   }
   fclose(f);
 
   /* Generate index file : */
 
-  wxSprintf(buf, _T("%s.hhk"), fname);
+  wxSnprintf(buf, sizeof(buf), _T("%s.hhk"), fname);
   f = wxFopen(buf, _T("wt"));
 
   wxFprintf(f,
@@ -3187,7 +3351,7 @@ void GenerateHTMLWorkshopFiles(wxChar *fname)
       _T("<UL>\n"));
 
   TopicTable.BeginFind();
-  wxNode *node = TopicTable.Next();
+  wxHashTable::Node *node = TopicTable.Next();
   while (node)
   {
     TexTopic *texTopic = (TexTopic *)node->GetData();
@@ -3203,7 +3367,7 @@ void GenerateHTMLWorkshopFiles(wxChar *fname)
             _T("  <param name=\"Local\" value=\"%s#%s\">\n")
             _T("  <param name=\"Name\" value=\"%s\">\n")
             _T("  </OBJECT>\n"),
-           texTopic->filename, topicName, s);
+        texTopic->filename, topicName, s);
         node1 = node1->GetNext();
       }
     }
@@ -3235,10 +3399,10 @@ void HTMLWorkshopAddToContents(int level, wxChar *s, wxChar *file)
             _T(" <LI> <OBJECT type=\"text/sitemap\">\n")
             _T("  <param name=\"Local\" value=\"%s#%s\">\n")
             _T("  <param name=\"Name\" value=\""),
-           file, s);
+            file, s);
   OutputCurrentSection();
   wxFprintf(HTMLWorkshopContents,
-                 _T("\">\n")
+            _T("\">\n")
             _T("  </OBJECT>\n"));
   HTMLWorkshopLastLevel = level;
 }
@@ -3247,7 +3411,7 @@ void HTMLWorkshopAddToContents(int level, wxChar *s, wxChar *file)
 void HTMLWorkshopStartContents()
 {
   wxChar buf[300];
-  wxSprintf(buf, _T("%s.hhc"), FileRoot);
+  wxSnprintf(buf, sizeof(buf), _T("%s.hhc"), FileRoot);
   HTMLWorkshopContents = wxFopen(buf, _T("wt"));
   HTMLWorkshopLastLevel = 0;
 
@@ -3275,7 +3439,15 @@ void HTMLWorkshopStartContents()
 
 void HTMLWorkshopEndContents()
 {
-  for (int i = HTMLWorkshopLastLevel; i >= 0; i--)
-    wxFprintf(HTMLWorkshopContents, _T("</UL>\n"));
-  fclose(HTMLWorkshopContents);
+    for (int i = HTMLWorkshopLastLevel; i >= 0; i--)
+        wxFprintf(HTMLWorkshopContents, _T("</UL>\n"));
+    fclose(HTMLWorkshopContents);
+}
+
+
+bool PrimaryAnchorOfTheFile( wxChar *file, wxChar *label )
+{
+    wxString file_label;
+    file_label.Printf( HTML_FILENAME_PATTERN, FileRoot, label );
+    return file_label.IsSameAs( file , false );
 }