]> git.saurik.com Git - wxWidgets.git/blobdiff - utils/tex2rtf/src/htmlutil.cpp
Third part of patch #1178276 and game improvements.
[wxWidgets.git] / utils / tex2rtf / src / htmlutil.cpp
index 2ef296d460b396cfb4cfbc920cc8dd50a6dfa6f6..ec8fdfb658ddc0d8860c08b6391bc45f65b5f2ed 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$
@@ -29,6 +29,8 @@
 #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)
@@ -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();
@@ -924,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();
@@ -1006,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();
@@ -1765,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)
 {
@@ -1791,8 +1894,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> "));
@@ -1853,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>"));
@@ -1944,6 +2046,7 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
     }
     break;
   }
+
   case ltHELPREF:
   case ltHELPREFN:
   case ltPOPREF:
@@ -1982,13 +2085,17 @@ bool HTMLOnArgument(int macroId, int arg_no, bool start)
             {
               TraverseChildrenFromChunk(helpRefFilename);
               TexOutput(_T("#"));
+              TexOutput(refName);
             }
             else if (refFilename)
             {
               TexOutput(ConvertCase(refFilename));
-              TexOutput(_T("#"));
+              if(!PrimaryAnchorOfTheFile(texRef->refFile, refName))
+              {
+                TexOutput(_T("#"));
+                TexOutput(refName);
+              }
             }
-            TexOutput(refName);
             TexOutput(_T("\">"));
             if (helpRefText)
               TraverseChildrenFromChunk(helpRefText);
@@ -2771,7 +2878,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"));
@@ -3329,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 );
 }