X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/c83c649542e6eefbb72b73d3e33c05f2e372b646..8902e0ed6134c9ecc1177ef535852070d0e7326d:/bk_text.c diff --git a/bk_text.c b/bk_text.c index 5df678e..bde11bb 100644 --- a/bk_text.c +++ b/bk_text.c @@ -12,6 +12,7 @@ typedef struct { alignment align; int just_numbers; wchar_t underline; + wchar_t *number_suffix; } alignstruct; typedef struct { @@ -64,11 +65,13 @@ 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.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].underline = L'\0'; ret.include_version_id = TRUE; ret.indent_preambles = FALSE; @@ -92,6 +95,8 @@ static textconfig text_configure(paragraph *source) { ret.achapter.underline = *uadv(source->keyword); } 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)); } else if (!ustricmp(source->keyword, L"text-section-align")) { wchar_t *p = uadv(source->keyword); int n = 0; @@ -137,6 +142,21 @@ static textconfig text_configure(paragraph *source) { ret.nasect = n+1; } ret.asect[n].just_numbers = utob(p); + } else if (!ustricmp(source->keyword, L"text-section-suffix")) { + wchar_t *p = uadv(source->keyword); + int n = 0; + if (uisdigit(*p)) { + n = utoi(p); + p = uadv(p); + } + if (n >= ret.nasect) { + int i; + ret.asect = resize(ret.asect, n+1); + 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); } 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")) { @@ -162,7 +182,9 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, word spaceword; FILE *fp; char *prefixextra; + int nesting, nestindent; int indentb, indenta; + int done_copyright; IGNORE(keywords); /* we don't happen to need this */ IGNORE(idx); /* or this */ @@ -187,21 +209,29 @@ 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); + done_copyright = FALSE; /* 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. @@ -211,7 +241,6 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, case para_Biblio: /* only touch BiblioCited */ case para_VersionID: case para_Copyright: - case para_Preamble: case para_NoCite: case para_Title: break; @@ -222,8 +251,22 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, case para_Chapter: case para_Appendix: case para_UnnumberedChapter: + /* + * The copyright should come after the preamble but before + * the first chapter title. + */ + if (!done_copyright) { + paragraph *p; + + for (p = sourceform; p; p = p->next) + if (p->type == para_Copyright) + text_para(fp, NULL, NULL, p->words, + conf.indent + nesting, 0, conf.width - nesting); + done_copyright = TRUE; + } text_heading(fp, p->kwtext, p->kwtext2, p->words, conf.achapter, conf.indent, conf.width); + nesting = 0; break; case para_Heading: @@ -234,10 +277,12 @@ 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_DescribedThing: + case para_Description: case para_BiblioCited: case para_Bullet: case para_NumberedList: @@ -251,6 +296,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; @@ -269,8 +319,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); @@ -278,7 +328,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; } @@ -293,7 +345,14 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, * Tidy up */ fclose(fp); - sfree(conf.bullet.text); + { + 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); + } } /* @@ -464,11 +523,19 @@ static void text_heading(FILE *fp, word *tprefix, word *nprefix, word *text, wrappedline *wrapping, *p; if (align.just_numbers && nprefix) { + char *c; text_rdaddwc(&t, nprefix, NULL); - rdaddc(&t, ' '); /* FIXME: as below */ + if (text_convert(align.number_suffix, &c)) { + rdaddsc(&t, c); + sfree(c); + } } else if (!align.just_numbers && tprefix) { + char *c; text_rdaddwc(&t, tprefix, NULL); - rdaddsc(&t, ": "); /* FIXME: configurability */ + if (text_convert(align.number_suffix, &c)) { + rdaddsc(&t, c); + sfree(c); + } } margin = length = (t.text ? strlen(t.text) : 0);