Implemented all the missing rendering features (such as different
[sgt/halibut] / bk_text.c
index 48e99f4..359ea25 100644 (file)
--- a/bk_text.c
+++ b/bk_text.c
@@ -24,6 +24,7 @@ typedef struct {
     int include_version_id;
     int indent_preambles;
     word bullet;
+    char *filename;
 } textconfig;
 
 static int text_convert(wchar_t *, char **);
@@ -65,22 +66,26 @@ static textconfig text_configure(paragraph *source) {
     ret.atitle.underline = L'=';
     ret.achapter.align = LEFT;
     ret.achapter.just_numbers = FALSE;
-    ret.achapter.number_suffix = ustrdup(L": ");
+    ret.achapter.number_suffix = L": ";
     ret.achapter.underline = L'-';
     ret.nasect = 1;
     ret.asect = mknewa(alignstruct, ret.nasect);
     ret.asect[0].align = LEFTPLUS;
     ret.asect[0].just_numbers = TRUE;
-    ret.asect[0].number_suffix = ustrdup(L" ");
+    ret.asect[0].number_suffix = L" ";
     ret.asect[0].underline = L'\0';
     ret.include_version_id = TRUE;
     ret.indent_preambles = FALSE;
-    ret.bullet.text = ustrdup(L"-");
+    ret.bullet.text = L"-";
+    ret.filename = dupstr("output.txt");
 
     for (; source; source = source->next) {
        if (source->type == para_Config) {
            if (!ustricmp(source->keyword, L"text-indent")) {
                ret.indent = utoi(uadv(source->keyword));
+           } else if (!ustricmp(source->keyword, L"text-filename")) {
+               sfree(ret.filename);
+               ret.filename = utoa_dup(uadv(source->keyword));
            } else if (!ustricmp(source->keyword, L"text-indent-code")) {
                ret.indent_code = utoi(uadv(source->keyword));
            } else if (!ustricmp(source->keyword, L"text-width")) {
@@ -96,7 +101,7 @@ static textconfig text_configure(paragraph *source) {
            } else if (!ustricmp(source->keyword, L"text-chapter-numeric")) {
                ret.achapter.just_numbers = utob(uadv(source->keyword));
            } else if (!ustricmp(source->keyword, L"text-chapter-suffix")) {
-               ret.achapter.number_suffix = ustrdup(uadv(source->keyword));
+               ret.achapter.number_suffix = uadv(source->keyword);
            } else if (!ustricmp(source->keyword, L"text-section-align")) {
                wchar_t *p = uadv(source->keyword);
                int n = 0;
@@ -152,11 +157,12 @@ static textconfig text_configure(paragraph *source) {
                if (n >= ret.nasect) {
                    int i;
                    ret.asect = resize(ret.asect, n+1);
-                   for (i = ret.nasect; i <= n; i++)
+                   for (i = ret.nasect; i <= n; i++) {
                        ret.asect[i] = ret.asect[ret.nasect-1];
+                   }
                    ret.nasect = n+1;
                }
-               ret.asect[n].number_suffix = ustrdup(p);
+               ret.asect[n].number_suffix = p;
            } else if (!ustricmp(source->keyword, L"text-title-align")) {
                ret.atitle.align = utoalign(uadv(source->keyword));
            } else if (!ustricmp(source->keyword, L"text-title-underline")) {
@@ -174,30 +180,57 @@ static textconfig text_configure(paragraph *source) {
     return ret;
 }
 
+paragraph *text_config_filename(char *filename)
+{
+    paragraph *p;
+    wchar_t *ufilename, *up;
+    int len;
+
+    p = mknew(paragraph);
+    memset(p, 0, sizeof(*p));
+    p->type = para_Config;
+    p->next = NULL;
+    p->fpos.filename = "<command line>";
+    p->fpos.line = p->fpos.col = -1;
+
+    ufilename = ufroma_dup(filename);
+    len = ustrlen(ufilename) + 2 + lenof(L"text-filename");
+    p->keyword = mknewa(wchar_t, len);
+    up = p->keyword;
+    ustrcpy(up, L"text-filename");
+    up = uadv(up);
+    ustrcpy(up, ufilename);
+    up = uadv(up);
+    *up = L'\0';
+    assert(up - p->keyword < len);
+    sfree(ufilename);
+
+    return p;
+}
+
 void text_backend(paragraph *sourceform, keywordlist *keywords,
-                 indexdata *idx) {
+                 indexdata *idx, void *unused) {
     paragraph *p;
     textconfig conf;
     word *prefix, *body, *wp;
     word spaceword;
     FILE *fp;
     char *prefixextra;
+    int nesting, nestindent;
     int indentb, indenta;
 
+    IGNORE(unused);
     IGNORE(keywords);                 /* we don't happen to need this */
     IGNORE(idx);                      /* or this */
 
     conf = text_configure(sourceform);
 
     /*
-     * Determine the output file name, and open the output file
-     *
-     * FIXME: want configurable output file names here. For the
-     * moment, we'll just call it `output.txt'.
+     * Open the output file.
      */
-    fp = fopen("output.txt", "w");
+    fp = fopen(conf.filename, "w");
     if (!fp) {
-       error(err_cantopenw, "output.txt");
+       error(err_cantopenw, conf.filename);
        return;
     }
 
@@ -207,21 +240,28 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
            text_heading(fp, NULL, NULL, p->words,
                         conf.atitle, conf.indent, conf.width);
 
-    /* Do the preamble and copyright */
-    for (p = sourceform; p; p = p->next)
-       if (p->type == para_Preamble)
-           text_para(fp, NULL, NULL, p->words,
-                     conf.indent_preambles ? conf.indent : 0, 0,
-                     conf.width + (conf.indent_preambles ? 0 : conf.indent));
-    for (p = sourceform; p; p = p->next)
-       if (p->type == para_Copyright)
-           text_para(fp, NULL, NULL, p->words,
-                     conf.indent_preambles ? conf.indent : 0, 0,
-                     conf.width + (conf.indent_preambles ? 0 : conf.indent));
+    nestindent = conf.listindentbefore + conf.listindentafter;
+    nesting = (conf.indent_preambles ? 0 : -conf.indent);
 
     /* Do the main document */
     for (p = sourceform; p; p = p->next) switch (p->type) {
 
+      case para_QuotePush:
+       nesting += 2;
+       break;
+      case para_QuotePop:
+       nesting -= 2;
+       assert(nesting >= 0);
+       break;
+
+      case para_LcontPush:
+       nesting += nestindent;
+       break;
+      case para_LcontPop:
+       nesting -= nestindent;
+       assert(nesting >= 0);
+       break;
+
        /*
         * Things we ignore because we've already processed them or
         * aren't going to touch them in this pass.
@@ -230,8 +270,6 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
       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;
@@ -244,6 +282,7 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
       case para_UnnumberedChapter:
        text_heading(fp, p->kwtext, p->kwtext2, p->words,
                     conf.achapter, conf.indent, conf.width);
+       nesting = 0;
        break;
 
       case para_Heading:
@@ -254,10 +293,13 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
        break;
 
       case para_Rule:
-       text_rule(fp, conf.indent, conf.width);
+       text_rule(fp, conf.indent + nesting, conf.width - nesting);
        break;
 
       case para_Normal:
+      case para_Copyright:
+      case para_DescribedThing:
+      case para_Description:
       case para_BiblioCited:
       case para_Bullet:
       case para_NumberedList:
@@ -271,6 +313,11 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
            prefixextra = ".";         /* FIXME: configurability */
            indentb = conf.listindentbefore;
            indenta = conf.listindentafter;
+       } else if (p->type == para_Description) {
+           prefix = NULL;
+           prefixextra = NULL;
+           indentb = conf.listindentbefore;
+           indenta = conf.listindentafter;
        } else {
            prefix = NULL;
            prefixextra = NULL;
@@ -289,8 +336,8 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
            body = p->words;
        }
        text_para(fp, prefix, prefixextra, body,
-                 conf.indent + indentb, indenta,
-                 conf.width - indentb - indenta);
+                 conf.indent + nesting + indentb, indenta,
+                 conf.width - nesting - indentb - indenta);
        if (wp) {
            wp->next = NULL;
            free_word_list(body);
@@ -298,7 +345,9 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
        break;
 
       case para_Code:
-       text_codepara(fp, p->words, conf.indent + conf.indent_code, conf.width - 2 * conf.indent_code);
+       text_codepara(fp, p->words,
+                     conf.indent + nesting + conf.indent_code,
+                     conf.width - nesting - 2 * conf.indent_code);
        break;
     }
 
@@ -313,14 +362,8 @@ void text_backend(paragraph *sourceform, keywordlist *keywords,
      * Tidy up
      */
     fclose(fp);
-    {
-       int i;
-       sfree(conf.achapter.number_suffix);
-       for (i = 0; i < conf.nasect; i++)
-           sfree(conf.asect[i].number_suffix);
-       sfree(conf.asect);
-       sfree(conf.bullet.text);
-    }
+    sfree(conf.asect);
+    sfree(conf.filename);
 }
 
 /*
@@ -430,18 +473,20 @@ static void text_rdaddwc(rdstringc *rs, word *text, word *end) {
     }
 }
 
-static int text_width(word *);
+static int text_width(void *, word *);
 
-static int text_width_list(word *text) {
+static int text_width_list(void *ctx, word *text) {
     int w = 0;
     while (text) {
-       w += text_width(text);
+       w += text_width(ctx, text);
        text = text->next;
     }
     return w;
 }
 
-static int text_width(word *text) {
+static int text_width(void *ctx, word *text) {
+    IGNORE(ctx);
+
     switch (text->type) {
       case word_HyperLink:
       case word_HyperEnd:
@@ -462,7 +507,7 @@ static int text_width(word *text) {
                 : 0) +
                (text_convert(text->text, NULL) ?
                 ustrlen(text->text) :
-                text_width_list(text->alt)));
+                text_width_list(ctx, text->alt)));
 
       case word_WhiteSpace:
       case word_EmphSpace:
@@ -518,7 +563,7 @@ static void text_heading(FILE *fp, word *tprefix, word *nprefix, word *text,
        wrapwidth = indent + width;
     }
 
-    wrapping = wrap_para(text, firstlinewidth, wrapwidth, text_width);
+    wrapping = wrap_para(text, firstlinewidth, wrapwidth, text_width, NULL, 0);
     for (p = wrapping; p; p = p->next) {
        text_rdaddwc(&t, p->begin, p->end);
        length = (t.text ? strlen(t.text) : 0);
@@ -586,7 +631,7 @@ static void text_para(FILE *fp, word *prefix, char *prefixextra, word *text,
     } else
        e = indent + extraindent;
 
-    wrapping = wrap_para(text, firstlinewidth, width, text_width);
+    wrapping = wrap_para(text, firstlinewidth, width, text_width, NULL, 0);
     for (p = wrapping; p; p = p->next) {
        rdstringc t = { 0, 0, NULL };
        text_rdaddwc(&t, p->begin, p->end);