Simplify treatment of the copyright notice, now I've also simplified
[sgt/halibut] / bk_xhtml.c
index edab934..745cce5 100644 (file)
@@ -94,11 +94,11 @@ static void xhtml_utostr(wchar_t *, char **);
 static int xhtml_para_level(paragraph *);
 static int xhtml_reservedchar(int);
 
-static int xhtml_convert(wchar_t *, char **, int);
-static void xhtml_rdaddwc(rdstringc *, word *, word *);
-static void xhtml_para(FILE *, word *);
+static int xhtml_convert(wchar_t *, int, char **, int);
+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
@@ -199,7 +199,7 @@ static xhtmlconfig xhtml_configure(paragraph *source)
       } else if (!ustricmp(source->keyword, L"xhtml-chapter-numeric")) {
        ret.fchapter.just_numbers = utob(uadv(source->keyword));
       } else if (!ustricmp(source->keyword, L"xhtml-chapter-suffix")) {
-       ret.fchapter.number_suffix = uadv(source->keyword);
+       ret.fchapter.number_suffix = ustrdup(uadv(source->keyword));
       } else if (!ustricmp(source->keyword, L"xhtml-section-numeric")) {
        wchar_t *p = uadv(source->keyword);
        int n = 0;
@@ -229,7 +229,7 @@ static xhtmlconfig xhtml_configure(paragraph *source)
            ret.fsect[i] = ret.fsect[ret.nfsect-1];
          ret.nfsect = n+1;
        }
-       ret.fsect[n].number_suffix = p;
+       ret.fsect[n].number_suffix = ustrdup(p);
       }
     }
   }
@@ -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);
+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);
@@ -767,7 +767,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 +776,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,28 +867,23 @@ 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_Preamble)
-    {
-      fprintf(fp, "<p>");
-      xhtml_para(fp, p->words);
-      fprintf(fp, "</p>\n");
-    }
-  }
-  for (p = sourceform; p; p = p->next)
-  {
-    if (p->type == para_Copyright)
-    {
-      fprintf(fp, "<p>");
-      xhtml_para(fp, p->words);
-      fprintf(fp, "</p>\n");
+    if (p->type == para_Chapter || p->type == para_Heading ||
+       p->type == para_Subsect || p->type == para_Appendix ||
+       p->type == para_UnnumberedChapter) {
+       /*
+        * We've found the end of the preamble. Do every normal
+        * paragraph up to there.
+        */
+       xhtml_do_paras(fp, sourceform, p, FALSE);
+       break;
     }
   }
 
@@ -1047,13 +1042,13 @@ static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit)
   }
   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");
   return TRUE;
