Support for \cfg{input-charset}. Input files can now be in ASCII,
[sgt/halibut] / bk_text.c
CommitLineData
d7482997 1/*
2 * text backend for Halibut
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <assert.h>
8#include "halibut.h"
9
10typedef enum { LEFT, LEFTPLUS, CENTRE } alignment;
11typedef struct {
12 alignment align;
13 int just_numbers;
14 wchar_t underline;
63223c78 15 wchar_t *number_suffix;
d7482997 16} alignstruct;
17
18typedef struct {
19 int indent, indent_code;
20 int listindentbefore, listindentafter;
21 int width;
22 alignstruct atitle, achapter, *asect;
23 int nasect;
24 int include_version_id;
25 int indent_preambles;
26 word bullet;
50d6b4bd 27 char *filename;
d7482997 28} textconfig;
29
30static int text_convert(wchar_t *, char **);
31
32static void text_heading(FILE *, word *, word *, word *, alignstruct, int,int);
33static void text_rule(FILE *, int, int);
34static void text_para(FILE *, word *, char *, word *, int, int, int);
35static void text_codepara(FILE *, word *, int, int);
36static void text_versionid(FILE *, word *);
37
38static alignment utoalign(wchar_t *p) {
39 if (!ustricmp(p, L"centre") || !ustricmp(p, L"center"))
40 return CENTRE;
41 if (!ustricmp(p, L"leftplus"))
42 return LEFTPLUS;
43 return LEFT;
44}
45
46static textconfig text_configure(paragraph *source) {
47 textconfig ret;
48
49 /*
50 * Non-negotiables.
51 */
52 ret.bullet.next = NULL;
53 ret.bullet.alt = NULL;
54 ret.bullet.type = word_Normal;
55 ret.atitle.just_numbers = FALSE; /* ignored */
56
57 /*
58 * Defaults.
59 */
60 ret.indent = 7;
61 ret.indent_code = 2;
62 ret.listindentbefore = 1;
63 ret.listindentafter = 3;
64 ret.width = 68;
65 ret.atitle.align = CENTRE;
66 ret.atitle.underline = L'=';
67 ret.achapter.align = LEFT;
68 ret.achapter.just_numbers = FALSE;
e5e6bf9d 69 ret.achapter.number_suffix = L": ";
d7482997 70 ret.achapter.underline = L'-';
71 ret.nasect = 1;
72 ret.asect = mknewa(alignstruct, ret.nasect);
73 ret.asect[0].align = LEFTPLUS;
74 ret.asect[0].just_numbers = TRUE;
e5e6bf9d 75 ret.asect[0].number_suffix = L" ";
d7482997 76 ret.asect[0].underline = L'\0';
77 ret.include_version_id = TRUE;
78 ret.indent_preambles = FALSE;
e5e6bf9d 79 ret.bullet.text = L"-";
50d6b4bd 80 ret.filename = dupstr("output.txt");
d7482997 81
82 for (; source; source = source->next) {
83 if (source->type == para_Config) {
84 if (!ustricmp(source->keyword, L"text-indent")) {
85 ret.indent = utoi(uadv(source->keyword));
50d6b4bd 86 } else if (!ustricmp(source->keyword, L"text-filename")) {
87 sfree(ret.filename);
88 ret.filename = utoa_dup(uadv(source->keyword));
d7482997 89 } else if (!ustricmp(source->keyword, L"text-indent-code")) {
90 ret.indent_code = utoi(uadv(source->keyword));
91 } else if (!ustricmp(source->keyword, L"text-width")) {
92 ret.width = utoi(uadv(source->keyword));
93 } else if (!ustricmp(source->keyword, L"text-list-indent")) {
94 ret.listindentbefore = utoi(uadv(source->keyword));
95 } else if (!ustricmp(source->keyword, L"text-listitem-indent")) {
96 ret.listindentafter = utoi(uadv(source->keyword));
97 } else if (!ustricmp(source->keyword, L"text-chapter-align")) {
98 ret.achapter.align = utoalign(uadv(source->keyword));
99 } else if (!ustricmp(source->keyword, L"text-chapter-underline")) {
100 ret.achapter.underline = *uadv(source->keyword);
101 } else if (!ustricmp(source->keyword, L"text-chapter-numeric")) {
c83c6495 102 ret.achapter.just_numbers = utob(uadv(source->keyword));
63223c78 103 } else if (!ustricmp(source->keyword, L"text-chapter-suffix")) {
e5e6bf9d 104 ret.achapter.number_suffix = uadv(source->keyword);
d7482997 105 } else if (!ustricmp(source->keyword, L"text-section-align")) {
106 wchar_t *p = uadv(source->keyword);
107 int n = 0;
108 if (uisdigit(*p)) {
109 n = utoi(p);
110 p = uadv(p);
111 }
112 if (n >= ret.nasect) {
113 int i;
114 ret.asect = resize(ret.asect, n+1);
115 for (i = ret.nasect; i <= n; i++)
116 ret.asect[i] = ret.asect[ret.nasect-1];
117 ret.nasect = n+1;
118 }
119 ret.asect[n].align = utoalign(p);
120 } else if (!ustricmp(source->keyword, L"text-section-underline")) {
121 wchar_t *p = uadv(source->keyword);
122 int n = 0;
123 if (uisdigit(*p)) {
124 n = utoi(p);
125 p = uadv(p);
126 }
127 if (n >= ret.nasect) {
128 int i;
129 ret.asect = resize(ret.asect, n+1);
130 for (i = ret.nasect; i <= n; i++)
131 ret.asect[i] = ret.asect[ret.nasect-1];
132 ret.nasect = n+1;
133 }
134 ret.asect[n].underline = *p;
135 } else if (!ustricmp(source->keyword, L"text-section-numeric")) {
136 wchar_t *p = uadv(source->keyword);
137 int n = 0;
138 if (uisdigit(*p)) {
139 n = utoi(p);
140 p = uadv(p);
141 }
142 if (n >= ret.nasect) {
143 int i;
144 ret.asect = resize(ret.asect, n+1);
145 for (i = ret.nasect; i <= n; i++)
146 ret.asect[i] = ret.asect[ret.nasect-1];
147 ret.nasect = n+1;
148 }
149 ret.asect[n].just_numbers = utob(p);
63223c78 150 } else if (!ustricmp(source->keyword, L"text-section-suffix")) {
151 wchar_t *p = uadv(source->keyword);
152 int n = 0;
153 if (uisdigit(*p)) {
154 n = utoi(p);
155 p = uadv(p);
156 }
157 if (n >= ret.nasect) {
158 int i;
159 ret.asect = resize(ret.asect, n+1);
e5e6bf9d 160 for (i = ret.nasect; i <= n; i++) {
63223c78 161 ret.asect[i] = ret.asect[ret.nasect-1];
e5e6bf9d 162 }
63223c78 163 ret.nasect = n+1;
164 }
e5e6bf9d 165 ret.asect[n].number_suffix = p;
d7482997 166 } else if (!ustricmp(source->keyword, L"text-title-align")) {
167 ret.atitle.align = utoalign(uadv(source->keyword));
168 } else if (!ustricmp(source->keyword, L"text-title-underline")) {
169 ret.atitle.underline = *uadv(source->keyword);
170 } else if (!ustricmp(source->keyword, L"text-versionid")) {
171 ret.include_version_id = utob(uadv(source->keyword));
172 } else if (!ustricmp(source->keyword, L"text-indent-preamble")) {
173 ret.indent_preambles = utob(uadv(source->keyword));
174 } else if (!ustricmp(source->keyword, L"text-bullet")) {
175 ret.bullet.text = uadv(source->keyword);
176 }
177 }
178 }
179
180 return ret;
181}
182
ba9c1487 183paragraph *text_config_filename(char *filename)
184{
185 paragraph *p;
186 wchar_t *ufilename, *up;
187 int len;
188
189 p = mknew(paragraph);
190 memset(p, 0, sizeof(*p));
191 p->type = para_Config;
192 p->next = NULL;
193 p->fpos.filename = "<command line>";
194 p->fpos.line = p->fpos.col = -1;
195
196 ufilename = ufroma_dup(filename);
197 len = ustrlen(ufilename) + 2 + lenof(L"text-filename");
198 p->keyword = mknewa(wchar_t, len);
199 up = p->keyword;
200 ustrcpy(up, L"text-filename");
201 up = uadv(up);
202 ustrcpy(up, ufilename);
203 up = uadv(up);
204 *up = L'\0';
205 assert(up - p->keyword < len);
206 sfree(ufilename);
207
208 return p;
209}
210
d7482997 211void text_backend(paragraph *sourceform, keywordlist *keywords,
43341922 212 indexdata *idx, void *unused) {
d7482997 213 paragraph *p;
214 textconfig conf;
215 word *prefix, *body, *wp;
216 word spaceword;
217 FILE *fp;
218 char *prefixextra;
7136a6c7 219 int nesting, nestindent;
d7482997 220 int indentb, indenta;
221
43341922 222 IGNORE(unused);
d7482997 223 IGNORE(keywords); /* we don't happen to need this */
224 IGNORE(idx); /* or this */
225
226 conf = text_configure(sourceform);
227
228 /*
50d6b4bd 229 * Open the output file.
d7482997 230 */
50d6b4bd 231 fp = fopen(conf.filename, "w");
d7482997 232 if (!fp) {
50d6b4bd 233 error(err_cantopenw, conf.filename);
d7482997 234 return;
235 }
236
237 /* Do the title */
238 for (p = sourceform; p; p = p->next)
239 if (p->type == para_Title)
240 text_heading(fp, NULL, NULL, p->words,
241 conf.atitle, conf.indent, conf.width);
242
7136a6c7 243 nestindent = conf.listindentbefore + conf.listindentafter;
8902e0ed 244 nesting = (conf.indent_preambles ? 0 : -conf.indent);
7136a6c7 245
d7482997 246 /* Do the main document */
247 for (p = sourceform; p; p = p->next) switch (p->type) {
248
2614b01d 249 case para_QuotePush:
250 nesting += 2;
251 break;
252 case para_QuotePop:
253 nesting -= 2;
254 assert(nesting >= 0);
255 break;
256
7136a6c7 257 case para_LcontPush:
2614b01d 258 nesting += nestindent;
7136a6c7 259 break;
260 case para_LcontPop:
2614b01d 261 nesting -= nestindent;
262 assert(nesting >= 0);
7136a6c7 263 break;
264
d7482997 265 /*
266 * Things we ignore because we've already processed them or
267 * aren't going to touch them in this pass.
268 */
269 case para_IM:
270 case para_BR:
271 case para_Biblio: /* only touch BiblioCited */
272 case para_VersionID:
d7482997 273 case para_NoCite:
274 case para_Title:
275 break;
276
277 /*
278 * Chapter titles.
279 */
280 case para_Chapter:
281 case para_Appendix:
282 case para_UnnumberedChapter:
283 text_heading(fp, p->kwtext, p->kwtext2, p->words,
284 conf.achapter, conf.indent, conf.width);
8902e0ed 285 nesting = 0;
d7482997 286 break;
287
288 case para_Heading:
289 case para_Subsect:
290 text_heading(fp, p->kwtext, p->kwtext2, p->words,
291 conf.asect[p->aux>=conf.nasect ? conf.nasect-1 : p->aux],
292 conf.indent, conf.width);
293 break;
294
295 case para_Rule:
2614b01d 296 text_rule(fp, conf.indent + nesting, conf.width - nesting);
d7482997 297 break;
298
299 case para_Normal:
9057a0a8 300 case para_Copyright:
7136a6c7 301 case para_DescribedThing:
302 case para_Description:
d7482997 303 case para_BiblioCited:
304 case para_Bullet:
305 case para_NumberedList:
306 if (p->type == para_Bullet) {
307 prefix = &conf.bullet;
308 prefixextra = NULL;
309 indentb = conf.listindentbefore;
310 indenta = conf.listindentafter;
311 } else if (p->type == para_NumberedList) {
312 prefix = p->kwtext;
313 prefixextra = "."; /* FIXME: configurability */
314 indentb = conf.listindentbefore;
315 indenta = conf.listindentafter;
7136a6c7 316 } else if (p->type == para_Description) {
317 prefix = NULL;
318 prefixextra = NULL;
319 indentb = conf.listindentbefore;
320 indenta = conf.listindentafter;
d7482997 321 } else {
322 prefix = NULL;
323 prefixextra = NULL;
324 indentb = indenta = 0;
325 }
326 if (p->type == para_BiblioCited) {
327 body = dup_word_list(p->kwtext);
328 for (wp = body; wp->next; wp = wp->next);
329 wp->next = &spaceword;
330 spaceword.next = p->words;
331 spaceword.alt = NULL;
332 spaceword.type = word_WhiteSpace;
333 spaceword.text = NULL;
334 } else {
335 wp = NULL;
336 body = p->words;
337 }
338 text_para(fp, prefix, prefixextra, body,
2614b01d 339 conf.indent + nesting + indentb, indenta,
340 conf.width - nesting - indentb - indenta);
d7482997 341 if (wp) {
342 wp->next = NULL;
343 free_word_list(body);
344 }
345 break;
346
347 case para_Code:
7136a6c7 348 text_codepara(fp, p->words,
2614b01d 349 conf.indent + nesting + conf.indent_code,
350 conf.width - nesting - 2 * conf.indent_code);
d7482997 351 break;
352 }
353
354 /* Do the version ID */
355 if (conf.include_version_id) {
356 for (p = sourceform; p; p = p->next)
357 if (p->type == para_VersionID)
358 text_versionid(fp, p->words);
359 }
360
361 /*
362 * Tidy up
363 */
364 fclose(fp);
e5e6bf9d 365 sfree(conf.asect);
50d6b4bd 366 sfree(conf.filename);
d7482997 367}
368
369/*
370 * Convert a wide string into a string of chars. If `result' is
371 * non-NULL, mallocs the resulting string and stores a pointer to
372 * it in `*result'. If `result' is NULL, merely checks whether all
373 * characters in the string are feasible for the output character
374 * set.
375 *
376 * Return is nonzero if all characters are OK. If not all
377 * characters are OK but `result' is non-NULL, a result _will_
378 * still be generated!
379 */
380static int text_convert(wchar_t *s, char **result) {
381 /*
382 * FIXME. Currently this is ISO8859-1 only.
383 */
384 int doing = (result != 0);
385 int ok = TRUE;
386 char *p = NULL;
387 int plen = 0, psize = 0;
388
389 for (; *s; s++) {
390 wchar_t c = *s;
391 char outc;
392
393 if ((c >= 32 && c <= 126) ||
394 (c >= 160 && c <= 255)) {
395 /* Char is OK. */
396 outc = (char)c;
397 } else {
398 /* Char is not OK. */
399 ok = FALSE;
400 outc = 0xBF; /* approximate the good old DEC `uh?' */
401 }
402 if (doing) {
403 if (plen >= psize) {
404 psize = plen + 256;
405 p = resize(p, psize);
406 }
407 p[plen++] = outc;
408 }
409 }
410 if (doing) {
411 p = resize(p, plen+1);
412 p[plen] = '\0';
413 *result = p;
414 }
415 return ok;
416}
417
418static void text_rdaddwc(rdstringc *rs, word *text, word *end) {
419 char *c;
420
421 for (; text && text != end; text = text->next) switch (text->type) {
422 case word_HyperLink:
423 case word_HyperEnd:
424 case word_UpperXref:
425 case word_LowerXref:
426 case word_XrefEnd:
427 case word_IndexRef:
428 break;
429
430 case word_Normal:
431 case word_Emph:
432 case word_Code:
433 case word_WeakCode:
434 case word_WhiteSpace:
435 case word_EmphSpace:
436 case word_CodeSpace:
437 case word_WkCodeSpace:
438 case word_Quote:
439 case word_EmphQuote:
440 case word_CodeQuote:
441 case word_WkCodeQuote:
442 assert(text->type != word_CodeQuote &&
443 text->type != word_WkCodeQuote);
444 if (towordstyle(text->type) == word_Emph &&
445 (attraux(text->aux) == attr_First ||
446 attraux(text->aux) == attr_Only))
447 rdaddc(rs, '_'); /* FIXME: configurability */
448 else if (towordstyle(text->type) == word_Code &&
449 (attraux(text->aux) == attr_First ||
450 attraux(text->aux) == attr_Only))
451 rdaddc(rs, '`'); /* FIXME: configurability */
452 if (removeattr(text->type) == word_Normal) {
12efc259 453 if (text_convert(text->text, &c) || !text->alt)
d7482997 454 rdaddsc(rs, c);
455 else
456 text_rdaddwc(rs, text->alt, NULL);
457 sfree(c);
458 } else if (removeattr(text->type) == word_WhiteSpace) {
459 rdaddc(rs, ' ');
460 } else if (removeattr(text->type) == word_Quote) {
461 rdaddc(rs, quoteaux(text->aux) == quote_Open ? '`' : '\'');
462 /* FIXME: configurability */
463 }
464 if (towordstyle(text->type) == word_Emph &&
465 (attraux(text->aux) == attr_Last ||
466 attraux(text->aux) == attr_Only))
467 rdaddc(rs, '_'); /* FIXME: configurability */
468 else if (towordstyle(text->type) == word_Code &&
469 (attraux(text->aux) == attr_Last ||
470 attraux(text->aux) == attr_Only))
471 rdaddc(rs, '\''); /* FIXME: configurability */
472 break;
473 }
474}
475
43341922 476static int text_width(void *, word *);
d7482997 477
43341922 478static int text_width_list(void *ctx, word *text) {
d7482997 479 int w = 0;
480 while (text) {
43341922 481 w += text_width(ctx, text);
d7482997 482 text = text->next;
483 }
484 return w;
485}
486
43341922 487static int text_width(void *ctx, word *text) {
488 IGNORE(ctx);
489
d7482997 490 switch (text->type) {
491 case word_HyperLink:
492 case word_HyperEnd:
493 case word_UpperXref:
494 case word_LowerXref:
495 case word_XrefEnd:
496 case word_IndexRef:
497 return 0;
498
499 case word_Normal:
500 case word_Emph:
501 case word_Code:
502 case word_WeakCode:
503 return (((text->type == word_Emph ||
504 text->type == word_Code)
505 ? (attraux(text->aux) == attr_Only ? 2 :
506 attraux(text->aux) == attr_Always ? 0 : 1)
507 : 0) +
12efc259 508 (text_convert(text->text, NULL) || !text->alt ?
d7482997 509 ustrlen(text->text) :
43341922 510 text_width_list(ctx, text->alt)));
d7482997 511
512 case word_WhiteSpace:
513 case word_EmphSpace:
514 case word_CodeSpace:
515 case word_WkCodeSpace:
516 case word_Quote:
517 case word_EmphQuote:
518 case word_CodeQuote:
519 case word_WkCodeQuote:
520 assert(text->type != word_CodeQuote &&
521 text->type != word_WkCodeQuote);
522 return (((towordstyle(text->type) == word_Emph ||
523 towordstyle(text->type) == word_Code)
524 ? (attraux(text->aux) == attr_Only ? 2 :
525 attraux(text->aux) == attr_Always ? 0 : 1)
526 : 0) + 1);
527 }
528 return 0; /* should never happen */
529}
530
531static void text_heading(FILE *fp, word *tprefix, word *nprefix, word *text,
532 alignstruct align, int indent, int width) {
533 rdstringc t = { 0, 0, NULL };
534 int margin, length;
535 int firstlinewidth, wrapwidth;
536 wrappedline *wrapping, *p;
537
538 if (align.just_numbers && nprefix) {
63223c78 539 char *c;
d7482997 540 text_rdaddwc(&t, nprefix, NULL);
63223c78 541 if (text_convert(align.number_suffix, &c)) {
542 rdaddsc(&t, c);
543 sfree(c);
544 }
d7482997 545 } else if (!align.just_numbers && tprefix) {
63223c78 546 char *c;
d7482997 547 text_rdaddwc(&t, tprefix, NULL);
63223c78 548 if (text_convert(align.number_suffix, &c)) {
549 rdaddsc(&t, c);
550 sfree(c);
551 }
d7482997 552 }
553 margin = length = (t.text ? strlen(t.text) : 0);
554
555 if (align.align == LEFTPLUS) {
556 margin = indent - margin;
557 if (margin < 0) margin = 0;
558 firstlinewidth = indent + width - margin - length;
559 wrapwidth = width;
560 } else if (align.align == LEFT || align.align == CENTRE) {
561 margin = 0;
562 firstlinewidth = indent + width - length;
563 wrapwidth = indent + width;
564 }
565
43341922 566 wrapping = wrap_para(text, firstlinewidth, wrapwidth, text_width, NULL, 0);
d7482997 567 for (p = wrapping; p; p = p->next) {
568 text_rdaddwc(&t, p->begin, p->end);
569 length = (t.text ? strlen(t.text) : 0);
570 if (align.align == CENTRE) {
571 margin = (indent + width - length)/2;
572 if (margin < 0) margin = 0;
573 }
574 fprintf(fp, "%*s%s\n", margin, "", t.text);
575 if (align.underline != L'\0') {
576 char *u, uc;
577 wchar_t uw[2];
578 uw[0] = align.underline; uw[1] = L'\0';
579 text_convert(uw, &u);
580 uc = u[0];
581 sfree(u);
582 fprintf(fp, "%*s", margin, "");
583 while (length--)
584 putc(uc, fp);
585 putc('\n', fp);
586 }
587 if (align.align == LEFTPLUS)
588 margin = indent;
589 else
590 margin = 0;
591 sfree(t.text);
592 t = empty_rdstringc;
593 }
594 wrap_free(wrapping);
595 putc('\n', fp);
596
597 sfree(t.text);
598}
599
600static void text_rule(FILE *fp, int indent, int width) {
601 while (indent--) putc(' ', fp);
602 while (width--) putc('-', fp); /* FIXME: configurability! */
603 putc('\n', fp);
604 putc('\n', fp);
605}
606
607static void text_para(FILE *fp, word *prefix, char *prefixextra, word *text,
608 int indent, int extraindent, int width) {
609 wrappedline *wrapping, *p;
610 rdstringc pfx = { 0, 0, NULL };
611 int e;
612 int firstlinewidth = width;
613
614 if (prefix) {
615 text_rdaddwc(&pfx, prefix, NULL);
616 if (prefixextra)
617 rdaddsc(&pfx, prefixextra);
618 fprintf(fp, "%*s%s", indent, "", pfx.text);
c83c6495 619 /* If the prefix is too long, shorten the first line to fit. */
d7482997 620 e = extraindent - strlen(pfx.text);
621 if (e < 0) {
c83c6495 622 firstlinewidth += e; /* this decreases it, since e < 0 */
d7482997 623 if (firstlinewidth < 0) {
624 e = indent + extraindent;
625 firstlinewidth = width;
626 fprintf(fp, "\n");
c83c6495 627 } else
628 e = 0;
d7482997 629 }
630 sfree(pfx.text);
631 } else
632 e = indent + extraindent;
633
43341922 634 wrapping = wrap_para(text, firstlinewidth, width, text_width, NULL, 0);
d7482997 635 for (p = wrapping; p; p = p->next) {
636 rdstringc t = { 0, 0, NULL };
637 text_rdaddwc(&t, p->begin, p->end);
638 fprintf(fp, "%*s%s\n", e, "", t.text);
639 e = indent + extraindent;
640 sfree(t.text);
641 }
642 wrap_free(wrapping);
643 putc('\n', fp);
644}
645
646static void text_codepara(FILE *fp, word *text, int indent, int width) {
647 for (; text; text = text->next) if (text->type == word_WeakCode) {
648 char *c;
649 text_convert(text->text, &c);
650 if (strlen(c) > (size_t)width) {
651 /* FIXME: warn */
652 }
653 fprintf(fp, "%*s%s\n", indent, "", c);
654 sfree(c);
655 }
656
657 putc('\n', fp);
658}
659
660static void text_versionid(FILE *fp, word *text) {
661 rdstringc t = { 0, 0, NULL };
662
663 rdaddc(&t, '['); /* FIXME: configurability */
664 text_rdaddwc(&t, text, NULL);
665 rdaddc(&t, ']'); /* FIXME: configurability */
666
667 fprintf(fp, "%s\n", t.text);
668 sfree(t.text);
669}