X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/halibut/blobdiff_plain/e5e6bf9d1d215fa25484c6d5832eec96dfaa607b..b80802bae4dfeae315236b136d6181198e84d4d7:/bk_text.c diff --git a/bk_text.c b/bk_text.c index df8ae62..359ea25 100644 --- 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 **); @@ -76,11 +77,15 @@ static textconfig text_configure(paragraph *source) { ret.include_version_id = TRUE; ret.indent_preambles = FALSE; 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")) { @@ -175,8 +180,36 @@ 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 = ""; + 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; @@ -186,20 +219,18 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, 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; } @@ -332,6 +363,7 @@ void text_backend(paragraph *sourceform, keywordlist *keywords, */ fclose(fp); sfree(conf.asect); + sfree(conf.filename); } /* @@ -441,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: @@ -473,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: @@ -529,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); @@ -597,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);