Apparently we must include the .TH directive in a man page even when
[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
211 /* Do the preamble and copyright */
212 for (p = sourceform; p; p = p->next)
213 if (p->type == para_Preamble)
214 text_para(fp, NULL, NULL, p->words,
215 conf.indent_preambles ? conf.indent : 0, 0,
216 conf.width + (conf.indent_preambles ? 0 : conf.indent));
217 for (p = sourceform; p; p = p->next)
218 if (p->type == para_Copyright)
219 text_para(fp, NULL, NULL, p->words,
220 conf.indent_preambles ? conf.indent : 0, 0,
221 conf.width + (conf.indent_preambles ? 0 : conf.indent));
222
7136a6c7 223 nestindent = conf.listindentbefore + conf.listindentafter;
224 nesting = 0;
225
d7482997 226 /* Do the main document */
227 for (p = sourceform; p; p = p->next) switch (p->type) {
228
7136a6c7 229 case para_LcontPush:
230 nesting++;
231 break;
232 case para_LcontPop:
233 assert(nesting > 0);
234 nesting--;
235 break;
236
d7482997 237 /*
238 * Things we ignore because we've already processed them or
239 * aren't going to touch them in this pass.
240 */
241 case para_IM:
242 case para_BR:
243 case para_Biblio: /* only touch BiblioCited */
244 case para_VersionID:
245 case para_Copyright:
246 case para_Preamble:
247 case para_NoCite:
248 case para_Title:
249 break;
250
251 /*
252 * Chapter titles.
253 */
254 case para_Chapter:
255 case para_Appendix:
256 case para_UnnumberedChapter:
257 text_heading(fp, p->kwtext, p->kwtext2, p->words,
258 conf.achapter, conf.indent, conf.width);
259 break;
260
261 case para_Heading:
262 case para_Subsect:
263 text_heading(fp, p->kwtext, p->kwtext2, p->words,
264 conf.asect[p->aux>=conf.nasect ? conf.nasect-1 : p->aux],
265 conf.indent, conf.width);
266 break;
267
268 case para_Rule:
7136a6c7 269 text_rule(fp, conf.indent + nestindent*nesting,
270 conf.width - nestindent*nesting);
d7482997 271 break;
272
273 case para_Normal:
7136a6c7 274 case para_DescribedThing:
275 case para_Description:
d7482997 276 case para_BiblioCited:
277 case para_Bullet:
278 case para_NumberedList:
279 if (p->type == para_Bullet) {
280 prefix = &conf.bullet;
281 prefixextra = NULL;
282 indentb = conf.listindentbefore;
283 indenta = conf.listindentafter;
284 } else if (p->type == para_NumberedList) {
285 prefix = p->kwtext;
286 prefixextra = "."; /* FIXME: configurability */
287 indentb = conf.listindentbefore;
288 indenta = conf.listindentafter;
7136a6c7 289 } else if (p->type == para_Description) {
290 prefix = NULL;
291 prefixextra = NULL;
292 indentb = conf.listindentbefore;
293 indenta = conf.listindentafter;
d7482997 294 } else {
295 prefix = NULL;
296 prefixextra = NULL;
297 indentb = indenta = 0;
298 }
299 if (p->type == para_BiblioCited) {
300 body = dup_word_list(p->kwtext);
301 for (wp = body; wp->next; wp = wp->next);
302 wp->next = &spaceword;
303 spaceword.next = p->words;
304 spaceword.alt = NULL;
305 spaceword.type = word_WhiteSpace;
306 spaceword.text = NULL;
307 } else {
308 wp = NULL;
309 body = p->words;
310 }
311 text_para(fp, prefix, prefixextra, body,
7136a6c7 312 conf.indent + nestindent*nesting + indentb, indenta,
313 conf.width - nestindent*nesting - indentb - indenta);
d7482997 314 if (wp) {
315 wp->next = NULL;
316 free_word_list(body);
317 }
318 break;
319
320 case para_Code:
7136a6c7 321 text_codepara(fp, p->words,
322 conf.indent + nestindent*nesting + conf.indent_code,
323 conf.width - nestindent*nesting - 2 * conf.indent_code);
d7482997 324 break;
325 }
326
327 /* Do the version ID */
328 if (conf.include_version_id) {
329 for (p = sourceform; p; p = p->next)
330 if (p->type == para_VersionID)
331 text_versionid(fp, p->words);
332 }
333
334 /*
335 * Tidy up
336 */
337 fclose(fp);
677e18a2 338 {
339 int i;
340 sfree(conf.achapter.number_suffix);
341 for (i = 0; i < conf.nasect; i++)
342 sfree(conf.asect[i].number_suffix);
343 sfree(conf.asect);
344 sfree(conf.bullet.text);
345 }
d7482997 346}
347
348/*
349 * Convert a wide string into a string of chars. If `result' is
350 * non-NULL, mallocs the resulting string and stores a pointer to
351 * it in `*result'. If `result' is NULL, merely checks whether all
352 * characters in the string are feasible for the output character
353 * set.
354 *
355 * Return is nonzero if all characters are OK. If not all
356 * characters are OK but `result' is non-NULL, a result _will_
357 * still be generated!
358 */
359static int text_convert(wchar_t *s, char **result) {
360 /*
361 * FIXME. Currently this is ISO8859-1 only.
362 */
363 int doing = (result != 0);
364 int ok = TRUE;
365 char *p = NULL;
366 int plen = 0, psize = 0;
367
368 for (; *s; s++) {
369 wchar_t c = *s;
370 char outc;
371
372 if ((c >= 32 && c <= 126) ||
373 (c >= 160 && c <= 255)) {
374 /* Char is OK. */
375 outc = (char)c;
376 } else {
377 /* Char is not OK. */
378 ok = FALSE;
379 outc = 0xBF; /* approximate the good old DEC `uh?' */
380 }
381 if (doing) {
382 if (plen >= psize) {
383 psize = plen + 256;
384 p = resize(p, psize);
385 }
386 p[plen++] = outc;
387 }
388 }
389 if (doing) {
390 p = resize(p, plen+1);
391 p[plen] = '\0';
392 *result = p;
393 }
394 return ok;
395}
396
397static void text_rdaddwc(rdstringc *rs, word *text, word *end) {
398 char *c;
399
400 for (; text && text != end; text = text->next) switch (text->type) {
401 case word_HyperLink:
402 case word_HyperEnd:
403 case word_UpperXref:
404 case word_LowerXref:
405 case word_XrefEnd:
406 case word_IndexRef:
407 break;
408
409 case word_Normal:
410 case word_Emph:
411 case word_Code:
412 case word_WeakCode:
413 case word_WhiteSpace:
414 case word_EmphSpace:
415 case word_CodeSpace:
416 case word_WkCodeSpace:
417 case word_Quote:
418 case word_EmphQuote:
419 case word_CodeQuote:
420 case word_WkCodeQuote:
421 assert(text->type != word_CodeQuote &&
422 text->type != word_WkCodeQuote);
423 if (towordstyle(text->type) == word_Emph &&
424 (attraux(text->aux) == attr_First ||
425 attraux(text->aux) == attr_Only))
426 rdaddc(rs, '_'); /* FIXME: configurability */
427 else if (towordstyle(text->type) == word_Code &&
428 (attraux(text->aux) == attr_First ||
429 attraux(text->aux) == attr_Only))
430 rdaddc(rs, '`'); /* FIXME: configurability */
431 if (removeattr(text->type) == word_Normal) {
432 if (text_convert(text->text, &c))
433 rdaddsc(rs, c);
434 else
435 text_rdaddwc(rs, text->alt, NULL);
436 sfree(c);
437 } else if (removeattr(text->type) == word_WhiteSpace) {
438 rdaddc(rs, ' ');
439 } else if (removeattr(text->type) == word_Quote) {
440 rdaddc(rs, quoteaux(text->aux) == quote_Open ? '`' : '\'');
441 /* FIXME: configurability */
442 }
443 if (towordstyle(text->type) == word_Emph &&
444 (attraux(text->aux) == attr_Last ||
445 attraux(text->aux) == attr_Only))
446 rdaddc(rs, '_'); /* FIXME: configurability */
447 else if (towordstyle(text->type) == word_Code &&
448 (attraux(text->aux) == attr_Last ||
449 attraux(text->aux) == attr_Only))
450 rdaddc(rs, '\''); /* FIXME: configurability */
451 break;
452 }
453}
454
455static int text_width(word *);
456
457static int text_width_list(word *text) {
458 int w = 0;
459 while (text) {
460 w += text_width(text);
461 text = text->next;
462 }
463 return w;
464}
465
466static int text_width(word *text) {
467 switch (text->type) {
468 case word_HyperLink:
469 case word_HyperEnd:
470 case word_UpperXref:
471 case word_LowerXref:
472 case word_XrefEnd:
473 case word_IndexRef:
474 return 0;
475
476 case word_Normal:
477 case word_Emph:
478 case word_Code:
479 case word_WeakCode:
480 return (((text->type == word_Emph ||
481 text->type == word_Code)
482 ? (attraux(text->aux) == attr_Only ? 2 :
483 attraux(text->aux) == attr_Always ? 0 : 1)
484 : 0) +
485 (text_convert(text->text, NULL) ?
486 ustrlen(text->text) :
487 text_width_list(text->alt)));
488
489 case word_WhiteSpace:
490 case word_EmphSpace:
491 case word_CodeSpace:
492 case word_WkCodeSpace:
493 case word_Quote:
494 case word_EmphQuote:
495 case word_CodeQuote:
496 case word_WkCodeQuote:
497 assert(text->type != word_CodeQuote &&
498 text->type != word_WkCodeQuote);
499 return (((towordstyle(text->type) == word_Emph ||
500 towordstyle(text->type) == word_Code)
501 ? (attraux(text->aux) == attr_Only ? 2 :
502 attraux(text->aux) == attr_Always ? 0 : 1)
503 : 0) + 1);
504 }
505 return 0; /* should never happen */
506}
507
508static void text_heading(FILE *fp, word *tprefix, word *nprefix, word *text,
509 alignstruct align, int indent, int width) {
510 rdstringc t = { 0, 0, NULL };
511 int margin, length;
512 int firstlinewidth, wrapwidth;
513 wrappedline *wrapping, *p;
514
515 if (align.just_numbers && nprefix) {
63223c78 516 char *c;
d7482997 517 text_rdaddwc(&t, nprefix, NULL);
63223c78 518 if (text_convert(align.number_suffix, &c)) {
519 rdaddsc(&t, c);
520 sfree(c);
521 }
d7482997 522 } else if (!align.just_numbers && tprefix) {
63223c78 523 char *c;
d7482997 524 text_rdaddwc(&t, tprefix, NULL);
63223c78 525 if (text_convert(align.number_suffix, &c)) {
526 rdaddsc(&t, c);
527 sfree(c);
528 }
d7482997 529 }
530 margin = length = (t.text ? strlen(t.text) : 0);
531
532 if (align.align == LEFTPLUS) {
533 margin = indent - margin;
534 if (margin < 0) margin = 0;
535 firstlinewidth = indent + width - margin - length;
536 wrapwidth = width;
537 } else if (align.align == LEFT || align.align == CENTRE) {
538 margin = 0;
539 firstlinewidth = indent + width - length;
540 wrapwidth = indent + width;
541 }
542
543 wrapping = wrap_para(text, firstlinewidth, wrapwidth, text_width);
544 for (p = wrapping; p; p = p->next) {
545 text_rdaddwc(&t, p->begin, p->end);
546 length = (t.text ? strlen(t.text) : 0);
547 if (align.align == CENTRE) {
548 margin = (indent + width - length)/2;
549 if (margin < 0) margin = 0;
550 }
551 fprintf(fp, "%*s%s\n", margin, "", t.text);
552 if (align.underline != L'\0') {
553 char *u, uc;
554 wchar_t uw[2];
555 uw[0] = align.underline; uw[1] = L'\0';
556 text_convert(uw, &u);
557 uc = u[0];
558 sfree(u);
559 fprintf(fp, "%*s", margin, "");
560 while (length--)
561 putc(uc, fp);
562 putc('\n', fp);
563 }
564 if (align.align == LEFTPLUS)
565 margin = indent;
566 else
567 margin = 0;
568 sfree(t.text);
569 t = empty_rdstringc;
570 }
571 wrap_free(wrapping);
572 putc('\n', fp);
573
574 sfree(t.text);
575}
576
577static void text_rule(FILE *fp, int indent, int width) {
578 while (indent--) putc(' ', fp);
579 while (width--) putc('-', fp); /* FIXME: configurability! */
580 putc('\n', fp);
581 putc('\n', fp);
582}
583
584static void text_para(FILE *fp, word *prefix, char *prefixextra, word *text,
585 int indent, int extraindent, int width) {
586 wrappedline *wrapping, *p;
587 rdstringc pfx = { 0, 0, NULL };
588 int e;
589 int firstlinewidth = width;
590
591 if (prefix) {
592 text_rdaddwc(&pfx, prefix, NULL);
593 if (prefixextra)
594 rdaddsc(&pfx, prefixextra);
595 fprintf(fp, "%*s%s", indent, "", pfx.text);
c83c6495 596 /* If the prefix is too long, shorten the first line to fit. */
d7482997 597 e = extraindent - strlen(pfx.text);
598 if (e < 0) {
c83c6495 599 firstlinewidth += e; /* this decreases it, since e < 0 */
d7482997 600 if (firstlinewidth < 0) {
601 e = indent + extraindent;
602 firstlinewidth = width;
603 fprintf(fp, "\n");
c83c6495 604 } else
605 e = 0;
d7482997 606 }
607 sfree(pfx.text);
608 } else
609 e = indent + extraindent;
610
611 wrapping = wrap_para(text, firstlinewidth, width, text_width);
612 for (p = wrapping; p; p = p->next) {
613 rdstringc t = { 0, 0, NULL };
614 text_rdaddwc(&t, p->begin, p->end);
615 fprintf(fp, "%*s%s\n", e, "", t.text);
616 e = indent + extraindent;
617 sfree(t.text);
618 }
619 wrap_free(wrapping);
620 putc('\n', fp);
621}
622
623static void text_codepara(FILE *fp, word *text, int indent, int width) {
624 for (; text; text = text->next) if (text->type == word_WeakCode) {
625 char *c;
626 text_convert(text->text, &c);
627 if (strlen(c) > (size_t)width) {
628 /* FIXME: warn */
629 }
630 fprintf(fp, "%*s%s\n", indent, "", c);
631 sfree(c);
632 }
633
634 putc('\n', fp);
635}
636
637static void text_versionid(FILE *fp, word *text) {
638 rdstringc t = { 0, 0, NULL };
639
640 rdaddc(&t, '['); /* FIXME: configurability */
641 text_rdaddwc(&t, text, NULL);
642 rdaddc(&t, ']'); /* FIXME: configurability */
643
644 fprintf(fp, "%s\n", t.text);
645 sfree(t.text);
646}