General configurability upgrade for the info back end.
[sgt/halibut] / bk_xhtml.c
index 2016e10..4d5069a 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <assert.h>
 #include "halibut.h"
 
+/*
+ * FILENAME_TEMPLATE (overridable in config of course) allows you
+ * to choose the general form for your HTML file names. It is
+ * slightly printf-styled (% followed by a single character is a
+ * formatting directive, %% is a literal %). Formatting directives
+ * are:
+ * 
+ *  - %n is the section type-plus-number, minus whitespace (`Chapter1.2').
+ *  - %b is the section number on its own (`1.2').
+ *  - %k is the section's _internal_ keyword.
+ *  - %N is the section's visible title in the output, again minus
+ *    whitespace.
+ * 
+ * %n, %b and %k will all default to %N if the section is
+ * unnumbered (`Bibliography' is often a good example).
+ * 
+ * FRAGMENT_TEMPLATE is the same, but defines the <a name="foo">
+ * markers used to cross-reference to particular subsections of a
+ * file.
+ */
+
+#define FILENAME_SINGLE "Manual.html"
+#define FILENAME_CONTENTS "Contents.html"
+#define FILENAME_INDEX "IndexPage.html"
+#define FILENAME_TEMPLATE "%n.html"
+#define FRAGMENT_TEMPLATE "%b"
+
 struct xhtmlsection_Struct {
     struct xhtmlsection_Struct *next; /* next sibling (NULL if split across files) */
     struct xhtmlsection_Struct *child; /* NULL if split across files */
@@ -62,6 +90,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 +103,10 @@ 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;
+  char *contents_filename, *index_filename;
+  char *single_filename, *template_filename, *template_fragment;
 } xhtmlconfig;
 
 /*static void xhtml_level(paragraph *, int);
@@ -86,11 +123,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
@@ -104,7 +141,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)
@@ -135,11 +172,40 @@ static xhtmlconfig xhtml_configure(paragraph *source)
   ret.nav_attrs = NULL;
   ret.suppress_address = FALSE;
 
+  ret.fchapter.just_numbers = FALSE;
+  ret.fchapter.number_suffix = L": ";
+  ret.nfsect = 2;
+  ret.fsect = mknewa(xhtmlheadfmt, ret.nfsect);
+  ret.fsect[0].just_numbers = FALSE;
+  ret.fsect[0].number_suffix = L": ";
+  ret.fsect[1].just_numbers = TRUE;
+  ret.fsect[1].number_suffix = L" ";
+  ret.contents_filename = strdup(FILENAME_CONTENTS);
+  ret.single_filename = strdup(FILENAME_SINGLE);
+  ret.index_filename = strdup(FILENAME_INDEX);
+  ret.template_filename = strdup(FILENAME_TEMPLATE);
+  ret.template_fragment = strdup(FRAGMENT_TEMPLATE);
+
   for (; source; source = source->next)
   {
     if (source->type == para_Config)
     {
-             if (!ustricmp(source->keyword, L"xhtml-contents-depth-0")) {
+      if (!ustricmp(source->keyword, L"xhtml-contents-filename")) {
+       sfree(ret.contents_filename);
+       ret.contents_filename = dupstr(adv(source->origkeyword));
+      } else if (!ustricmp(source->keyword, L"xhtml-single-filename")) {
+       sfree(ret.single_filename);
+       ret.single_filename = dupstr(adv(source->origkeyword));
+      } else if (!ustricmp(source->keyword, L"xhtml-index-filename")) {
+       sfree(ret.index_filename);
+       ret.index_filename = dupstr(adv(source->origkeyword));
+      } else if (!ustricmp(source->keyword, L"xhtml-template-filename")) {
+       sfree(ret.template_filename);
+       ret.template_filename = dupstr(adv(source->origkeyword));
+      } else if (!ustricmp(source->keyword, L"xhtml-template-fragment")) {
+       sfree(ret.template_fragment);
+       ret.template_fragment = utoa_dup(uadv(source->keyword), CS_ASCII);
+      } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-0")) {
         ret.contents_depth[0] = utoi(uadv(source->keyword));
       } else if (!ustricmp(source->keyword, L"xhtml-contents-depth-1")) {
         ret.contents_depth[1] = utoi(uadv(source->keyword));
@@ -153,9 +219,6 @@ static xhtmlconfig xhtml_configure(paragraph *source)
         ret.contents_depth[5] = utoi(uadv(source->keyword));
       } else if (!ustricmp(source->keyword, L"xhtml-leaf-level")) {
         ret.leaf_level = utoi(uadv(source->keyword));
-        if (ret.leaf_level==0) {
-          fatal(err_whatever, "xhtml-leaf-level cannot be zero");
-        }
       } else if (!ustricmp(source->keyword, L"xhtml-leaf-smallest-contents")) {
         ret.leaf_smallest_contents = utoi(uadv(source->keyword));
       } else if (!ustricmp(source->keyword, L"xhtml-versionid")) {
@@ -182,6 +245,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 = 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 = p;
       }
     }
   }
@@ -197,6 +294,24 @@ static xhtmlconfig xhtml_configure(paragraph *source)
   return ret;
 }
 
+paragraph *xhtml_config_filename(char *filename)
+{
+    /*
+     * If the user passes in a single filename as a parameter to
+     * the `--html' command-line option, then we should assume it
+     * to imply _two_ config directives:
+     * \cfg{xhtml-single-filename}{whatever} and
+     * \cfg{xhtml-leaf-level}{0}; the rationale being that the user
+     * wants their output _in that file_.
+     */
+    paragraph *p, *q;
+
+    p = cmdline_cfg_simple("xhtml-single-filename", filename, NULL);
+    q = cmdline_cfg_simple("xhtml-leaf-level", "0", NULL);
+    p->next = q;
+    return p;
+}
+
 static xhtmlsection *xhtml_new_section(xhtmlsection *last)
 {
   xhtmlsection *ret = mknew(xhtmlsection);
@@ -239,6 +354,62 @@ static xhtmlsection *xhtml_find_section(paragraph *p)
   return ret;
 }
 
