After so much fuss was made about this being an _XHTML_ back end
[sgt/halibut] / bk_xhtml.c
index b2bed07..02e2a32 100644 (file)
@@ -95,10 +95,10 @@ static int xhtml_para_level(paragraph *);
 static int xhtml_reservedchar(int);
 
 static int xhtml_convert(wchar_t *, int, char **, int);
-static void xhtml_rdaddwc(rdstringc *, word *, word *);
-static void xhtml_para(FILE *, word *);
+static void xhtml_rdaddwc(rdstringc *, word *, word *, int);
+static void xhtml_para(FILE *, word *, int);
 static void xhtml_codepara(FILE *, word *);
-static void xhtml_heading(FILE *, paragraph *);
+static void xhtml_heading(FILE *, paragraph *, int);
 
 /* File-global variables are much easier than passing these things
  * all over the place. Evil, but easier. We can replace this with a single
@@ -112,7 +112,7 @@ static xhtmlsection *topsection;
 static paragraph *sourceparas;
 static xhtmlfile *lastfile;
 static xhtmlfile *xhtml_last_file = NULL;
-static int last_level=-1;
+static int last_level=-1, start_level;
 static xhtmlsection *currentsection;
 
 static xhtmlconfig xhtml_configure(paragraph *source)
@@ -580,7 +580,7 @@ static void xhtml_ponder_layout(paragraph *p)
 static void xhtml_do_index();
 static void xhtml_do_file(xhtmlfile *file);
 static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform);
-static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end);
+static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end, int indexable);
 static int xhtml_do_contents_limit(FILE *fp, xhtmlfile *file, int limit);
 static int xhtml_do_contents_section_limit(FILE *fp, xhtmlsection *section, int limit);
 static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit);
@@ -730,6 +730,11 @@ static void xhtml_donavlinks(FILE *fp, xhtmlfile *file)
     fprintf(fp, "<a href='%s'>Previous</a> | ", xhtml_last_file->filename);
   }
   fprintf(fp, "<a href='Contents.html'>Contents</a> | ");
+  if (file == NULL) {
+    fprintf(fp, "Index | ");
+  } else {
+    fprintf(fp, "<a href='%s'>Index</a> | ", xhtml_index_filename);
+  }
   if (file != NULL) { /* otherwise we're doing nav links for the index */
     if (xhtml_next_file==NULL)
       xhtml_next_file = file->child;
@@ -767,7 +772,7 @@ static void xhtml_do_index_body(FILE *fp)
       xhtmlindex *xi;
 
       fprintf(fp, "<dt>");
-      xhtml_para(fp, y->text);
+      xhtml_para(fp, y->text, FALSE);
       fprintf(fp, "</dt>\n<dd>");
 
       xi = (xhtmlindex*) y->backend_data;
@@ -776,9 +781,9 @@ static void xhtml_do_index_body(FILE *fp)
        if (sect) {
          fprintf(fp, "<a href='%s#%s'>", sect->file->filename, sect->fragment);
          if (sect->para->kwtext) {
-           xhtml_para(fp, sect->para->kwtext);
+           xhtml_para(fp, sect->para->kwtext, FALSE);
          } else if (sect->para->words) {
-           xhtml_para(fp, sect->para->words);
+           xhtml_para(fp, sect->para->words, FALSE);
          }
          fprintf(fp, "</a>");
          if (i+1<xi->nsection) {
@@ -867,12 +872,12 @@ static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform)
   for (p = sourceform; p; p = p->next)
   {
     if (p->type == para_Title) {
-      xhtml_heading(fp, p);
+      xhtml_heading(fp, p, FALSE);
       break;
     }
   }
 
-  /* Do the preamble and copyright */
+  /* Do the preamble */
   for (p = sourceform; p; p = p->next)
   {
     if (p->type == para_Chapter || p->type == para_Heading ||
@@ -882,19 +887,10 @@ static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform)
         * We've found the end of the preamble. Do every normal
         * paragraph up to there.
         */
-       xhtml_do_paras(fp, sourceform, p);
+       xhtml_do_paras(fp, sourceform, p, FALSE);
        break;
     }
   }