@@ -1067,23 +1062,25 @@ static void xhtml_do_sections(FILE *fp, xhtmlsection *sections)
 {
   while (sections) {
     currentsection = sections;
-    xhtml_do_paras(fp, sections->para);
+    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)
+static void xhtml_do_paras(FILE *fp, paragraph *p, paragraph *end,
+                          int indexable)
 {
-  int last_type = -1, first=TRUE;
+  int last_type = -1, ptype, first=TRUE;
+  stack lcont_stack = stk_new();
   if (!p)
     return;
 
 /*  for (; p && (xhtml_para_level(p)>limit || xhtml_para_level(p)==-1 || first); p=p->next) {*/
-  for (; p && (xhtml_para_level(p)==-1 || first); p=p->next) {
+  for (; p && p != end && (xhtml_para_level(p)==-1 || first); p=p->next) {
     first=FALSE;
-    switch (p->type)
+    switch (ptype = p->type)
     {
       /*
        * Things we ignore because we've already processed them or
@@ -1093,8 +1090,6 @@ static void xhtml_do_paras(FILE *fp, paragraph *p)
      case para_BR:
      case para_Biblio:                /* only touch BiblioCited */
      case para_VersionID:
-     case para_Copyright:
-     case para_Preamble:
      case para_NoCite:
      case para_Title:
        break;
@@ -1105,12 +1100,12 @@ static void xhtml_do_paras(FILE *fp, paragraph *p)
       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:
@@ -1118,13 +1113,41 @@ static void xhtml_do_paras(FILE *fp, paragraph *p)
         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;
 
+      case para_LcontPush:
+       {
+           int *p;
+           p = mknew(int);
+           *p = last_type;
+           stk_push(lcont_stack, p);
+           last_type = para_Normal;
+       }
+       break;
+      case para_LcontPop:
+       {
+           int *p = stk_pop(lcont_stack);
+           assert(p);
+           ptype = last_type = *p;
+           sfree(p);
+           goto closeofflist;         /* ick */
+       }
+       break;
+      case para_QuotePush:
+       fprintf(fp, "<blockquote>\n");
+       break;
+      case para_QuotePop:
+       fprintf(fp, "</blockquote>\n");
+       break;
+
       case para_Bullet:
       case para_NumberedList:
+      case para_Description:
+      case para_DescribedThing:
       case para_BiblioCited:
         if (last_type!=p->type) {
           /* start up list if necessary */
@@ -1132,41 +1155,62 @@ static void xhtml_do_paras(FILE *fp, paragraph *p)
             fprintf(fp, "<ul>\n");
           } else if (p->type == para_NumberedList) {
             fprintf(fp, "<ol>\n");
-          } else if (p->type == para_BiblioCited) {
+          } else if (p->type == para_BiblioCited ||
+                    p->type == para_DescribedThing ||
+                    p->type == para_Description) {
             fprintf(fp, "<dl>\n");
           }
         }
-        if (p->type == para_Bullet || p->type == para_NumberedList)
+        if (p->type == para_Bullet || p->type == para_NumberedList) {
           fprintf(fp, "<li>");
-        else if (p->type == para_BiblioCited) {
+       } else if (p->type == para_DescribedThing) {
+          fprintf(fp, "<dt>");
+       } else if (p->type == para_Description) {
+          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);
-        if (p->type == para_BiblioCited) {
+        xhtml_para(fp, p->words, indexable);
+       {
+          paragraph *p2 = p->next;
+          if (p2 && xhtml_para_level(p2)==-1 && p2->type == para_LcontPush)
+           break;
+       }
+
+       closeofflist:
+        if (ptype == para_BiblioCited) {
           fprintf(fp, "</dd>\n");
-        } else if (p->type == para_Bullet || p->type == para_NumberedList) {
+       } else if (p->type == para_DescribedThing) {
+          fprintf(fp, "</dt>");
+       } else if (p->type == para_Description) {
+          fprintf(fp, "</dd>");
+        } else if (ptype == para_Bullet || ptype == para_NumberedList) {
           fprintf(fp, "</li>");
         }
-        if (p->type == para_Bullet || p->type == para_NumberedList || p->type == para_BiblioCited)
+        if (ptype == para_Bullet || ptype == para_NumberedList ||
+           ptype == para_BiblioCited || ptype == para_Description ||
+           ptype == para_DescribedThing)
           /* close off list if necessary */
         {
           paragraph *p2 = p->next;
           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 != p->type)
+            if (p2->type != ptype && p2->type != para_LcontPush)
               close_off=TRUE;
           } else {
             close_off=TRUE;
           }
           if (close_off) {
-            if (p->type == para_Bullet) {
+            if (ptype == para_Bullet) {
               fprintf(fp, "</ul>\n");
-            } else if (p->type == para_NumberedList) {
+            } else if (ptype == para_NumberedList) {
               fprintf(fp, "</ol>\n");
-            } else if (p->type == para_BiblioCited) {
+            } else if (ptype == para_BiblioCited ||
+                      ptype == para_Description ||
+                      ptype == para_DescribedThing) {
               fprintf(fp, "</dl>\n");
             }
           }
@@ -1177,8 +1221,10 @@ static void xhtml_do_paras(FILE *fp, paragraph *p)
         xhtml_codepara(fp, p->words);
         break;
     }
-    last_type = p->type;
+    last_type = ptype;
   }
+
+  stk_free(lcont_stack);
 }
 
 /*
@@ -1192,7 +1238,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)
@@ -1248,7 +1294,7 @@ 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)
@@ -1286,13 +1332,17 @@ static int xhtml_reservedchar(int c)
  * characters are OK but `result' is non-NULL, a result _will_
  * still be generated!
  */
-static int xhtml_convert(wchar_t *s, char **result, int hard_spaces) {
+static int xhtml_convert(wchar_t *s, int maxlen, char **result,
+                        int hard_spaces) {
     int doing = (result != 0);
     int ok = TRUE;
     char *p = NULL;
     int plen = 0, psize = 0;
 
-    for (; *s; s++) {
+    if (maxlen <= 0)
+       maxlen = -1;
+
+    for (; *s && maxlen != 0; s++, maxlen--) {
        wchar_t c = *s;
 
 #define ensure_size(i) if (i>=psize) { psize = i+256; p = resize(p, psize); }
@@ -1334,8 +1384,14 @@ static int xhtml_convert(wchar_t *s, char **result, int hard_spaces) {
 
 /*
  * 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;
@@ -1382,6 +1438,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) {
@@ -1459,10 +1518,10 @@ static void xhtml_rdaddwc(rdstringc *rs, word *text, word *end) {
            rdaddsc(rs, "<code>");
 
        if (removeattr(text->type) == word_Normal) {
-         if (xhtml_convert(text->text, &c, TRUE)) /* spaces in the word are hard */
+         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, ' ');
@@ -1485,7 +1544,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;
@@ -1516,25 +1575,25 @@ 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, &c, FALSE)) {
+           if (xhtml_convert(fmt->number_suffix, 0, &c, FALSE)) {
                rdaddsc(&t, c);
                sfree(c);
            }
        }
     } 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, &c, FALSE)) {
+           if (xhtml_convert(fmt->number_suffix, 0, &c, FALSE)) {
                rdaddsc(&t, c);
                sfree(c);
            }
        }
     }
-    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
@@ -1554,10 +1613,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);
 }