+static void xhtml_format(paragraph *p, char *template_string, rdstringc *r)
+{
+    char *c, *t;
+    word *w;
+    wchar_t *ws;
+
+    t = template_string;
+    while (*t) {
+       if (*t == '%' && t[1]) {
+           int fmt;
+
+           t++;
+           fmt = *t++;
+
+           if (fmt == '%') {
+               rdaddc(r, fmt);
+               continue;
+           }
+
+           w = NULL;
+           ws = NULL;
+
+           if (p->kwtext && fmt == 'n')
+               w = p->kwtext;
+           else if (p->kwtext2 && fmt == 'b')
+               w = p->kwtext2;
+           else if (p->keyword && *p->keyword && fmt == 'k')
+               ws = p->keyword;
+           else
+               w = p->words;
+
+           while (w) {
+               switch (removeattr(w->type))
+               {
+                 case word_Normal:
+                   /*case word_Emph:
+                    case word_Code:
+                    case word_WeakCode:*/
+                   xhtml_utostr(w->text, &c);
+                   rdaddsc(r,c);
+                   sfree(c);
+                   break;
+               }
+               w = w->next;
+           }
+           if (ws) {
+               xhtml_utostr(ws, &c);
+               rdaddsc(r,c);
+               sfree(c);
+           }
+       } else {
+           rdaddc(r, *t++);
+       }
+    }
+}
+
 static xhtmlfile *xhtml_new_file(xhtmlsection *sect)
 {
   xhtmlfile *ret = mknew(xhtmlfile);
@@ -251,34 +422,16 @@ static xhtmlfile *xhtml_new_file(xhtmlsection *sect)
   ret->is_leaf=(sect!=NULL && sect->level==conf.leaf_level);
   if (sect==NULL) {
     if (conf.leaf_level==0) { /* currently unused */
-#define FILENAME_MANUAL "Manual.html"
-#define FILENAME_CONTENTS "Contents.html"
-      ret->filename = smalloc(strlen(FILENAME_MANUAL)+1);
-      sprintf(ret->filename, FILENAME_MANUAL);
+      ret->filename = smalloc(strlen(conf.single_filename)+1);
+      sprintf(ret->filename, conf.single_filename);
     } else {
-      ret->filename = smalloc(strlen(FILENAME_CONTENTS)+1);
-      sprintf(ret->filename, FILENAME_CONTENTS);
+      ret->filename = smalloc(strlen(conf.contents_filename)+1);
+      sprintf(ret->filename, conf.contents_filename);
     }
   } else {
     paragraph *p = sect->para;
     rdstringc fname_c = { 0, 0, NULL };
-    char *c;
-    word *w;
-    for (w=(p->kwtext)?(p->kwtext):(p->words); w; w=w->next)
-    {
-      switch (removeattr(w->type))
-      {
-      case word_Normal:
-        /*case word_Emph:
-        case word_Code:
-        case word_WeakCode:*/
-        xhtml_utostr(w->text, &c);
-        rdaddsc(&fname_c,c);
-        sfree(c);
-        break;
-      }
-    }
-    rdaddsc(&fname_c, ".html");
+    xhtml_format(p, conf.template_filename, &fname_c);
     ret->filename = rdtrimc(&fname_c);
   }
   /*  printf(" ! new file '%s', is_leaf == %s\n", ret->filename, (ret->is_leaf)?("true"):("false"));*/
@@ -311,7 +464,7 @@ void xhtml_fixup_layout(xhtmlfile* file)
  * |                 |                 |
  * X            +----X----+           (1)
  *              |         |
- *              Y        (3)
+ *              Y        (2)
  *              |
  *             (3)
  *
@@ -390,35 +543,26 @@ static void xhtml_ponder_layout(paragraph *p)
   currentfile = topfile;
   currentsect = topsection;
 
+  if (conf.leaf_level == 0) {
+    topfile->is_leaf = 1;
+    topfile->sections = topsection;
+    topsection->file = topfile;
+  }
+
   for (; p; p=p->next)
   {
     int level = xhtml_para_level(p);
     if (level>0) /* actually a section */
     {
       xhtmlsection *sect;
-      word *w;
-      char *c;
-      rdstringc fname_c = { 0, 0, NULL };
+      rdstringc frag_c = { 0, 0, NULL };
 
       sect = xhtml_new_section(lastsection);
       lastsection = sect;
       sect->para = p;
-      for (w=(p->kwtext2)?(p->kwtext2):(p->words); w; w=w->next) /* kwtext2 because we want numbers only! */
-      {
-        switch (removeattr(w->type))
-        {
-        case word_Normal:
-         /*case word_Emph:
-         case word_Code:
-         case word_WeakCode:*/
-          xhtml_utostr(w->text, &c);
-          rdaddsc(&fname_c,c);
-          sfree(c);
-          break;
-        }
-      }
-/*      rdaddsc(&fname_c, ".html");*/
-      sect->fragment = rdtrimc(&fname_c);
+
+      xhtml_format(p, conf.template_fragment, &frag_c);
+      sect->fragment = rdtrimc(&frag_c);
       sect->level = level;
       /*      printf(" ! adding para @ %p as sect %s, level %i\n", sect->para, sect->fragment, level);*/
 
@@ -526,7 +670,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);
@@ -567,13 +711,15 @@ static void xhtml_free_file(xhtmlfile* xfile)
  * Main function.
  */
 void xhtml_backend(paragraph *sourceform, keywordlist *in_keywords,
-                  indexdata *in_idx)
+                  indexdata *in_idx, void *unused)
 {
 /*  int i;*/
   indexentry *ientry;
   int ti;
   xhtmlsection *xsect;
 
+  IGNORE(unused);    
+
   sourceparas = sourceform;
   conf = xhtml_configure(sourceform);
   keywords = in_keywords;
@@ -596,8 +742,10 @@ void xhtml_backend(paragraph *sourceform, keywordlist *in_keywords,
   /* new system ... (writes to *.html, but isn't fully trusted) */
   xhtml_do_top_file(topfile, sourceform);
   assert(!topfile->next); /* shouldn't have a sibling at all */
-  xhtml_do_files(topfile->child);
-  xhtml_do_index();
+  if (topfile->child) {
+    xhtml_do_files(topfile->child);
+    xhtml_do_index();
+  }
 
   /* release file, section, index data structures */
   xsect = topsection;
@@ -620,12 +768,16 @@ void xhtml_backend(paragraph *sourceform, keywordlist *in_keywords,
     }
     ientry->backend_data = NULL;
   }
+  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:
@@ -644,8 +796,6 @@ static int xhtml_para_level(paragraph *p)
   }
 }
 