-  for (p = sourceform; p; p = p->next)
-  {
-    if (p->type == para_Copyright)
-    {
-      fprintf(fp, "<p>");
-      xhtml_para(fp, p->words);
-      fprintf(fp, "</p>\n");
-    }
-  }
 
   xhtml_do_contents(fp, file);
   xhtml_do_sections(fp, file->sections);
@@ -937,7 +933,7 @@ static void xhtml_utostr(wchar_t *in, char **out)
  */
 static int xhtml_do_contents(FILE *fp, xhtmlfile *file)
 {
-  int level, limit, start_level, count = 0;
+  int level, limit, count = 0;
   if (!file)
     return 0;
 
@@ -951,7 +947,7 @@ static int xhtml_do_contents(FILE *fp, xhtmlfile *file)
   if (fp!=NULL) {
     while (last_level > start_level) {
       last_level--;
-      fprintf(fp, "</ul>\n");
+      fprintf(fp, "</li></ul>\n");
     }
   }
   return count;
@@ -973,7 +969,7 @@ static int xhtml_do_naked_contents(FILE *fp, xhtmlfile *file)
   if (fp!=NULL) {
     while (last_level > start_level) {
       last_level--;
-      fprintf(fp, "</ul>\n");
+      fprintf(fp, "</li></ul>\n");
     }
   }
   return count;
@@ -1041,25 +1037,30 @@ static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit)
     return FALSE;
   if (fp==NULL || section->level < 0)
     return TRUE;
-  while (last_level > section->level) {
-    last_level--;
-    fprintf(fp, "</ul>\n");
-  }
-  while (last_level < section->level) {
+  if (last_level > section->level) {
+    while (last_level > section->level) {
+      last_level--;
+      fprintf(fp, "</li></ul>\n");
+    }
+    fprintf(fp, "</li>\n");
+  } else if (last_level < section->level) {
+    assert(last_level == section->level - 1);
     last_level++;
     fprintf(fp, "<ul>\n");
+  } else {
+    fprintf(fp, "</li>\n");
   }
   fprintf(fp, "<li><a href=\"%s#%s\">", section->file->filename, section->fragment);
   if (section->para->kwtext) {
-    xhtml_para(fp, section->para->kwtext);
+    xhtml_para(fp, section->para->kwtext, FALSE);
     if (section->para->words) {
       fprintf(fp, ": ");
     }
   }
   if (section->para->words) {
-    xhtml_para(fp, section->para->words);
+    xhtml_para(fp, section->para->words, FALSE);
   }
-  fprintf(fp, "</a></li>\n");
+  fprintf(fp, "</a>\n");
   return TRUE;
 }
 
@@ -1071,14 +1072,15 @@ static void xhtml_do_sections(FILE *fp, xhtmlsection *sections)
 {
   while (sections) {
     currentsection = sections;
-    xhtml_do_paras(fp, sections->para, NULL);
+    xhtml_do_paras(fp, sections->para, NULL, TRUE);
     xhtml_do_sections(fp, sections->child);
     sections = sections->next;
   }
 }
 
 /* Write this list of paragraphs. Close off all lists at the end. */
-static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end)
+static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end,
+                          int indexable)
 {
   int last_type = -1, ptype, first=TRUE;
   stack lcont_stack = stk_new();
@@ -1098,7 +1100,6 @@ static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end)
      case para_BR:
      case para_Biblio:                /* only touch BiblioCited */
      case para_VersionID:
-     case para_Copyright:
      case para_NoCite:
      case para_Title:
        break;
@@ -1109,12 +1110,12 @@ static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end)
       case para_Chapter:
       case para_Appendix:
       case para_UnnumberedChapter:
-        xhtml_heading(fp, p);
+        xhtml_heading(fp, p, indexable);
         break;
 
       case para_Heading:
       case para_Subsect:
