Obsoleted the `\preamble' command. Preamble text is now taken to be
[sgt/halibut] / bk_xhtml.c
index 053bf33..b2bed07 100644 (file)
@@ -94,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 *);
@@ -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);
 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);
@@ -875,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)
@@ -1067,14 +1071,14 @@ 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, ptype, first=TRUE;
   stack lcont_stack = stk_new();
@@ -1082,7 +1086,7 @@ static void xhtml_do_paras(FILE *fp, paragraph *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 (ptype = p->type)
     {
@@ -1095,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;
@@ -1142,6 +1145,12 @@ static void xhtml_do_paras(FILE *fp, paragraph *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:
@@ -1331,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); }
@@ -1504,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);
@@ -1564,7 +1577,7 @@ static void xhtml_heading(FILE *fp, paragraph *p)
        xhtml_rdaddwc(&t, nprefix, NULL);
        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);
            }
@@ -1573,7 +1586,7 @@ static void xhtml_heading(FILE *fp, paragraph *p)
        xhtml_rdaddwc(&t, tprefix, NULL);
        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);
            }
@@ -1616,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");
 }