-static char* xhtml_index_filename = "IndexPage.html";
-
 /* Output the nav links for the current file.
  * file == NULL means we're doing the index
  */
@@ -661,9 +811,14 @@ static void xhtml_donavlinks(FILE *fp, xhtmlfile *file)
   if (xhtml_last_file==NULL) {
     fprintf(fp, "Previous | ");
   } else {
-    fprintf(fp, "<a href='%s'>Previous</a> | ", xhtml_last_file->filename);
+    fprintf(fp, "<a href=\"%s\">Previous</a> | ", xhtml_last_file->filename);
+  }
+  fprintf(fp, "<a href=\"%s\">Contents</a> | ", conf.contents_filename);
+  if (file == NULL) {
+    fprintf(fp, "Index | ");
+  } else {
+    fprintf(fp, "<a href=\"%s\">Index</a> | ", conf.index_filename);
   }
-  fprintf(fp, "<a href='Contents.html'>Contents</a> | ");
   if (file != NULL) { /* otherwise we're doing nav links for the index */
     if (xhtml_next_file==NULL)
       xhtml_next_file = file->child;
@@ -676,26 +831,22 @@ static void xhtml_donavlinks(FILE *fp, xhtmlfile *file)
     if (file==NULL) { /* index, so no next file */
       fprintf(fp, "Next        ");
     } else {
-      fprintf(fp, "<a href='%s'>Next</a>", xhtml_index_filename);
+      fprintf(fp, "<a href=\"%s\">Next</a>", conf.index_filename);
     }
   } else {
-    fprintf(fp, "<a href='%s'>Next</a>", xhtml_next_file->filename);
+    fprintf(fp, "<a href=\"%s\">Next</a>", xhtml_next_file->filename);
   }
   fprintf(fp, "</p>\n");
 }
 
 /* Write out the index file */