@@ -1571,10 +1630,49 @@ static void xhtml_codepara(FILE *fp, word *text)
 {
   fprintf(fp, "<pre>");
     for (; text; text = text->next) if (text->type == word_WeakCode) {
+       word *here, *next;
        char *c;
-       xhtml_convert(text->text, &c, FALSE);
-       fprintf(fp, "%s\n", c);
-       sfree(c);
+
+       /*
+        * See if this WeakCode is followed by an Emph to indicate
+        * emphasis.
+        */
+       here = text;
+       if (text->next && text->next->type == word_Emph) {
+           next = text = text->next;
+       } else
+           next = NULL;
+
+       if (next) {
+           wchar_t *t, *e;
+           int n;
+
+           t = here->text;
+           e = next->text;
+
+           while (*e) {
+               int ec = *e;
+
+               for (n = 0; t[n] && e[n] && e[n] == ec; n++);
+               xhtml_convert(t, n, &c, FALSE);
+               fprintf(fp, "%s%s%s",
+                       (ec == 'i' ? "<em>" : ec == 'b' ? "<b>" : ""),
+                       c,
+                       (ec == 'i' ? "</em>" : ec == 'b' ? "</b>" : ""));
+               sfree(c);
+
+               t += n;
+               e += n;
+           }
+
+           xhtml_convert(t, 0, &c, FALSE);
+           fprintf(fp, "%s\n", c);
+           sfree(c);
+       } else {
+           xhtml_convert(here->text, 0, &c, FALSE);
+           fprintf(fp, "%s\n", c);
+           sfree(c);
+       }
     }
   fprintf(fp, "</pre>\n");
 }