-        xhtml_heading(fp, p);
+        xhtml_heading(fp, p, indexable);
         break;
 
       case para_Rule:
@@ -1122,8 +1123,9 @@ static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end)
         break;
 
       case para_Normal:
+      case para_Copyright:
         fprintf(fp, "\n<p>");
-        xhtml_para(fp, p->words);
+        xhtml_para(fp, p->words, indexable);
         fprintf(fp, "</p>\n");
         break;
 
@@ -1157,7 +1159,9 @@ static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end)
       case para_Description:
       case para_DescribedThing:
       case para_BiblioCited:
-        if (last_type!=p->type) {
+        if (last_type!=p->type &&
+           !(last_type==para_DescribedThing && p->type==para_Description) &&
+           !(last_type==para_Description && p->type==para_DescribedThing)) {
           /* start up list if necessary */
           if (p->type == para_Bullet) {
             fprintf(fp, "<ul>\n");
@@ -1177,10 +1181,10 @@ static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end)
           fprintf(fp, "<dd>");
        } else if (p->type == para_BiblioCited) {
           fprintf(fp, "<dt>");
-          xhtml_para(fp, p->kwtext);
+          xhtml_para(fp, p->kwtext, indexable);
           fprintf(fp, "</dt>\n<dd>");
         }
-        xhtml_para(fp, p->words);
+        xhtml_para(fp, p->words, indexable);
        {
           paragraph *p2 = p->next;
           if (p2 && xhtml_para_level(p2)==-1 && p2->type == para_LcontPush)
@@ -1190,9 +1194,9 @@ static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end)
        closeofflist:
         if (ptype == para_BiblioCited) {
           fprintf(fp, "</dd>\n");
-       } else if (p->type == para_DescribedThing) {
+       } else if (ptype == para_DescribedThing) {
           fprintf(fp, "</dt>");
-       } else if (p->type == para_Description) {
+       } else if (ptype == para_Description) {
           fprintf(fp, "</dd>");
         } else if (ptype == para_Bullet || ptype == para_NumberedList) {
           fprintf(fp, "</li>");
@@ -1206,7 +1210,10 @@ static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end)
           int close_off=FALSE;
 /*          if (p2 && (xhtml_para_level(p2)>limit || xhtml_para_level(p2)==-1)) {*/
           if (p2 && xhtml_para_level(p2)==-1) {
-            if (p2->type != ptype && p2->type != para_LcontPush)
+            if (p2->type != ptype &&
+               !(p2->type==para_DescribedThing && ptype==para_Description) &&
+               !(p2->type==para_Description && ptype==para_DescribedThing) &&
+               p2->type != para_LcontPush)
               close_off=TRUE;
           } else {
             close_off=TRUE;
@@ -1246,7 +1253,7 @@ static void xhtml_doheader(FILE *fp, word *title)
   if (title==NULL)
     fprintf(fp, "The thing with no name!");
   else
-    xhtml_para(fp, title);
+    xhtml_para(fp, title, FALSE);
   fprintf(fp, "</title>\n");
   fprintf(fp, "<meta name=\"generator\" content=\"Halibut %s xhtml-backend\" />\n", version);
   if (conf.author)
@@ -1302,11 +1309,11 @@ static void xhtml_versionid(FILE *fp, word *text, int started)
   rdstringc t = { 0, 0, NULL };
 
   rdaddc(&t, '[');                    /* FIXME: configurability */
-  xhtml_rdaddwc(&t, text, NULL);
+  xhtml_rdaddwc(&t, text, NULL, FALSE);
   rdaddc(&t, ']');                    /* FIXME: configurability */
 
   if (started)
-    fprintf(fp, "<br>\n");
+    fprintf(fp, "<br />\n");
   fprintf(fp, "%s\n", t.text);
   sfree(t.text);
 }
@@ -1392,8 +1399,14 @@ static int xhtml_convert(wchar_t *s, int maxlen, char **result,
 
 /*
  * This formats the given words as XHTML.
+ * 
+ * `indexable', if FALSE, prohibits adding any index references.
+ * You might use this, for example, if an index reference occurred
+ * in a section title, to prevent phony index references when the
+ * section title is processed in strange places such as contents
+ * sections.
  */
-static void xhtml_rdaddwc(rdstringc *rs, word *text, word *end) {
+static void xhtml_rdaddwc(rdstringc *rs, word *text, word *end, int indexable) {
     char *c;
     keyword *kwl;
     xhtmlsection *sect;
@@ -1440,6 +1453,9 @@ static void xhtml_rdaddwc(rdstringc *rs, word *text, word *end) {
        /* what we _do_ need to do is to fix up the backend data
         * for any indexentry this points to.
         */
+       if (!indexable)
+         break;
+
        for (ti=0; (itag = (indextag *)index234(idx->tags, ti))!=NULL; ti++) {
          /* FIXME: really ustricmp() and not ustrcmp()? */
          if (ustricmp(itag->name, text->text)==0) {
@@ -1520,7 +1536,7 @@ static void xhtml_rdaddwc(rdstringc *rs, word *text, word *end) {
          if (xhtml_convert(text->text, 0, &c, TRUE)) /* spaces in the word are hard */
            rdaddsc(rs, c);
          else
-           xhtml_rdaddwc(rs, text->alt, NULL);
+           xhtml_rdaddwc(rs, text->alt, NULL, indexable);
          sfree(c);
        } else if (removeattr(text->type) == word_WhiteSpace) {
          rdaddc(rs, ' ');
@@ -1543,7 +1559,7 @@ static void xhtml_rdaddwc(rdstringc *rs, word *text, word *end) {
 
 /* Output a heading, formatted as XHTML.
  */
-static void xhtml_heading(FILE *fp, paragraph *p)
+static void xhtml_heading(FILE *fp, paragraph *p, int indexable)
 {
     rdstringc t = { 0, 0, NULL };
     word *tprefix = p->kwtext;
@@ -1574,7 +1590,7 @@ static void xhtml_heading(FILE *fp, paragraph *p)
        fmt = &conf.fsect[conf.nfsect-1];
 
     if (fmt && fmt->just_numbers && nprefix) {
-       xhtml_rdaddwc(&t, nprefix, NULL);
+       xhtml_rdaddwc(&t, nprefix, NULL, indexable);
        if (fmt) {
            char *c;
            if (xhtml_convert(fmt->number_suffix, 0, &c, FALSE)) {
@@ -1583,7 +1599,7 @@ static void xhtml_heading(FILE *fp, paragraph *p)
            }
        }
     } else if (fmt && !fmt->just_numbers && tprefix) {
-       xhtml_rdaddwc(&t, tprefix, NULL);
+       xhtml_rdaddwc(&t, tprefix, NULL, indexable);
        if (fmt) {
            char *c;
            if (xhtml_convert(fmt->number_suffix, 0, &c, FALSE)) {
@@ -1592,7 +1608,7 @@ static void xhtml_heading(FILE *fp, paragraph *p)
            }
        }
     }
-    xhtml_rdaddwc(&t, text, NULL);
+    xhtml_rdaddwc(&t, text, NULL, indexable);
     /*
      * If we're outputting in single-file mode, we need to lower
      * the level of each heading by one, because the overall
@@ -1612,10 +1628,10 @@ static void xhtml_heading(FILE *fp, paragraph *p)
 /* Output a paragraph. Styles are handled by xhtml_rdaddwc().
  * This looks pretty simple; I may have missed something ...
  */
-static void xhtml_para(FILE *fp, word *text)
+static void xhtml_para(FILE *fp, word *text, int indexable)
 {
   rdstringc out = { 0, 0, NULL };
-  xhtml_rdaddwc(&out, text, NULL);
+  xhtml_rdaddwc(&out, text, NULL, indexable);
   fprintf(fp, "%s", out.text);
   sfree(out.text);
 }