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