-static void xhtml_do_index()
+static void xhtml_do_index_body(FILE *fp)
 {
-  word temp_word = { NULL, NULL, word_Normal, 0, 0, L"Index", { NULL, 0, 0} };
   indexentry *y;
   int ti;
-  FILE *fp = fopen(xhtml_index_filename, "w");
 
-  if (fp==NULL)
-    fatal(err_cantopenw, xhtml_index_filename);
-  xhtml_doheader(fp, &temp_word);
-  xhtml_donavlinks(fp, NULL);
+  if (count234(idx->entries) == 0)
+    return;                           /* don't write anything at all */
 
   fprintf(fp, "<dl>\n");
   /* iterate over idx->entries using the tree functions and display everything */
@@ -705,18 +856,18 @@ static void xhtml_do_index()
       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;
       for (i=0; i<xi->nsection; i++) {
        xhtmlsection *sect = xi->sections[i];
        if (sect) {
-         fprintf(fp, "<a href='%s#%s'>", sect->file->filename, sect->fragment);
+         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) {
@@ -728,6 +879,19 @@ static void xhtml_do_index()
     }
   }
   fprintf(fp, "</dl>\n");
+}
+static void xhtml_do_index()
+{
+  word temp_word = { NULL, NULL, word_Normal, 0, 0, L"Index",
+      { NULL, 0, 0}, NULL };
+  FILE *fp = fopen(conf.index_filename, "w");
+
+  if (fp==NULL)
+    fatal(err_cantopenw, conf.index_filename);
+  xhtml_doheader(fp, &temp_word);
+  xhtml_donavlinks(fp, NULL);
+
+  xhtml_do_index_body(fp);
 
   xhtml_donavlinks(fp, NULL);
   xhtml_dofooter(fp);
@@ -751,7 +915,8 @@ static void xhtml_do_file(xhtmlfile *file)
 
   xhtml_donavlinks(fp, file);
 
-  if (file->is_leaf && conf.leaf_contains_contents && xhtml_do_contents(NULL, file)>=conf.leaf_smallest_contents)
+  if (file->is_leaf && conf.leaf_contains_contents &&
+      xhtml_do_contents(NULL, file)>=conf.leaf_smallest_contents)
     xhtml_do_contents(fp, file);
   xhtml_do_sections(fp, file->sections);
   if (!file->is_leaf)
@@ -786,28 +951,44 @@ static void xhtml_do_top_file(xhtmlfile *file, paragraph *sourceform)
   if (!done)
     xhtml_doheader(fp, NULL /* Eek! */);
 
-  /* Do the preamble and copyright */
+  /*
+   * Display the title.
+   */
   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_Title) {
+      xhtml_heading(fp, p, FALSE);
+      break;
     }
   }
