Obsoleted the `\preamble' command. Preamble text is now taken to be
[sgt/halibut] / bk_xhtml.c
index 18a7409..b2bed07 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <assert.h>
 #include "halibut.h"
 
@@ -62,6 +63,11 @@ struct xhtmlindex_Struct {
 };
 
 typedef struct {
+    int just_numbers;
+    wchar_t *number_suffix;
+} xhtmlheadfmt;
+
+typedef struct {
   int contents_depth[6];
   int leaf_contains_contents;
   int leaf_level;
@@ -70,6 +76,8 @@ typedef struct {
   wchar_t *author, *description;
   wchar_t *head_end, *body, *body_start, *body_end, *address_start, *address_end, *nav_attrs;
   int suppress_address;
+  xhtmlheadfmt fchapter, *fsect;
+  int nfsect;
 } xhtmlconfig;
 
 /*static void xhtml_level(paragraph *, int);
@@ -86,7 +94,7 @@ 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 int xhtml_convert(wchar_t *, int, char **, int);
 static void xhtml_rdaddwc(rdstringc *, word *, word *);
 static void xhtml_para(FILE *, word *);
 static void xhtml_codepara(FILE *, word *);
@@ -135,6 +143,15 @@ static xhtmlconfig xhtml_configure(paragraph *source)
   ret.nav_attrs = NULL;
   ret.suppress_address = FALSE;
 
+  ret.fchapter.just_numbers = FALSE;
+  ret.fchapter.number_suffix = ustrdup(L": ");
+  ret.nfsect = 2;
+  ret.fsect = mknewa(xhtmlheadfmt, ret.nfsect);
+  ret.fsect[0].just_numbers = FALSE;
+  ret.fsect[0].number_suffix = ustrdup(L": ");
+  ret.fsect[1].just_numbers = TRUE;
+  ret.fsect[1].number_suffix = ustrdup(L" ");
+
   for (; source; source = source->next)
   {
     if (source->type == para_Config)
@@ -179,6 +196,40 @@ static xhtmlconfig xhtml_configure(paragraph *source)
         ret.address_end = uadv(source->keyword);
       } else if (!ustricmp(source->keyword, L"xhtml-navigation-attributes")) {
         ret.nav_attrs = uadv(source->keyword);
+      } 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 = ustrdup(uadv(source->keyword));
+      } else if (!ustricmp(source->keyword, L"xhtml-section-numeric")) {
+       wchar_t *p = uadv(source->keyword);
+       int n = 0;
+       if (uisdigit(*p)) {
+         n = utoi(p);
+         p = uadv(p);
+       }
+       if (n >= ret.nfsect) {
+         int i;
+         ret.fsect = resize(ret.fsect, n+1);
+         for (i = ret.nfsect; i <= n; i++)
+           ret.fsect[i] = ret.fsect[ret.nfsect-1];
+         ret.nfsect = n+1;
+       }
+       ret.fsect[n].just_numbers = utob(p);
+      } else if (!ustricmp(source->keyword, L"xhtml-section-suffix")) {
+       wchar_t *p = uadv(source->keyword);
+       int n = 0;
+       if (uisdigit(*p)) {
+         n = utoi(p);
+         p = uadv(p);
+       }
+       if (n >= ret.nfsect) {
+         int i;
+         ret.fsect = resize(ret.fsect, n+1);
+         for (i = ret.nfsect; i <= n; i++)
+           ret.fsect[i] = ret.fsect[ret.nfsect-1];
+         ret.nfsect = n+1;
+       }
+       ret.fsect[n].number_suffix = ustrdup(p);
       }
     }
   }
@@ -308,7 +359,7 @@ void xhtml_fixup_layout(xhtmlfile* file)
  * |                 |                 |
  * X            +----X----+           (1)
  *              |         |
- *              Y        (3)
+ *              Y        (2)
  *              |
  *             (3)
  *
@@ -529,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);
 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);
@@ -625,16 +676,25 @@ void xhtml_backend(paragraph *sourceform, keywordlist *in_keywords,
     }
     ientry->backend_data = NULL;
   }
+  {
+    int i;
+    sfree(conf.fchapter.number_suffix);
+    for (i = 0; i < conf.nfsect; i++)
+      sfree(conf.fsect[i].number_suffix);
+    sfree(conf.fsect);
+  }
 }
 
 static int xhtml_para_level(paragraph *p)
 {
   switch (p->type)
   {
+  case para_Title:
+    return 0;
+    break;
   case para_UnnumberedChapter:
   case para_Chapter:
   case para_Appendix:
-  case para_Title:
     return 1;
     break;
 /*  case para_BiblioCited:
@@ -815,11 +875,15 @@ static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform)
   /* Do the preamble and copyright */
   for (p = sourceform; p; p = p->next)
   {
-    if (p->type == para_Preamble)
-    {
-      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);
+       break;
     }
   }
   for (p = sourceform; p; p = p->next)
@@ -835,7 +899,11 @@ static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform)
   xhtml_do_contents(fp, file);
   xhtml_do_sections(fp, file->sections);
 
-  if (count234(idx->entries) > 0) {
+  /*
+   * Put the index in the top file if we're in single-file mode
+   * (leaf-level 0).
+   */
+  if (conf.leaf_level == 0 && count234(idx->entries) > 0) {
     fprintf(fp, "<a name=\"index\"></a><h1>Index</h1>\n");
     xhtml_do_index_body(fp);
   }
@@ -971,7 +1039,7 @@ static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit)
 {
   if (!section || section->level > limit)
     return FALSE;
-  if (fp==NULL || !section->parent)
+  if (fp==NULL || section->level < 0)
     return TRUE;
   while (last_level > section->level) {
     last_level--;
@@ -1003,23 +1071,24 @@ 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);
     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 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
@@ -1030,7 +1099,6 @@ static void xhtml_do_paras(FILE *fp, paragraph *p)
      case para_Biblio:                /* only touch BiblioCited */
      case para_VersionID:
      case para_Copyright:
-     case para_Preamble:
      case para_NoCite:
      case para_Title:
        break;
@@ -1059,8 +1127,35 @@ static void xhtml_do_paras(FILE *fp, paragraph *p)
         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 */
@@ -1068,41 +1163,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);
           fprintf(fp, "</dt>\n<dd>");
         }
         xhtml_para(fp, p->words);
-        if (p->type == para_BiblioCited) {
+       {
+          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");
             }
           }
@@ -1113,8 +1229,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);
 }
 
 /*
@@ -1222,13 +1340,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); }
@@ -1395,7 +1517,7 @@ 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);
@@ -1429,6 +1551,7 @@ static void xhtml_heading(FILE *fp, paragraph *p)
     word *text = p->words;
     int level = xhtml_para_level(p);
     xhtmlsection *sect = xhtml_find_section(p);
+    xhtmlheadfmt *fmt;
     char *fragment;
     if (sect) {
       fragment = sect->fragment;
@@ -1441,14 +1564,47 @@ static void xhtml_heading(FILE *fp, paragraph *p)
       }
     }
 
-    if (level>2 && nprefix) { /* FIXME: configurability on the level thing */
+    if (p->type == para_Title)
+       fmt = NULL;
+    else if (level == 1)
+       fmt = &conf.fchapter;
+    else if (level-1 < conf.nfsect)
+       fmt = &conf.fsect[level-1];
+    else
+       fmt = &conf.fsect[conf.nfsect-1];
+
+    if (fmt && fmt->just_numbers && nprefix) {
        xhtml_rdaddwc(&t, nprefix, NULL);
-       rdaddc(&t, ' ');               /* FIXME: as below */
-    } else if (tprefix) {
+       if (fmt) {
+           char *c;
+           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);
-       rdaddsc(&t, ": ");             /* FIXME: configurability */
+       if (fmt) {
+           char *c;
+           if (xhtml_convert(fmt->number_suffix, 0, &c, FALSE)) {
+               rdaddsc(&t, c);
+               sfree(c);
+           }
+       }
     }
     xhtml_rdaddwc(&t, text, NULL);
+    /*
+     * If we're outputting in single-file mode, we need to lower
+     * the level of each heading by one, because the overall
+     * document title will be sitting right at the top as an <h1>
+     * and so chapters and sections should start at <h2>.
+     * 
+     * Even if not, the document title will come back from
+     * xhtml_para_level() as level zero, so we must increment that
+     * no matter what leaf_level is set to.
+     */
+    if (conf.leaf_level == 0 || level == 0)
+       level++;
     fprintf(fp, "<a name=\"%s\"></a><h%i>%s</h%i>\n", fragment, level, t.text, level);
     sfree(t.text);
 }
@@ -1473,10 +1629,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");
 }