]> git.saurik.com Git - wxWidgets.git/blobdiff - utils/tex2rtf/src/htmlutil.cpp
give an error message if we're built in Unicode mode (in response to bug 1079224)
[wxWidgets.git] / utils / tex2rtf / src / htmlutil.cpp
index cc8187395f09bf4d45649477ebb025b76ad8495a..7931b51e5ae49059f53cfe183dc58c62d296b597 100644 (file)
@@ -2,7 +2,7 @@
 // Name:        htmlutil.cpp
 // Purpose:     Converts Latex to HTML
 // Author:      Julian Smart
-// Modified by: Wlodzimiez ABX Skiba 2003/2004 Unicode support
+// Modified by: Wlodzimierz ABX Skiba 2003/2004 Unicode support
 //              Ron Lee
 // Created:     7.9.93
 // RCS-ID:      $Id$
@@ -30,6 +30,8 @@
 #include "tex2rtf.h"
 #include "table.h"
 
+#define HTML_FILENAME_PATTERN _T("%s_%s.html")
+
 #if !WXWIN_COMPATIBILITY_2_4
 static inline wxChar* copystring(const wxChar* s)
     { return wxStrcpy(new wxChar[wxStrlen(s) + 1], s); }
@@ -41,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();
@@ -205,7 +209,7 @@ void ReopenFile(FILE **fd, wxChar **fileName, const wxChar *label)
   {
     if (fileId == 1)
       gs_filenames.Add(wxEmptyString);
-    wxSnprintf(buf, sizeof(buf), _T("%s_%s.html"), FileRoot, label);
+    wxSnprintf(buf, sizeof(buf), HTML_FILENAME_PATTERN, FileRoot, label);
     gs_filenames.Add(buf);
   }
   if (*fileName) delete[] *fileName;
@@ -258,7 +262,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)
   {
@@ -273,19 +277,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;"));
@@ -293,7 +297,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;"));
@@ -476,7 +480,7 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
 
   if (upLabel && upFilename)
   {
-    if (wxStrlen(upLabel) > 0)
+    if ( (wxStrlen(upLabel) > 0) && !PrimaryAnchorOfTheFile(upFilename, upLabel) )
       wxSnprintf(buf, sizeof(buf),
                  _T("<A HREF=\"%s#%s\">%s</A> "),
                  ConvertCase(upFilename), upLabel, upReference);
@@ -501,9 +505,14 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
 
   if (previousLabel && previousFilename)
   {
-    wxSnprintf(buf, sizeof(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>"));
@@ -550,9 +559,14 @@ void AddBrowseButtons(wxChar *upLabel, wxChar *upFilename,
 
   if (nextLabel && nextFilename)
   {
-    wxSnprintf(buf, sizeof(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
@@ -764,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"));
       }
@@ -831,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();
@@ -874,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);
@@ -885,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);
@@ -910,7 +942,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
             if (htmlWorkshopFiles) HTMLWorkshopAddToContents(2, topicName, SubsectionsName);
             SetCurrentOutput(Subsections);
 
-           HTMLHead();
+            HTMLHead();
             TexOutput(_T("<title>"));
             OutputCurrentSection();
             TexOutput(_T("</title></head>\n"));
@@ -922,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();
@@ -991,7 +1026,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
             if (htmlWorkshopFiles) HTMLWorkshopAddToContents(3, topicName, SubsubsectionsName);
 
             SetCurrentOutput(Subsubsections);
-           HTMLHead();
+            HTMLHead();
             TexOutput(_T("<title>"));
             OutputCurrentSection();
             TexOutput(_T("</title></head>\n"));
@@ -1004,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();
@@ -1133,7 +1171,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
         else
           wxSnprintf(buf, sizeof(buf), _T("\n<TD ALIGN=LEFT>"));
         TexOutput(buf);
-               OutputFont();
+        OutputFont();
       }
       else
         TexOutput(_T("&amp;"));
@@ -1182,7 +1220,7 @@ void HTMLOnMacro(int macroId, int no_args, bool start)
       else
         wxSnprintf(buf, sizeof(buf), _T("<TR>\n<TD ALIGN=LEFT>"));
       TexOutput(buf);
-         OutputFont();
+      OutputFont();
     }
     else
     {
@@ -1649,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
       {
@@ -1971,16 +2015,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);
@@ -2059,7 +2107,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("\">"));
@@ -2068,17 +2116,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
         {
@@ -2195,27 +2243,36 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     {
       if ( start ) {
         // DHS
-       if (TwoColWidthA > -1) {
+        if (TwoColWidthA > -1)
+        {
           wxChar buf[100];
           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];
           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"));
     }
@@ -2701,7 +2758,7 @@ 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++)
           {
@@ -2754,7 +2811,10 @@ 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"));
@@ -2797,7 +2857,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
           {
             wxStrcpy(buf2, _T("#000000"));
             wxChar buf[100];
-                       wxSnprintf(buf, sizeof(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=\""));
@@ -3014,15 +3074,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
@@ -3049,6 +3109,7 @@ bool HTMLGo(void)
 
       // Output <BODY...> to temporary title page
       OutputBodyStart();
+      fflush(tmpTitle);
 
       // Concat titlepage
       FILE *fd = wxFopen(TitlepageName, _T("r"));
@@ -3057,7 +3118,7 @@ bool HTMLGo(void)
         int ch = getc(fd);
         while (ch != EOF)
         {
-          putc(ch, tmpTitle);
+          wxPutc(ch, tmpTitle);
           ch = getc(fd);
         }
         fclose(fd);
@@ -3123,7 +3184,7 @@ void GenerateHTMLIndexFile(wxChar *fname)
     return;
 
   TopicTable.BeginFind();
-  wxNode *node = TopicTable.Next();
+  wxHashTable::Node *node = TopicTable.Next();
   while (node)
   {
     TexTopic *texTopic = (TexTopic *)node->GetData();
@@ -3223,7 +3284,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();
@@ -3239,7 +3300,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();
       }
     }
@@ -3271,10 +3332,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;
 }
@@ -3311,7 +3372,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 );
 }