+
+  /* Do the preamble */
   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;
     }
   }
 
   xhtml_do_contents(fp, file);
   xhtml_do_sections(fp, file->sections);
+
+  /*
+   * 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);
+  }
+
   xhtml_dofooter(fp);
   fclose(fp);
 }
@@ -837,7 +1018,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;
 
@@ -851,7 +1032,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;
@@ -873,7 +1054,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;
@@ -939,27 +1120,32 @@ static int xhtml_add_contents_entry(FILE *fp, xhtmlsection *section, int limit)
 {
   if (!section || section->level > limit)
     return FALSE;
-  if (fp==NULL)
+  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;
 }
 
@@ -971,23 +1157,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
@@ -997,8 +1185,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;
@@ -1009,12 +1195,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:
@@ -1022,55 +1208,109 @@ 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) {
+        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");
           } 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 (ptype == para_DescribedThing) {
+          fprintf(fp, "</dt>");
+       } else if (ptype == 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_DescribedThing && ptype==para_Description) &&
+               !(p2->type==para_Description && ptype==para_DescribedThing) &&
+               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");
             }
           }
@@ -1081,8 +1321,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);
 }
 
 /*
@@ -1092,11 +1334,11 @@ static void xhtml_doheader(FILE *fp, word *title)
 {
   fprintf(fp, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n");
   fprintf(fp, "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n");
-  fprintf(fp, "<html xmlns='http://www.w3.org/1999/xhtml'>\n\n<head>\n<title>");
+  fprintf(fp, "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n\n<head>\n<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)
@@ -1152,11 +1394,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);
 }
@@ -1190,13 +1432,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); }
@@ -1238,8 +1484,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;
@@ -1286,6 +1538,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) {
@@ -1363,10 +1618,11 @@ 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) || !text->alt)
+               /* 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, ' ');
@@ -1389,7 +1645,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;
@@ -1397,22 +1653,60 @@ 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;
     } else {
-      fragment = ""; /* FIXME: what else can we do? */
-      error(err_whatever, "Couldn't locate heading cross-reference!");
+      if (p->type == para_Title)
+       fragment = "title";
+      else {
+       fragment = ""; /* FIXME: what else can we do? */
+       error(err_whatever, "Couldn't locate heading cross-reference!");
+      }
     }
 
-    if (level>2 && nprefix) { /* FIXME: configurability on the level thing */
-       xhtml_rdaddwc(&t, nprefix, NULL);
-       rdaddc(&t, ' ');               /* FIXME: as below */
-    } else if (tprefix) {
-       xhtml_rdaddwc(&t, tprefix, NULL);
-       rdaddsc(&t, ": ");             /* FIXME: configurability */
+    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, indexable);
+       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, indexable);
+       if (fmt) {
+           char *c;
+           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
+     * 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);
 }
@@ -1420,10 +1714,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);
 }
@@ -1437,10 +1731,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");
 }